Skip to content

Commit 976ef66

Browse files
author
Stephan Dilly
committed
support showing char count (closes #466)
1 parent 8535d86 commit 976ef66

File tree

7 files changed

+43
-2
lines changed

7 files changed

+43
-2
lines changed

src/components/commit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ impl CommitComponent {
138138
key_config.clone(),
139139
"",
140140
&strings::commit_msg(&key_config),
141+
true,
141142
),
142143
key_config,
143144
}

src/components/create_branch.rs

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ impl CreateBranchComponent {
102102
key_config.clone(),
103103
&strings::create_branch_popup_title(&key_config),
104104
&strings::create_branch_popup_msg(&key_config),
105+
true,
105106
),
106107
commit_id: None,
107108
key_config,

src/components/cred.rs

+2
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ impl CredComponent {
3737
key_config.clone(),
3838
&strings::username_popup_title(&key_config),
3939
&strings::username_popup_msg(&key_config),
40+
false,
4041
)
4142
.with_input_type(InputType::Singleline),
4243
input_password: TextInputComponent::new(
4344
theme,
4445
key_config.clone(),
4546
&strings::password_popup_title(&key_config),
4647
&strings::password_popup_msg(&key_config),
48+
false,
4749
)
4850
.with_input_type(InputType::Password),
4951
key_config,

src/components/rename_branch.rs

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ impl RenameBranchComponent {
102102
key_config.clone(),
103103
&strings::rename_branch_popup_title(&key_config),
104104
&strings::rename_branch_popup_msg(&key_config),
105+
true,
105106
),
106107
branch_ref: None,
107108
key_config,

src/components/stashmsg.rs

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ impl StashMsgComponent {
136136
key_config.clone(),
137137
&strings::stash_popup_title(&key_config),
138138
&strings::stash_popup_msg(&key_config),
139+
true,
139140
),
140141
key_config,
141142
}

src/components/tag_commit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ impl TagCommitComponent {
102102
key_config.clone(),
103103
&strings::tag_commit_popup_title(&key_config),
104104
&strings::tag_commit_popup_msg(&key_config),
105+
true,
105106
),
106107
commit_id: None,
107108
key_config,

src/components/textinput.rs

+36-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ use itertools::Itertools;
1414
use std::{collections::HashMap, ops::Range};
1515
use tui::{
1616
backend::Backend,
17-
layout::Rect,
17+
layout::{Alignment, Rect},
1818
style::Modifier,
1919
text::{Spans, Text},
20-
widgets::Clear,
20+
widgets::{Clear, Paragraph},
2121
Frame,
2222
};
2323

@@ -34,6 +34,7 @@ pub struct TextInputComponent {
3434
default_msg: String,
3535
msg: String,
3636
visible: bool,
37+
show_char_count: bool,
3738
theme: SharedTheme,
3839
key_config: SharedKeyConfig,
3940
cursor_position: usize,
@@ -47,12 +48,14 @@ impl TextInputComponent {
4748
key_config: SharedKeyConfig,
4849
title: &str,
4950
default_msg: &str,
51+
show_char_count: bool,
5052
) -> Self {
5153
Self {
5254
msg: String::new(),
5355
visible: false,
5456
theme,
5557
key_config,
58+
show_char_count,
5659
title: title.to_string(),
5760
default_msg: default_msg.to_string(),
5861
cursor_position: 0,
@@ -209,6 +212,28 @@ impl TextInputComponent {
209212
_ => self.msg[range].to_owned(),
210213
}
211214
}
215+
216+
fn draw_char_count<B: Backend>(&self, f: &mut Frame<B>, r: Rect) {
217+
let count = self.msg.len();
218+
if count > 0 {
219+
let w = Paragraph::new(format!("[{} chars]", count))
220+
.alignment(Alignment::Right);
221+
222+
let mut rect = {
223+
let mut rect = r;
224+
rect.y += rect.height.saturating_sub(1);
225+
rect
226+
};
227+
228+
rect.x += 1;
229+
rect.width = rect.width.saturating_sub(2);
230+
rect.height = rect
231+
.height
232+
.saturating_sub(rect.height.saturating_sub(1));
233+
234+
f.render_widget(w, rect);
235+
}
236+
}
212237
}
213238

214239
// merges last line of `txt` with first of `append` so we do not generate unneeded newlines
@@ -269,6 +294,10 @@ impl DrawableComponent for TextInputComponent {
269294
),
270295
area,
271296
);
297+
298+
if self.show_char_count {
299+
self.draw_char_count(f, area);
300+
}
272301
}
273302

274303
Ok(())
@@ -369,6 +398,7 @@ mod tests {
369398
SharedKeyConfig::default(),
370399
"",
371400
"",
401+
false,
372402
);
373403

374404
comp.set_text(String::from("a\nb"));
@@ -389,6 +419,7 @@ mod tests {
389419
SharedKeyConfig::default(),
390420
"",
391421
"",
422+
false,
392423
);
393424
let theme = SharedTheme::default();
394425
let underlined = theme
@@ -411,6 +442,7 @@ mod tests {
411442
SharedKeyConfig::default(),
412443
"",
413444
"",
445+
false,
414446
);
415447
let theme = SharedTheme::default();
416448
let underlined_whitespace = theme
@@ -444,6 +476,7 @@ mod tests {
444476
SharedKeyConfig::default(),
445477
"",
446478
"",
479+
false,
447480
);
448481

449482
let theme = SharedTheme::default();
@@ -473,6 +506,7 @@ mod tests {
473506
SharedKeyConfig::default(),
474507
"",
475508
"",
509+
false,
476510
);
477511

478512
let theme = SharedTheme::default();

0 commit comments

Comments
 (0)