1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-25 06:12:16 +03:00

fix rendering of the cursor position in the line editor

This commit is contained in:
Wez Furlong 2019-11-16 13:58:01 -08:00
parent 6289c08a4e
commit d397976acf
4 changed files with 15 additions and 13 deletions

View File

@ -44,13 +44,18 @@ fn password_prompt(
// Rewrite the input so that we can obscure the password // Rewrite the input so that we can obscure the password
// characters when output to the terminal widget // characters when output to the terminal widget
fn highlight_line(&self, line: &str, _cursor_position: usize) -> Vec<OutputElement> { fn highlight_line(
&self,
line: &str,
cursor_position: usize,
) -> (Vec<OutputElement>, usize) {
let placeholder = "🔑";
let grapheme_count = unicode_column_width(line); let grapheme_count = unicode_column_width(line);
let mut output = vec![]; let mut output = vec![];
for _ in 0..grapheme_count { 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| { match termwiztermtab::run(60, 10, move |mut term| {

View File

@ -1,7 +1,7 @@
[package] [package]
authors = ["Wez Furlong"] authors = ["Wez Furlong"]
name = "termwiz" name = "termwiz"
version = "0.4.0" version = "0.5.0"
edition = "2018" edition = "2018"
repository = "https://github.com/wez/wezterm" repository = "https://github.com/wez/wezterm"
description = "Terminal Wizardry for Unix and Windows" description = "Terminal Wizardry for Unix and Windows"

View File

@ -57,8 +57,9 @@ pub trait LineEditorHost {
/// The `OutputElement` type allows returning graphic attribute changes /// The `OutputElement` type allows returning graphic attribute changes
/// as well as textual output. /// as well as textual output.
/// The default implementation returns the line as-is with no coloring. /// The default implementation returns the line as-is with no coloring.
fn highlight_line(&self, line: &str, _cursor_position: usize) -> Vec<OutputElement> { fn highlight_line(&self, line: &str, cursor_position: usize) -> (Vec<OutputElement>, usize) {
vec![OutputElement::Text(line.to_owned())] 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 /// Returns the history implementation

View File

@ -163,16 +163,12 @@ impl<T: Terminal> LineEditor<T> {
} }
changes.push(Change::AllAttributes(Default::default())); changes.push(Change::AllAttributes(Default::default()));
let mut grapheme_count = 0; let (elements, cursor_x_pos) = host.highlight_line(&self.line, self.cursor);
for ele in host.highlight_line(&self.line, self.cursor) { for ele in elements {
if let OutputElement::Text(ref t) = ele {
grapheme_count += unicode_column_width(t);
}
changes.push(ele.into()); changes.push(ele.into());
} }
changes.push(Change::CursorPosition { changes.push(Change::CursorPosition {
x: Position::Absolute(prompt_width + grapheme_count), x: Position::Absolute(prompt_width + cursor_x_pos),
y: Position::NoChange, y: Position::NoChange,
}); });