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):
[nitpick] Document the expected keys for the `truncate` Lua method—especially how `ellipsis` should be provided—so users know that omitting it will use a default.

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 {