Skip to content

Commit

Permalink
fix char_count with utf8 chars (fixes #1726)
Browse files Browse the repository at this point in the history
  • Loading branch information
extrawurst committed Jun 20, 2023
1 parent e90e8dc commit 022b389
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
* support 'n'/'p' key to move to the next/prev hunk in diff component [[@hamflx](https://github.com/hamflx)] ([#1523](https://github.com/extrawurst/gitui/issues/1523))

### Fixes
* fix commit dialog char count for multibyte characters ([#1726](https://github.com/extrawurst/gitui/issues/1726))

## [0.23.0] - 2022-06-19

**reset to commit**
Expand Down
24 changes: 20 additions & 4 deletions src/components/textinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use ratatui::{
Frame,
};
use std::{cell::Cell, collections::HashMap, ops::Range};
use unicode_segmentation::UnicodeSegmentation;

#[derive(PartialEq, Eq)]
pub enum InputType {
Expand All @@ -43,6 +44,7 @@ pub struct TextInputComponent {
input_type: InputType,
current_area: Cell<Rect>,
embed: bool,
char_count: usize,
}

impl TextInputComponent {
Expand All @@ -66,6 +68,7 @@ impl TextInputComponent {
input_type: InputType::Multiline,
current_area: Cell::new(Rect::default()),
embed: false,
char_count: 0,
}
}

Expand All @@ -80,6 +83,7 @@ impl TextInputComponent {
/// Clear the `msg`.
pub fn clear(&mut self) {
self.msg.clear();
self.update_count();
self.cursor_position = 0;
}

Expand Down Expand Up @@ -203,13 +207,15 @@ impl TextInputComponent {
if self.cursor_position > 0 {
self.decr_cursor();
self.msg.remove(self.cursor_position);
self.update_count();
}
}

/// Set the `msg`.
pub fn set_text(&mut self, msg: String) {
self.msg = msg;
self.cursor_position = 0;
self.update_count();
}

/// Set the `title`.
Expand Down Expand Up @@ -303,10 +309,12 @@ impl TextInputComponent {
}

fn draw_char_count<B: Backend>(&self, f: &mut Frame<B>, r: Rect) {
let count = self.msg.len();
if count > 0 {
let w = Paragraph::new(format!("[{count} chars]"))
.alignment(Alignment::Right);
if self.char_count > 0 {
let w = Paragraph::new(format!(
"[{} chars]",
self.char_count
))
.alignment(Alignment::Right);

let mut rect = {
let mut rect = r;
Expand All @@ -323,6 +331,10 @@ impl TextInputComponent {
f.render_widget(w, rect);
}
}

fn update_count(&mut self) {
self.char_count = self.msg.graphemes(true).count();
}
}

// merges last line of `txt` with first of `append` so we do not generate unneeded newlines
Expand Down Expand Up @@ -432,6 +444,7 @@ impl Component for TextInputComponent {
match e.code {
KeyCode::Char(c) if !is_ctrl => {
self.msg.insert(self.cursor_position, c);
self.update_count();
self.incr_cursor();
return Ok(EventState::Consumed);
}
Expand All @@ -441,6 +454,7 @@ impl Component for TextInputComponent {
self.cursor_position..pos,
"",
);
self.update_count();
}
return Ok(EventState::Consumed);
}
Expand All @@ -455,6 +469,7 @@ impl Component for TextInputComponent {
"",
);
self.cursor_position = pos;
self.update_count();
}
return Ok(EventState::Consumed);
}
Expand All @@ -475,6 +490,7 @@ impl Component for TextInputComponent {
KeyCode::Delete => {
if self.cursor_position < self.msg.len() {
self.msg.remove(self.cursor_position);
self.update_count();
}
return Ok(EventState::Consumed);
}
Expand Down

0 comments on commit 022b389

Please sign in to comment.