support showing char count (closes #466)

This commit is contained in:
Stephan Dilly 2020-12-27 14:11:19 +01:00
parent c0d0e31186
commit c428db09f6
7 changed files with 43 additions and 2 deletions

View File

@ -138,6 +138,7 @@ impl CommitComponent {
key_config.clone(),
"",
&strings::commit_msg(&key_config),
true,
),
key_config,
}

View File

@ -102,6 +102,7 @@ impl CreateBranchComponent {
key_config.clone(),
&strings::create_branch_popup_title(&key_config),
&strings::create_branch_popup_msg(&key_config),
true,
),
commit_id: None,
key_config,

View File

@ -37,6 +37,7 @@ impl CredComponent {
key_config.clone(),
&strings::username_popup_title(&key_config),
&strings::username_popup_msg(&key_config),
false,
)
.with_input_type(InputType::Singleline),
input_password: TextInputComponent::new(
@ -44,6 +45,7 @@ impl CredComponent {
key_config.clone(),
&strings::password_popup_title(&key_config),
&strings::password_popup_msg(&key_config),
false,
)
.with_input_type(InputType::Password),
key_config,

View File

@ -102,6 +102,7 @@ impl RenameBranchComponent {
key_config.clone(),
&strings::rename_branch_popup_title(&key_config),
&strings::rename_branch_popup_msg(&key_config),
true,
),
branch_ref: None,
key_config,

View File

@ -136,6 +136,7 @@ impl StashMsgComponent {
key_config.clone(),
&strings::stash_popup_title(&key_config),
&strings::stash_popup_msg(&key_config),
true,
),
key_config,
}

View File

@ -102,6 +102,7 @@ impl TagCommitComponent {
key_config.clone(),
&strings::tag_commit_popup_title(&key_config),
&strings::tag_commit_popup_msg(&key_config),
true,
),
commit_id: None,
key_config,

View File

@ -14,10 +14,10 @@ use itertools::Itertools;
use std::{collections::HashMap, ops::Range};
use tui::{
backend::Backend,
layout::Rect,
layout::{Alignment, Rect},
style::Modifier,
text::{Spans, Text},
widgets::Clear,
widgets::{Clear, Paragraph},
Frame,
};
@ -34,6 +34,7 @@ pub struct TextInputComponent {
default_msg: String,
msg: String,
visible: bool,
show_char_count: bool,
theme: SharedTheme,
key_config: SharedKeyConfig,
cursor_position: usize,
@ -47,12 +48,14 @@ impl TextInputComponent {
key_config: SharedKeyConfig,
title: &str,
default_msg: &str,
show_char_count: bool,
) -> Self {
Self {
msg: String::new(),
visible: false,
theme,
key_config,
show_char_count,
title: title.to_string(),
default_msg: default_msg.to_string(),
cursor_position: 0,
@ -209,6 +212,28 @@ impl TextInputComponent {
_ => self.msg[range].to_owned(),
}
}
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!("[{} chars]", count))
.alignment(Alignment::Right);
let mut rect = {
let mut rect = r;
rect.y += rect.height.saturating_sub(1);
rect
};
rect.x += 1;
rect.width = rect.width.saturating_sub(2);
rect.height = rect
.height
.saturating_sub(rect.height.saturating_sub(1));
f.render_widget(w, rect);
}
}
}
// merges last line of `txt` with first of `append` so we do not generate unneeded newlines
@ -269,6 +294,10 @@ impl DrawableComponent for TextInputComponent {
),
area,
);
if self.show_char_count {
self.draw_char_count(f, area);
}
}
Ok(())
@ -369,6 +398,7 @@ mod tests {
SharedKeyConfig::default(),
"",
"",
false,
);
comp.set_text(String::from("a\nb"));
@ -389,6 +419,7 @@ mod tests {
SharedKeyConfig::default(),
"",
"",
false,
);
let theme = SharedTheme::default();
let underlined = theme
@ -411,6 +442,7 @@ mod tests {
SharedKeyConfig::default(),
"",
"",
false,
);
let theme = SharedTheme::default();
let underlined_whitespace = theme
@ -444,6 +476,7 @@ mod tests {
SharedKeyConfig::default(),
"",
"",
false,
);
let theme = SharedTheme::default();
@ -473,6 +506,7 @@ mod tests {
SharedKeyConfig::default(),
"",
"",
false,
);
let theme = SharedTheme::default();