Skip to content

Commit

Permalink
support showing char count (closes #466)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Dilly committed May 27, 2021
1 parent 8535d86 commit 976ef66
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/components/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ impl CommitComponent {
key_config.clone(),
"",
&strings::commit_msg(&key_config),
true,
),
key_config,
}
Expand Down
1 change: 1 addition & 0 deletions src/components/create_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/components/cred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ 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(
theme,
key_config.clone(),
&strings::password_popup_title(&key_config),
&strings::password_popup_msg(&key_config),
false,
)
.with_input_type(InputType::Password),
key_config,
Expand Down
1 change: 1 addition & 0 deletions src/components/rename_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/components/stashmsg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ impl StashMsgComponent {
key_config.clone(),
&strings::stash_popup_title(&key_config),
&strings::stash_popup_msg(&key_config),
true,
),
key_config,
}
Expand Down
1 change: 1 addition & 0 deletions src/components/tag_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
38 changes: 36 additions & 2 deletions src/components/textinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -269,6 +294,10 @@ impl DrawableComponent for TextInputComponent {
),
area,
);

if self.show_char_count {
self.draw_char_count(f, area);
}
}

Ok(())
Expand Down Expand Up @@ -369,6 +398,7 @@ mod tests {
SharedKeyConfig::default(),
"",
"",
false,
);

comp.set_text(String::from("a\nb"));
Expand All @@ -389,6 +419,7 @@ mod tests {
SharedKeyConfig::default(),
"",
"",
false,
);
let theme = SharedTheme::default();
let underlined = theme
Expand All @@ -411,6 +442,7 @@ mod tests {
SharedKeyConfig::default(),
"",
"",
false,
);
let theme = SharedTheme::default();
let underlined_whitespace = theme
Expand Down Expand Up @@ -444,6 +476,7 @@ mod tests {
SharedKeyConfig::default(),
"",
"",
false,
);

let theme = SharedTheme::default();
Expand Down Expand Up @@ -473,6 +506,7 @@ mod tests {
SharedKeyConfig::default(),
"",
"",
false,
);

let theme = SharedTheme::default();
Expand Down

0 comments on commit 976ef66

Please sign in to comment.