Skip to content

Commit 28e9f09

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 28e9f09

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/main.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ fn save_state(todos: &[String], dones: &[String], file_path: &str) {
337337
fn main() {
338338
ctrlc::init();
339339

340+
// On some old terminals ESC was used instead of ALT. So we have to tell ncurses not to wait
341+
// for another key when receiving ESC.
342+
set_escdelay(0);
343+
340344
let mut args = env::args();
341345
args.next().unwrap();
342346

@@ -410,8 +414,14 @@ fn main() {
410414
if editing {
411415
ui.edit_field(todo, &mut editing_cursor, x / 2);
412416

413-
if let Some('\n') = ui.key.take().map(|x| x as u8 as char) {
414-
editing = false;
417+
match ui.key.take().map(|x| x as u8 as char) {
418+
Some('\n') => editing = false,
419+
Some('\u{1b}') => {
420+
editing = false;
421+
list_delete(&mut todos, &mut todo_curr);
422+
break;
423+
}
424+
_ => {}
415425
}
416426
} else {
417427
ui.label_fixed_width(
@@ -483,7 +493,9 @@ fn main() {
483493
if editing {
484494
ui.edit_field(done, &mut editing_cursor, x / 2);
485495

486-
if let Some('\n') = ui.key.take().map(|x| x as u8 as char) {
496+
if let Some('\n') | Some('\u{1b}') =
497+
ui.key.take().map(|x| x as u8 as char)
498+
{
487499
editing = false;
488500
}
489501
} else {

0 commit comments

Comments
 (0)