Skip to content

Commit 557de9d

Browse files
authoredJun 28, 2021
Merge pull request #30 from tsoding/ui-key
Move current key into ui
2 parents 32d1a51 + 0a751e9 commit 557de9d

File tree

1 file changed

+18
-36
lines changed

1 file changed

+18
-36
lines changed
 

‎src/main.rs

+18-36
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ impl Layout {
8383
#[derive(Default)]
8484
struct Ui {
8585
layouts: Vec<Layout>,
86+
key: Option<i32>,
8687
}
8788

8889
impl Ui {
@@ -136,13 +137,7 @@ impl Ui {
136137
}
137138

138139
// 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) {
146141
let layout = self
147142
.layouts
148143
.last_mut()
@@ -153,7 +148,7 @@ impl Ui {
153148
*cursor = buffer.len();
154149
}
155150

156-
if let Some(key) = key_current.take() {
151+
if let Some(key) = self.key.take() {
157152
match key {
158153
32..=126 => {
159154
if *cursor >= buffer.len() {
@@ -187,7 +182,7 @@ impl Ui {
187182
}
188183
}
189184
_ => {
190-
*key_current = Some(key);
185+
self.key = Some(key);
191186
}
192187
}
193188
}
@@ -391,7 +386,6 @@ fn main() {
391386
let mut editing_cursor = 0;
392387

393388
let mut ui = Ui::default();
394-
let mut key_current = None;
395389
while !quit && !ctrlc::poll() {
396390
erase();
397391

@@ -414,15 +408,9 @@ fn main() {
414408
for (index, todo) in todos.iter_mut().enumerate() {
415409
if index == todo_curr {
416410
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);
423412

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) {
426414
editing = false;
427415
}
428416
} else {
@@ -431,10 +419,10 @@ fn main() {
431419
x / 2,
432420
HIGHLIGHT_PAIR,
433421
);
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) {
435423
editing = true;
436424
editing_cursor = todo.len();
437-
key_current = None;
425+
ui.key = None;
438426
}
439427
}
440428
} else {
@@ -446,7 +434,7 @@ fn main() {
446434
}
447435
}
448436

449-
if let Some(key) = key_current.take() {
437+
if let Some(key) = ui.key.take() {
450438
match key as u8 as char {
451439
'K' => list_drag_up(&mut todos, &mut todo_curr),
452440
'J' => list_drag_down(&mut todos, &mut todo_curr),
@@ -473,7 +461,7 @@ fn main() {
473461
panel = panel.toggle();
474462
}
475463
_ => {
476-
key_current = Some(key);
464+
ui.key = Some(key);
477465
}
478466
}
479467
}
@@ -493,15 +481,9 @@ fn main() {
493481
for (index, done) in dones.iter_mut().enumerate() {
494482
if index == done_curr {
495483
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);
502485

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) {
505487
editing = false;
506488
}
507489
} else {
@@ -510,10 +492,10 @@ fn main() {
510492
x / 2,
511493
HIGHLIGHT_PAIR,
512494
);
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) {
514496
editing = true;
515497
editing_cursor = done.len();
516-
key_current = None;
498+
ui.key = None;
517499
}
518500
}
519501
} else {
@@ -525,7 +507,7 @@ fn main() {
525507
}
526508
}
527509

528-
if let Some(key) = key_current.take() {
510+
if let Some(key) = ui.key.take() {
529511
match key as u8 as char {
530512
'K' => list_drag_up(&mut dones, &mut done_curr),
531513
'J' => list_drag_down(&mut dones, &mut done_curr),
@@ -549,7 +531,7 @@ fn main() {
549531
'\t' => {
550532
panel = panel.toggle();
551533
}
552-
_ => key_current = Some(key),
534+
_ => ui.key = Some(key),
553535
}
554536
}
555537
} else {
@@ -565,7 +547,7 @@ fn main() {
565547
}
566548
ui.end();
567549

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) {
569551
quit = true;
570552
}
571553

@@ -574,7 +556,7 @@ fn main() {
574556
let key = getch();
575557
if key != ERR {
576558
notification.clear();
577-
key_current = Some(key);
559+
ui.key = Some(key);
578560
}
579561
}
580562

0 commit comments

Comments
 (0)
Please sign in to comment.