Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

fix(text_prompt): use unicode-width instead of char cnt #46

Merged
merged 1 commit into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ edition = "2021"
crossterm = "0.27.0"
itertools = "0.12.1"
ratatui = "0.26.2"
unicode-width = "0.1.12"

[dev-dependencies]
clap = { version = "4.5.4", features = ["derive"] }
Expand Down
20 changes: 19 additions & 1 deletion src/text_prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ratatui::{
prelude::*,
widgets::{Block, Paragraph, StatefulWidget, Widget},
};
use unicode_width::UnicodeWidthStr;

// TODO style the widget
// TODO style each element of the widget.
Expand Down Expand Up @@ -100,7 +101,7 @@ impl<'a> StatefulWidget for TextPrompt<'a> {
" › ".cyan().dim(),
Span::raw(value),
]);
let prompt_length = line.to_string().chars().count() - value_length;
let prompt_length = line.to_string().width() - value_length;
let lines = wrap(line, width).take(height).collect_vec();

// constrain the position to the area
Expand Down Expand Up @@ -247,6 +248,23 @@ mod tests {
expected.set_style(Rect::new(2, 0, 6, 1), Style::new().bold());
expected.set_style(Rect::new(8, 0, 3, 1), Style::new().cyan().dim());
assert_buffer_eq!(buffer, expected);
assert_eq!(state.cursor(), (11, 0));
}

#[test]
fn render_emoji() {
let prompt = TextPrompt::from("🔍");
let mut state = TextState::new();
let mut buffer = Buffer::empty(Rect::new(0, 0, 11, 1));

prompt.render(buffer.area, &mut buffer, &mut state);

let mut expected = Buffer::with_lines(vec!["? 🔍 › "]);
expected.set_style(Rect::new(0, 0, 1, 1), PENDING_STYLE);
expected.set_style(Rect::new(2, 0, 1, 1), Style::new().bold());
expected.set_style(Rect::new(4, 0, 3, 1), Style::new().cyan().dim());
assert_buffer_eq!(buffer, expected);
assert_eq!(state.cursor(), (7, 0));
}

#[test]
Expand Down
Loading