Repository: sxyazi/yazi
File: yazi-plugin/src/elements/line.rs (Rust)

Code before:
		crate::impl_area_method!(methods);
		crate::impl_style_method!(methods, inner.style);
		crate::impl_style_shorthands!(methods, inner.style);

		methods.add_method("width", |_, me, ()| Ok(me.inner.width()));
		methods.add_function_mut("align", |_, (ud, align): (AnyUserData, u8)| {
			ud.borrow_mut::<Self>()?.inner.alignment = Some(match align {
				CENTER => ratatui::layout::Alignment::Center,
				RIGHT => ratatui::layout::Alignment::Right,
				_ => ratatui::layout::Alignment::Left,
			});
			Ok(ud)
		});
		methods.add_method("visible", |_, me, ()| {
			Ok(me.inner.iter().flat_map(|s| s.content.chars()).any(|c| c.width().unwrap_or(0) > 0))
		});
		methods.add_function_mut("truncate", |_, (ud, t): (AnyUserData, Table)| {
			let mut me = ud.borrow_mut::<Self>()?;
			let max = t.raw_get("max")?;

			let mut width = 0;
			'outer: for (x, span) in me.inner.iter_mut().enumerate() {
				for (y, c) in span.content.char_indices() {
					width += c.width().unwrap_or(0);
					if width < max {
						continue;
					} else if width == max && span.content[y..].chars().nth(1).is_none() {
						continue;
					}

					match &mut span.content {
						Cow::Borrowed(s) => span.content = Cow::Borrowed(&s[..y]),
						Cow::Owned(s) => s.truncate(y),
					}
					me.inner.spans.truncate(x + 1);
					me.inner.spans.push(ratatui::text::Span::raw("…"));
					break 'outer;
				}
			}
			Ok(ud)
		});
	}
}


Review comment (suggestion):
Using `raw_get("ellipsis")?` will error out if the key is missing rather than defaulting to `Value::Nil`. Consider using `t.raw_get("ellipsis").unwrap_or(Value::Nil)` or a lookup with a default to make `ellipsis` truly optional.
```suggestion
			let ellipsis = match t.raw_get::<Value>("ellipsis").unwrap_or(Value::Nil) {
```

Code after:
		crate::impl_area_method!(methods);
		crate::impl_style_method!(methods, inner.style);
		crate::impl_style_shorthands!(methods, inner.style);

		methods.add_method("width", |_, me, ()| Ok(me.inner.width()));
		methods.add_function_mut("align", |_, (ud, align): (AnyUserData, u8)| {
			ud.borrow_mut::<Self>()?.inner.alignment = Some(match align {
				CENTER => ratatui::layout::Alignment::Center,
				RIGHT => ratatui::layout::Alignment::Right,
				_ => ratatui::layout::Alignment::Left,
			});
			Ok(ud)
		});
		methods.add_method("visible", |_, me, ()| {
			Ok(me.inner.iter().flat_map(|s| s.content.chars()).any(|c| c.width().unwrap_or(0) > 0))
		});
		methods.add_function_mut("truncate", |_, (ud, t): (AnyUserData, Table)| {
			let mut me = ud.borrow_mut::<Self>()?;

			let max = t.raw_get("max")?;
			if max < 1 {
				me.inner.spans.clear();
				return Ok(ud);
			}

			let ellipsis = match t.raw_get::<Value>("ellipsis")? {
				Value::Nil => (1, ratatui::text::Span::raw("…")),
				v => {
					let mut span = Span::try_from(v)?;
					(span.truncate(max), span.0)
				}
			};

			let (mut width, mut last) = (0, None);
			'outer: for (x, span) in me.inner.iter_mut().enumerate() {
				for (y, c) in span.content.char_indices() {
					width += c.width().unwrap_or(0);
					match last {
						None if width > max - ellipsis.0 => last = Some((false, x, y)),
						Some((false, x, y)) if width > max => {
							last = Some((true, x, y));
							break 'outer;
						}
						_ => {}
					}
				}
			}

			if let Some((true, x, y)) = last {
				let spans = &mut me.inner.spans;
				match &mut spans[x].content {