From d397976acf2f3d0f0ee5ae45e6529bb7a6556108 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 16 Nov 2019 13:58:01 -0800 Subject: [PATCH] fix rendering of the cursor position in the line editor --- src/ssh.rs | 11 ++++++++--- termwiz/Cargo.toml | 2 +- termwiz/src/lineedit/host.rs | 5 +++-- termwiz/src/lineedit/mod.rs | 10 +++------- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/ssh.rs b/src/ssh.rs index c4e705d92..177635f72 100644 --- a/src/ssh.rs +++ b/src/ssh.rs @@ -44,13 +44,18 @@ fn password_prompt( // Rewrite the input so that we can obscure the password // characters when output to the terminal widget - fn highlight_line(&self, line: &str, _cursor_position: usize) -> Vec { + fn highlight_line( + &self, + line: &str, + cursor_position: usize, + ) -> (Vec, usize) { + let placeholder = "🔑"; let grapheme_count = unicode_column_width(line); let mut output = vec![]; for _ in 0..grapheme_count { - output.push(OutputElement::Text("🔑".to_string())); + output.push(OutputElement::Text(placeholder.to_string())); } - output + (output, unicode_column_width(placeholder) * cursor_position) } } match termwiztermtab::run(60, 10, move |mut term| { diff --git a/termwiz/Cargo.toml b/termwiz/Cargo.toml index 7b865c613..2bf1ed4da 100644 --- a/termwiz/Cargo.toml +++ b/termwiz/Cargo.toml @@ -1,7 +1,7 @@ [package] authors = ["Wez Furlong"] name = "termwiz" -version = "0.4.0" +version = "0.5.0" edition = "2018" repository = "https://github.com/wez/wezterm" description = "Terminal Wizardry for Unix and Windows" diff --git a/termwiz/src/lineedit/host.rs b/termwiz/src/lineedit/host.rs index 983ecb4a6..203afc135 100644 --- a/termwiz/src/lineedit/host.rs +++ b/termwiz/src/lineedit/host.rs @@ -57,8 +57,9 @@ pub trait LineEditorHost { /// The `OutputElement` type allows returning graphic attribute changes /// as well as textual output. /// The default implementation returns the line as-is with no coloring. - fn highlight_line(&self, line: &str, _cursor_position: usize) -> Vec { - vec![OutputElement::Text(line.to_owned())] + fn highlight_line(&self, line: &str, cursor_position: usize) -> (Vec, usize) { + let cursor_x_pos = crate::cell::unicode_column_width(&line[0..cursor_position]); + (vec![OutputElement::Text(line.to_owned())], cursor_x_pos) } /// Returns the history implementation diff --git a/termwiz/src/lineedit/mod.rs b/termwiz/src/lineedit/mod.rs index 32ba86a2b..fad6ac805 100644 --- a/termwiz/src/lineedit/mod.rs +++ b/termwiz/src/lineedit/mod.rs @@ -163,16 +163,12 @@ impl LineEditor { } changes.push(Change::AllAttributes(Default::default())); - let mut grapheme_count = 0; - for ele in host.highlight_line(&self.line, self.cursor) { - if let OutputElement::Text(ref t) = ele { - grapheme_count += unicode_column_width(t); - } + let (elements, cursor_x_pos) = host.highlight_line(&self.line, self.cursor); + for ele in elements { changes.push(ele.into()); } - changes.push(Change::CursorPosition { - x: Position::Absolute(prompt_width + grapheme_count), + x: Position::Absolute(prompt_width + cursor_x_pos), y: Position::NoChange, });