From c4dc35828252336fcd64d0cf1b7e35683701bc5c Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 27 Apr 2021 06:20:37 -0700 Subject: [PATCH] add truncate_left_to_width refs: #647 --- config/src/lua.rs | 31 +++++++++++++++++++ docs/changelog.md | 2 +- .../lua/wezterm/truncate_left_to_width.md | 15 +++++++++ docs/config/lua/wezterm/truncate_to_width.md | 3 ++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 docs/config/lua/wezterm/truncate_left_to_width.md diff --git a/config/src/lua.rs b/config/src/lua.rs index ac70896c4..87457365d 100644 --- a/config/src/lua.rs +++ b/config/src/lua.rs @@ -151,6 +151,37 @@ pub fn make_lua_context(config_file: &Path) -> anyhow::Result { )?, )?; + wezterm_mod.set( + "truncate_left_to_width", + lua.create_function( + |_, (s, max_width, min_width): (String, usize, Option)| { + let mut result = vec![]; + let mut len = 0; + for g in s.graphemes(true).rev() { + let g_len = grapheme_column_width(g); + if g_len + len > max_width { + break; + } + result.push(g); + len += g_len; + } + + if let Some(min_width) = min_width { + while len < min_width { + if len >= max_width { + break; + } + result.push(" "); + len += 1; + } + } + + result.reverse(); + Ok(result.join("")) + }, + )?, + )?; + wezterm_mod.set("font", lua.create_function(font)?)?; wezterm_mod.set( "font_with_fallback", diff --git a/docs/changelog.md b/docs/changelog.md index d3c60aff2..949c0ec53 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -46,7 +46,7 @@ As features stabilize some brief notes about them will accumulate here. * Fixed: tabs would remain hovered after moving the mouse down into the main terminal area [#591](https://github.com/wez/wezterm/issues/591) * New: [tab_bar_at_bottom](config/lua/config/tab_bar_at_bottom.md) setting to put the tab bar at the bottom of the window [#278](https://github.com/wez/wezterm/issues/278) * New: [wezterm.column_width](config/lua/wezterm/column_width.md) function for measuring the displayed width of a string -* New: [wezterm.truncate_to_width](config/lua/wezterm/truncate_to_width.md) function for truncating/padding a string based on its displayed width +* New: [wezterm.truncate_to_width](config/lua/wezterm/truncate_to_width.md) and [wezterm.truncate_left_to_width](config/lua/wezterm/truncate_left_to_width.md) function for truncating/padding a string based on its displayed width ### 20210405-110924-a5bb5be8 diff --git a/docs/config/lua/wezterm/truncate_left_to_width.md b/docs/config/lua/wezterm/truncate_left_to_width.md new file mode 100644 index 000000000..1c487eaa2 --- /dev/null +++ b/docs/config/lua/wezterm/truncate_left_to_width.md @@ -0,0 +1,15 @@ +# wezterm.truncate_left_to_width(string, max_width, min_width) + +*Since: nightly builds only* + +Returns a copy of `string` that is no longer than `max_width` columns +(as measured by [wezterm.column_width](column_width.md)), and, optionally, +no shorter than `min_width` columns, padding out with spaces. + +Truncation and padding occur on the left hand side of the string. + +For example, `wezterm.truncate_to_width("hello", 3)` returns `"llo"`, +and `wezterm.truncate_to_width("a", 10, 5)` returns " a"`. + +See also: [wezterm.truncate_to_width](truncate_to_width.md) + diff --git a/docs/config/lua/wezterm/truncate_to_width.md b/docs/config/lua/wezterm/truncate_to_width.md index 632b31f6a..a78a26502 100644 --- a/docs/config/lua/wezterm/truncate_to_width.md +++ b/docs/config/lua/wezterm/truncate_to_width.md @@ -6,6 +6,9 @@ Returns a copy of `string` that is no longer than `max_width` columns (as measured by [wezterm.column_width](column_width.md)), and, optionally, no shorter than `min_width` columns, padding out with spaces. +Truncation and padding occur on the right hand side of the string. + For example, `wezterm.truncate_to_width("hello", 3)` returns `"hel"`, and `wezterm.truncate_to_width("a", 10, 5)` returns "a "`. +See also: [wezterm.truncate_left_to_width](truncate_left_to_width.md)