mirror of
https://github.com/wez/wezterm.git
synced 2024-11-22 22:42:48 +03:00
add wezterm.truncate_to_width
refs: https://github.com/wez/wezterm/issues/647
This commit is contained in:
parent
d176af9c30
commit
7e996950f6
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -700,6 +700,7 @@ dependencies = [
|
||||
"termwiz",
|
||||
"toml",
|
||||
"umask",
|
||||
"unicode-segmentation",
|
||||
"wezterm-input-types",
|
||||
"wezterm-term",
|
||||
"winapi 0.3.9",
|
||||
|
@ -36,6 +36,7 @@ terminfo = "0.7"
|
||||
termwiz = { path = "../termwiz" }
|
||||
toml = "0.5"
|
||||
umask = { path = "../umask" }
|
||||
unicode-segmentation = "1.7"
|
||||
wezterm-input-types = { path = "../wezterm-input-types" }
|
||||
wezterm-term = { path = "../term", features=["use_serde"] }
|
||||
|
||||
|
@ -7,10 +7,11 @@ use mlua::{Lua, Table, Value};
|
||||
use serde::*;
|
||||
use smol::prelude::*;
|
||||
use std::path::Path;
|
||||
use termwiz::cell::{unicode_column_width, AttributeChange, CellAttributes};
|
||||
use termwiz::cell::{grapheme_column_width, unicode_column_width, AttributeChange, CellAttributes};
|
||||
use termwiz::color::{AnsiColor, ColorAttribute, ColorSpec, RgbColor};
|
||||
use termwiz::input::Modifiers;
|
||||
use termwiz::surface::change::Change;
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
/// Set up a lua context for executing some code.
|
||||
/// The path to the directory containing the configuration is
|
||||
@ -120,6 +121,36 @@ pub fn make_lua_context(config_file: &Path) -> anyhow::Result<Lua> {
|
||||
lua.create_function(|_, s: String| Ok(unicode_column_width(&s)))?,
|
||||
)?;
|
||||
|
||||
wezterm_mod.set(
|
||||
"truncate_to_width",
|
||||
lua.create_function(
|
||||
|_, (s, max_width, min_width): (String, usize, Option<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;
|
||||
}
|
||||
|
||||
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("font", lua.create_function(font)?)?;
|
||||
wezterm_mod.set(
|
||||
"font_with_fallback",
|
||||
|
@ -46,6 +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
|
||||
|
||||
### 20210405-110924-a5bb5be8
|
||||
|
||||
|
11
docs/config/lua/wezterm/truncate_to_width.md
Normal file
11
docs/config/lua/wezterm/truncate_to_width.md
Normal file
@ -0,0 +1,11 @@
|
||||
# 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.
|
||||
|
||||
For example, `wezterm.truncate_to_width("hello", 3)` returns `"hel"`,
|
||||
and `wezterm.truncate_to_width("a", 10, 5)` returns "a "`.
|
||||
|
@ -87,13 +87,17 @@ wezterm.on("format-tab-title", function(tab, tabs, panes, config, hover, max_wid
|
||||
|
||||
local edge_foreground = background
|
||||
|
||||
-- ensure that the titles fit in the available space,
|
||||
-- and that we have room for the edges
|
||||
local title = wezterm.truncate_to_width(tab.active_pane.title, max_width-2)
|
||||
|
||||
return {
|
||||
{Background={Color=edge_background}},
|
||||
{Foreground={Color=edge_foreground}},
|
||||
{Text=SOLID_LEFT_ARROW},
|
||||
{Background={Color=background}},
|
||||
{Foreground={Color=foreground}},
|
||||
{Text=tab.active_pane.title},
|
||||
{Text=title},
|
||||
{Background={Color=edge_background}},
|
||||
{Foreground={Color=edge_foreground}},
|
||||
{Text=SOLID_RIGHT_ARROW},
|
||||
|
Loading…
Reference in New Issue
Block a user