@@ -14,10 +14,10 @@ use itertools::Itertools;
1414use std:: { collections:: HashMap , ops:: Range } ;
1515use 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\n b" ) ) ;
@@ -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