Repository: sxyazi/yazi
File: yazi-plugin/src/utils/text.rs (Rust)

Code before:
	pub(super) fn hash(lua: &Lua) -> mlua::Result<Function> {
		lua.create_async_function(move |_, s: mlua::String| async move {
			Ok(format!("{:x}", XxHash3_128::oneshot(&s.as_bytes())))
		})
	}

	pub(super) fn quote(lua: &Lua) -> mlua::Result<Function> {
		lua.create_function(|lua, (s, unix): (mlua::String, Option<bool>)| {
			let s = s.to_str()?;
			let s = match unix {
				Some(true) => yazi_shared::shell::escape_unix(s.as_ref()),
				Some(false) => yazi_shared::shell::escape_windows(s.as_ref()),
				None => yazi_shared::shell::escape_native(s.as_ref()),
			};
			lua.create_string(s.as_ref())
		})
	}

	pub(super) fn truncate(lua: &Lua) -> mlua::Result<Function> {
		fn traverse(
			it: impl Iterator<Item = (usize, char)>,
			max: usize,
		) -> (Option<usize>, usize, bool) {
			let (mut adv, mut last) = (0, 0);
			let idx = it
				.take_while(|&(_, c)| {
					last = adv;
					adv += c.width().unwrap_or(0);
					adv <= max
				})
				.map(|(i, _)| i)
				.last();
			(idx, last, adv > max)
		}

		lua.create_function(|lua, (s, t): (mlua::String, Table)| {
			let b = s.as_bytes();
			if b.is_empty() {
				return Ok(s);
			}

			let max = t.raw_get("max")?;
			if b.len() <= max {
				return Ok(s);
			} else if max < 1 {
				return lua.create_string("");
			}

			let lossy = String::from_utf8_lossy(&b);
			let rtl = t.raw_get("rtl").unwrap_or(false);
			let (idx, width, remain) = if rtl {

Review comment (suggestion):
[nitpick] Consider adding an inline comment explaining the tuple assignment for clarity, especially for developers less familiar with this pattern.
```suggestion
				.take_while(|&(_, c)| {
					// Update `last` to the previous value of `adv` and increment `adv` by the width of the current character (or 0 if unavailable).
```

Code after:
	pub(super) fn hash(lua: &Lua) -> mlua::Result<Function> {
		lua.create_async_function(move |_, s: mlua::String| async move {
			Ok(format!("{:x}", XxHash3_128::oneshot(&s.as_bytes())))
		})
	}

	pub(super) fn quote(lua: &Lua) -> mlua::Result<Function> {
		lua.create_function(|lua, (s, unix): (mlua::String, Option<bool>)| {
			let s = s.to_str()?;
			let s = match unix {
				Some(true) => yazi_shared::shell::escape_unix(s.as_ref()),
				Some(false) => yazi_shared::shell::escape_windows(s.as_ref()),
				None => yazi_shared::shell::escape_native(s.as_ref()),
			};
			lua.create_string(s.as_ref())
		})
	}

	pub(super) fn truncate(lua: &Lua) -> mlua::Result<Function> {
		fn traverse(
			it: impl Iterator<Item = (usize, char)>,
			max: usize,
		) -> (Option<usize>, usize, bool) {
			let (mut adv, mut last) = (0, 0);
			let idx = it
				.take_while(|&(_, c)| {
					(last, adv) = (adv, adv + c.width().unwrap_or(0));
					adv <= max
				})
				.map(|(i, _)| i)
				.last();
			(idx, last, adv > max)
		}

		lua.create_function(|lua, (s, t): (mlua::String, Table)| {
			let b = s.as_bytes();
			if b.is_empty() {
				return Ok(s);
			}

			let max = t.raw_get("max")?;
			if b.len() <= max {
				return Ok(s);
			} else if max < 1 {
				return lua.create_string("");
			}

			let lossy = String::from_utf8_lossy(&b);
			let rtl = t.raw_get("rtl").unwrap_or(false);
			let (idx, width, remain) = if rtl {
				traverse(lossy.char_indices().rev(), max)