1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-23 21:32:13 +03:00

split padding out from truncation

This makes these functions a bit more composable!

Heads up @jankatins; this is a breaking change (for the better!)

refs: https://github.com/wez/wezterm/issues/647
This commit is contained in:
Wez Furlong 2021-05-01 18:26:53 -07:00
parent 89a8e2c4fb
commit 82d825ab37
8 changed files with 113 additions and 82 deletions

View File

@ -122,9 +122,34 @@ pub fn make_lua_context(config_file: &Path) -> anyhow::Result<Lua> {
)?;
wezterm_mod.set(
"truncate_to_width",
lua.create_function(
|_, (s, max_width, min_width): (String, usize, Option<usize>)| {
"pad_right",
lua.create_function(|_, (mut result, width): (String, usize)| {
let mut len = unicode_column_width(&result);
while len < width {
result.push(' ');
len += 1;
}
Ok(result)
})?,
)?;
wezterm_mod.set(
"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;
}
Ok(result)
})?,
)?;
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) {
@ -136,25 +161,13 @@ pub fn make_lua_context(config_file: &Path) -> anyhow::Result<Lua> {
len += g_len;
}
if let Some(min_width) = min_width {
while len < min_width {
if len >= max_width {
break;
}
result.push(' ');
len += 1;
}
}
Ok(result)
},
)?,
})?,
)?;
wezterm_mod.set(
"truncate_left_to_width",
lua.create_function(
|_, (s, max_width, min_width): (String, usize, Option<usize>)| {
"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() {
@ -166,20 +179,9 @@ pub fn make_lua_context(config_file: &Path) -> anyhow::Result<Lua> {
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)?)?;

View File

@ -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)

View File

@ -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).

View File

@ -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).

View File

@ -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).

View File

@ -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)

View File

@ -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).

View File

@ -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)