diff --git a/config/src/lua.rs b/config/src/lua.rs index 87457365d..c1d620d6f 100644 --- a/config/src/lua.rs +++ b/config/src/lua.rs @@ -122,64 +122,66 @@ pub fn make_lua_context(config_file: &Path) -> anyhow::Result { )?; wezterm_mod.set( - "truncate_to_width", - lua.create_function( - |_, (s, max_width, min_width): (String, usize, Option)| { - let mut result = String::new(); - let mut len = 0; - for g in s.graphemes(true) { - let g_len = grapheme_column_width(g); - if g_len + len > max_width { - break; - } - result.push_str(g); - len += g_len; - } + "pad_right", + lua.create_function(|_, (mut result, width): (String, usize)| { + let mut len = unicode_column_width(&result); + while len < width { + result.push(' '); + len += 1; + } - if let Some(min_width) = min_width { - while len < min_width { - if len >= max_width { - break; - } - result.push(' '); - len += 1; - } - } - - Ok(result) - }, - )?, + Ok(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; - } + "pad_left", + lua.create_function(|_, (mut result, width): (String, usize)| { + let mut len = unicode_column_width(&result); + while len < width { + result.insert(0, ' '); + len += 1; + } - if let Some(min_width) = min_width { - while len < min_width { - if len >= max_width { - break; - } - result.push(" "); - len += 1; - } - } + Ok(result) + })?, + )?; - result.reverse(); - Ok(result.join("")) - }, - )?, + wezterm_mod.set( + "truncate_right", + lua.create_function(|_, (s, max_width): (String, usize)| { + let mut result = String::new(); + let mut len = 0; + for g in s.graphemes(true) { + let g_len = grapheme_column_width(g); + if g_len + len > max_width { + break; + } + result.push_str(g); + len += g_len; + } + + Ok(result) + })?, + )?; + + wezterm_mod.set( + "truncate_left", + lua.create_function(|_, (s, max_width): (String, usize)| { + 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; + } + + result.reverse(); + Ok(result.join("")) + })?, )?; wezterm_mod.set("font", lua.create_function(font)?)?; diff --git a/docs/changelog.md b/docs/changelog.md index 0b65550ef..5962622f5 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) 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 +* New: [wezterm.pad_left](config/lua/wezterm/pad_left.md), [wwezterm.pad_right](config/lua/wezterm/pad_right.md), [wezterm.truncate_left](config/lua/wezterm/truncate_left.md) and [wezterm.truncate_right](config/lua/wezterm/truncate_right.md) function for truncating/padding a string based on its displayed width * Updated bundled `Noto Color Emoji` font to version 2.020 with unicode 13.1 support. Thanks to [@4cm4k1](https://github.com/4cm4k1)! [#742](https://github.com/wez/wezterm/pull/742) * Fixed: Numpad Enter reported as CTRL-C on macOS [#739](https://github.com/wez/wezterm/issues/739) * Fixed: mouse reporting gbutton state not cleared when focus is lost, eg: from clicking a link [#744](https://github.com/wez/wezterm/issues/744) diff --git a/docs/config/lua/wezterm/pad_left.md b/docs/config/lua/wezterm/pad_left.md new file mode 100644 index 000000000..40f48e37c --- /dev/null +++ b/docs/config/lua/wezterm/pad_left.md @@ -0,0 +1,15 @@ +# wezterm.pad_left(string, min_width) + +*Since: nightly builds only* + +Returns a copy of `string` that is at leasst `min_width` columns +(as measured by [wezterm.column_width](column_width.md)). + +If the string is shorter than `min_width`, spaces are added to +the left end of the string. + +For example, `wezterm.pad_left("o", 3)` returns `" o"`. + +See also: [wezterm.truncate_left](truncate_left.md), [wezterm.pad_right](pad_right.md). + + diff --git a/docs/config/lua/wezterm/pad_right.md b/docs/config/lua/wezterm/pad_right.md new file mode 100644 index 000000000..2ccdf92a1 --- /dev/null +++ b/docs/config/lua/wezterm/pad_right.md @@ -0,0 +1,16 @@ +# wezterm.pad_right(string, min_width) + +*Since: nightly builds only* + +Returns a copy of `string` that is at leasst `min_width` columns +(as measured by [wezterm.column_width](column_width.md)). + +If the string is shorter than `min_width`, spaces are added to +the right end of the string. + +For example, `wezterm.pad_righ("o", 3)` returns `"o "`. + +See also: [wezterm.truncate_left](truncate_left.md), [wezterm.pad_left](pad_left.md). + + + diff --git a/docs/config/lua/wezterm/truncate_left.md b/docs/config/lua/wezterm/truncate_left.md new file mode 100644 index 000000000..cdf688624 --- /dev/null +++ b/docs/config/lua/wezterm/truncate_left.md @@ -0,0 +1,14 @@ +# wezterm.truncate_left(string, max_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)). + +Truncation occurs by removing excess characters from the left +end of the string. + +For example, `wezterm.truncate_left("hello", 3)` returns `"llo"`. + +See also: [wezterm.truncate_right](truncate_right.md), [wezterm.pad_right](pad_right.md). + diff --git a/docs/config/lua/wezterm/truncate_left_to_width.md b/docs/config/lua/wezterm/truncate_left_to_width.md deleted file mode 100644 index 1c487eaa2..000000000 --- a/docs/config/lua/wezterm/truncate_left_to_width.md +++ /dev/null @@ -1,15 +0,0 @@ -# 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_right.md b/docs/config/lua/wezterm/truncate_right.md new file mode 100644 index 000000000..f7cfaa510 --- /dev/null +++ b/docs/config/lua/wezterm/truncate_right.md @@ -0,0 +1,13 @@ +# wezterm.truncate_right(string, max_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)). + +Truncation occurs by reemoving excess characters from the right end +of the string. + +For example, `wezterm.truncate_right("hello", 3)` returns `"hel"`, + +See also: [wezterm.truncate_left](truncate_left.md), [wezterm.pad_left](pad_left.md). diff --git a/docs/config/lua/wezterm/truncate_to_width.md b/docs/config/lua/wezterm/truncate_to_width.md deleted file mode 100644 index a78a26502..000000000 --- a/docs/config/lua/wezterm/truncate_to_width.md +++ /dev/null @@ -1,14 +0,0 @@ -# wezterm.truncate_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 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)