Skip to content

Commit 49d6628

Browse files
committed
Use escape key to exit editing
The escape key can now be used to exit edit mode. In writing a todo, this will also cancel the new todo.
1 parent 557de9d commit 49d6628

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

src/main.rs

+30-10
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ enum Status {
223223
Done,
224224
}
225225

226+
enum EditMode {
227+
Not,
228+
New,
229+
Editing,
230+
}
231+
226232
impl Status {
227233
fn toggle(&self) -> Self {
228234
match self {
@@ -337,6 +343,10 @@ fn save_state(todos: &[String], dones: &[String], file_path: &str) {
337343
fn main() {
338344
ctrlc::init();
339345

346+
// On some old terminals ESC was used instead of ALT. So we have to tell ncurses not to wait
347+
// for another key when receiving ESC.
348+
set_escdelay(0);
349+
340350
let mut args = env::args();
341351
args.next().unwrap();
342352

@@ -382,7 +392,7 @@ fn main() {
382392

383393
let mut quit = false;
384394
let mut panel = Status::Todo;
385-
let mut editing = false;
395+
let mut editing = EditMode::Not;
386396
let mut editing_cursor = 0;
387397

388398
let mut ui = Ui::default();
@@ -407,11 +417,19 @@ fn main() {
407417
// TODO(#27): the item lists don't have a scroll area
408418
for (index, todo) in todos.iter_mut().enumerate() {
409419
if index == todo_curr {
410-
if editing {
420+
if let EditMode::Editing | EditMode::New = editing {
411421
ui.edit_field(todo, &mut editing_cursor, x / 2);
412422

413-
if let Some('\n') = ui.key.take().map(|x| x as u8 as char) {
414-
editing = false;
423+
match ui.key.take().map(|x| x as u8 as char) {
424+
Some('\n') => editing = EditMode::Not,
425+
Some('\u{1b}') => {
426+
if let EditMode::New = editing {
427+
list_delete(&mut todos, &mut todo_curr);
428+
}
429+
editing = EditMode::Not;
430+
break;
431+
}
432+
_ => {}
415433
}
416434
} else {
417435
ui.label_fixed_width(
@@ -420,7 +438,7 @@ fn main() {
420438
HIGHLIGHT_PAIR,
421439
);
422440
if let Some('r') = ui.key.map(|x| x as u8 as char) {
423-
editing = true;
441+
editing = EditMode::Editing;
424442
editing_cursor = todo.len();
425443
ui.key = None;
426444
}
@@ -441,7 +459,7 @@ fn main() {
441459
'i' => {
442460
todos.insert(todo_curr, String::new());
443461
editing_cursor = 0;
444-
editing = true;
462+
editing = EditMode::New;
445463
notification.push_str("What needs to be done?");
446464
}
447465
'd' => {
@@ -480,11 +498,13 @@ fn main() {
480498
ui.label_fixed_width("DONE", x / 2, HIGHLIGHT_PAIR);
481499
for (index, done) in dones.iter_mut().enumerate() {
482500
if index == done_curr {
483-
if editing {
501+
if let EditMode::Editing = editing {
484502
ui.edit_field(done, &mut editing_cursor, x / 2);
485503

486-
if let Some('\n') = ui.key.take().map(|x| x as u8 as char) {
487-
editing = false;
504+
if let Some('\n') | Some('\u{1b}') =
505+
ui.key.take().map(|x| x as u8 as char)
506+
{
507+
editing = EditMode::Not;
488508
}
489509
} else {
490510
ui.label_fixed_width(
@@ -493,7 +513,7 @@ fn main() {
493513
HIGHLIGHT_PAIR,
494514
);
495515
if let Some('r') = ui.key.map(|x| x as u8 as char) {
496-
editing = true;
516+
editing = EditMode::Editing;
497517
editing_cursor = done.len();
498518
ui.key = None;
499519
}

0 commit comments

Comments
 (0)