From 976ef664eed7e6a6b840edd6d915584b0eb913cb Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Sun, 27 Dec 2020 14:11:19 +0100 Subject: [PATCH] support showing char count (closes #466) --- src/components/commit.rs | 1 + src/components/create_branch.rs | 1 + src/components/cred.rs | 2 ++ src/components/rename_branch.rs | 1 + src/components/stashmsg.rs | 1 + src/components/tag_commit.rs | 1 + src/components/textinput.rs | 38 +++++++++++++++++++++++++++++++-- 7 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/components/commit.rs b/src/components/commit.rs index 708aeb1c40f..13a09f38beb 100644 --- a/src/components/commit.rs +++ b/src/components/commit.rs @@ -138,6 +138,7 @@ impl CommitComponent { key_config.clone(), "", &strings::commit_msg(&key_config), + true, ), key_config, } diff --git a/src/components/create_branch.rs b/src/components/create_branch.rs index d10e4f64876..88f7bd48f67 100644 --- a/src/components/create_branch.rs +++ b/src/components/create_branch.rs @@ -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, diff --git a/src/components/cred.rs b/src/components/cred.rs index 8f0d526aa6d..13525e05597 100644 --- a/src/components/cred.rs +++ b/src/components/cred.rs @@ -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, diff --git a/src/components/rename_branch.rs b/src/components/rename_branch.rs index 81a9cda78b1..3a943604a86 100644 --- a/src/components/rename_branch.rs +++ b/src/components/rename_branch.rs @@ -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, diff --git a/src/components/stashmsg.rs b/src/components/stashmsg.rs index 8b3e01ff7ec..4959124b9c2 100644 --- a/src/components/stashmsg.rs +++ b/src/components/stashmsg.rs @@ -136,6 +136,7 @@ impl StashMsgComponent { key_config.clone(), &strings::stash_popup_title(&key_config), &strings::stash_popup_msg(&key_config), + true, ), key_config, } diff --git a/src/components/tag_commit.rs b/src/components/tag_commit.rs index 063871b3939..a41bd4de216 100644 --- a/src/components/tag_commit.rs +++ b/src/components/tag_commit.rs @@ -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, diff --git a/src/components/textinput.rs b/src/components/textinput.rs index a0c471d08df..58044f8b0f6 100644 --- a/src/components/textinput.rs +++ b/src/components/textinput.rs @@ -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(&self, f: &mut Frame, 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();