@@ -14,10 +14,10 @@ use itertools::Itertools;
14
14
use std:: { collections:: HashMap , ops:: Range } ;
15
15
use tui:: {
16
16
backend:: Backend ,
17
- layout:: Rect ,
17
+ layout:: { Alignment , Rect } ,
18
18
style:: Modifier ,
19
19
text:: { Spans , Text } ,
20
- widgets:: Clear ,
20
+ widgets:: { Clear , Paragraph } ,
21
21
Frame ,
22
22
} ;
23
23
@@ -34,6 +34,7 @@ pub struct TextInputComponent {
34
34
default_msg : String ,
35
35
msg : String ,
36
36
visible : bool ,
37
+ show_char_count : bool ,
37
38
theme : SharedTheme ,
38
39
key_config : SharedKeyConfig ,
39
40
cursor_position : usize ,
@@ -47,12 +48,14 @@ impl TextInputComponent {
47
48
key_config : SharedKeyConfig ,
48
49
title : & str ,
49
50
default_msg : & str ,
51
+ show_char_count : bool ,
50
52
) -> Self {
51
53
Self {
52
54
msg : String :: new ( ) ,
53
55
visible : false ,
54
56
theme,
55
57
key_config,
58
+ show_char_count,
56
59
title : title. to_string ( ) ,
57
60
default_msg : default_msg. to_string ( ) ,
58
61
cursor_position : 0 ,
@@ -209,6 +212,28 @@ impl TextInputComponent {
209
212
_ => self . msg [ range] . to_owned ( ) ,
210
213
}
211
214
}
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
+ }
212
237
}
213
238
214
239
// merges last line of `txt` with first of `append` so we do not generate unneeded newlines
@@ -269,6 +294,10 @@ impl DrawableComponent for TextInputComponent {
269
294
) ,
270
295
area,
271
296
) ;
297
+
298
+ if self . show_char_count {
299
+ self . draw_char_count ( f, area) ;
300
+ }
272
301
}
273
302
274
303
Ok ( ( ) )
@@ -369,6 +398,7 @@ mod tests {
369
398
SharedKeyConfig :: default ( ) ,
370
399
"" ,
371
400
"" ,
401
+ false ,
372
402
) ;
373
403
374
404
comp. set_text ( String :: from ( "a\n b" ) ) ;
@@ -389,6 +419,7 @@ mod tests {
389
419
SharedKeyConfig :: default ( ) ,
390
420
"" ,
391
421
"" ,
422
+ false ,
392
423
) ;
393
424
let theme = SharedTheme :: default ( ) ;
394
425
let underlined = theme
@@ -411,6 +442,7 @@ mod tests {
411
442
SharedKeyConfig :: default ( ) ,
412
443
"" ,
413
444
"" ,
445
+ false ,
414
446
) ;
415
447
let theme = SharedTheme :: default ( ) ;
416
448
let underlined_whitespace = theme
@@ -444,6 +476,7 @@ mod tests {
444
476
SharedKeyConfig :: default ( ) ,
445
477
"" ,
446
478
"" ,
479
+ false ,
447
480
) ;
448
481
449
482
let theme = SharedTheme :: default ( ) ;
@@ -473,6 +506,7 @@ mod tests {
473
506
SharedKeyConfig :: default ( ) ,
474
507
"" ,
475
508
"" ,
509
+ false ,
476
510
) ;
477
511
478
512
let theme = SharedTheme :: default ( ) ;
0 commit comments