@@ -83,6 +83,7 @@ impl Layout {
83
83
#[ derive( Default ) ]
84
84
struct Ui {
85
85
layouts : Vec < Layout > ,
86
+ key : Option < i32 > ,
86
87
}
87
88
88
89
impl Ui {
@@ -136,13 +137,7 @@ impl Ui {
136
137
}
137
138
138
139
// TODO(#26): Ui::edit_field does not scroll according to the cursor
139
- fn edit_field (
140
- & mut self ,
141
- buffer : & mut String ,
142
- cursor : & mut usize ,
143
- key_current : & mut Option < i32 > ,
144
- width : i32 ,
145
- ) {
140
+ fn edit_field ( & mut self , buffer : & mut String , cursor : & mut usize , width : i32 ) {
146
141
let layout = self
147
142
. layouts
148
143
. last_mut ( )
@@ -153,7 +148,7 @@ impl Ui {
153
148
* cursor = buffer. len ( ) ;
154
149
}
155
150
156
- if let Some ( key) = key_current . take ( ) {
151
+ if let Some ( key) = self . key . take ( ) {
157
152
match key {
158
153
32 ..=126 => {
159
154
if * cursor >= buffer. len ( ) {
@@ -187,7 +182,7 @@ impl Ui {
187
182
}
188
183
}
189
184
_ => {
190
- * key_current = Some ( key) ;
185
+ self . key = Some ( key) ;
191
186
}
192
187
}
193
188
}
@@ -391,7 +386,6 @@ fn main() {
391
386
let mut editing_cursor = 0 ;
392
387
393
388
let mut ui = Ui :: default ( ) ;
394
- let mut key_current = None ;
395
389
while !quit && !ctrlc:: poll ( ) {
396
390
erase ( ) ;
397
391
@@ -414,15 +408,9 @@ fn main() {
414
408
for ( index, todo) in todos. iter_mut ( ) . enumerate ( ) {
415
409
if index == todo_curr {
416
410
if editing {
417
- ui. edit_field (
418
- todo,
419
- & mut editing_cursor,
420
- & mut key_current,
421
- x / 2 ,
422
- ) ;
411
+ ui. edit_field ( todo, & mut editing_cursor, x / 2 ) ;
423
412
424
- if let Some ( '\n' ) = key_current. take ( ) . map ( |x| x as u8 as char )
425
- {
413
+ if let Some ( '\n' ) = ui. key . take ( ) . map ( |x| x as u8 as char ) {
426
414
editing = false ;
427
415
}
428
416
} else {
@@ -431,10 +419,10 @@ fn main() {
431
419
x / 2 ,
432
420
HIGHLIGHT_PAIR ,
433
421
) ;
434
- if let Some ( 'r' ) = key_current . map ( |x| x as u8 as char ) {
422
+ if let Some ( 'r' ) = ui . key . map ( |x| x as u8 as char ) {
435
423
editing = true ;
436
424
editing_cursor = todo. len ( ) ;
437
- key_current = None ;
425
+ ui . key = None ;
438
426
}
439
427
}
440
428
} else {
@@ -446,7 +434,7 @@ fn main() {
446
434
}
447
435
}
448
436
449
- if let Some ( key) = key_current . take ( ) {
437
+ if let Some ( key) = ui . key . take ( ) {
450
438
match key as u8 as char {
451
439
'K' => list_drag_up ( & mut todos, & mut todo_curr) ,
452
440
'J' => list_drag_down ( & mut todos, & mut todo_curr) ,
@@ -473,7 +461,7 @@ fn main() {
473
461
panel = panel. toggle ( ) ;
474
462
}
475
463
_ => {
476
- key_current = Some ( key) ;
464
+ ui . key = Some ( key) ;
477
465
}
478
466
}
479
467
}
@@ -493,15 +481,9 @@ fn main() {
493
481
for ( index, done) in dones. iter_mut ( ) . enumerate ( ) {
494
482
if index == done_curr {
495
483
if editing {
496
- ui. edit_field (
497
- done,
498
- & mut editing_cursor,
499
- & mut key_current,
500
- x / 2 ,
501
- ) ;
484
+ ui. edit_field ( done, & mut editing_cursor, x / 2 ) ;
502
485
503
- if let Some ( '\n' ) = key_current. take ( ) . map ( |x| x as u8 as char )
504
- {
486
+ if let Some ( '\n' ) = ui. key . take ( ) . map ( |x| x as u8 as char ) {
505
487
editing = false ;
506
488
}
507
489
} else {
@@ -510,10 +492,10 @@ fn main() {
510
492
x / 2 ,
511
493
HIGHLIGHT_PAIR ,
512
494
) ;
513
- if let Some ( 'r' ) = key_current . map ( |x| x as u8 as char ) {
495
+ if let Some ( 'r' ) = ui . key . map ( |x| x as u8 as char ) {
514
496
editing = true ;
515
497
editing_cursor = done. len ( ) ;
516
- key_current = None ;
498
+ ui . key = None ;
517
499
}
518
500
}
519
501
} else {
@@ -525,7 +507,7 @@ fn main() {
525
507
}
526
508
}
527
509
528
- if let Some ( key) = key_current . take ( ) {
510
+ if let Some ( key) = ui . key . take ( ) {
529
511
match key as u8 as char {
530
512
'K' => list_drag_up ( & mut dones, & mut done_curr) ,
531
513
'J' => list_drag_down ( & mut dones, & mut done_curr) ,
@@ -549,7 +531,7 @@ fn main() {
549
531
'\t' => {
550
532
panel = panel. toggle ( ) ;
551
533
}
552
- _ => key_current = Some ( key) ,
534
+ _ => ui . key = Some ( key) ,
553
535
}
554
536
}
555
537
} else {
@@ -565,7 +547,7 @@ fn main() {
565
547
}
566
548
ui. end ( ) ;
567
549
568
- if let Some ( 'q' ) = key_current . take ( ) . map ( |x| x as u8 as char ) {
550
+ if let Some ( 'q' ) = ui . key . take ( ) . map ( |x| x as u8 as char ) {
569
551
quit = true ;
570
552
}
571
553
@@ -574,7 +556,7 @@ fn main() {
574
556
let key = getch ( ) ;
575
557
if key != ERR {
576
558
notification. clear ( ) ;
577
- key_current = Some ( key) ;
559
+ ui . key = Some ( key) ;
578
560
}
579
561
}
580
562
0 commit comments