Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use escape key to exit editing #32

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ enum Status {
Done,
}

enum EditMode {
Not,
New,
Editing,
}

impl Status {
fn toggle(&self) -> Self {
match self {
Expand Down Expand Up @@ -337,6 +343,10 @@ fn save_state(todos: &[String], dones: &[String], file_path: &str) {
fn main() {
ctrlc::init();

// On some old terminals ESC was used instead of ALT. So we have to tell ncurses not to wait
// for another key when receiving ESC.
set_escdelay(0);

let mut args = env::args();
args.next().unwrap();

Expand Down Expand Up @@ -382,7 +392,7 @@ fn main() {

let mut quit = false;
let mut panel = Status::Todo;
let mut editing = false;
let mut editing = EditMode::Not;
let mut editing_cursor = 0;

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

if let Some('\n') = ui.key.take().map(|x| x as u8 as char) {
editing = false;
match ui.key.take().map(|x| x as u8 as char) {
Some('\n') => editing = EditMode::Not,
Some('\u{1b}') => {
if let EditMode::New = editing {
list_delete(&mut todos, &mut todo_curr);
}
editing = EditMode::Not;
break;
}
_ => {}
}
} else {
ui.label_fixed_width(
Expand All @@ -420,7 +438,7 @@ fn main() {
HIGHLIGHT_PAIR,
);
if let Some('r') = ui.key.map(|x| x as u8 as char) {
editing = true;
editing = EditMode::Editing;
editing_cursor = todo.len();
ui.key = None;
}
Expand All @@ -441,7 +459,7 @@ fn main() {
'i' => {
todos.insert(todo_curr, String::new());
editing_cursor = 0;
editing = true;
editing = EditMode::New;
notification.push_str("What needs to be done?");
}
'd' => {
Expand Down Expand Up @@ -480,11 +498,13 @@ fn main() {
ui.label_fixed_width("DONE", x / 2, HIGHLIGHT_PAIR);
for (index, done) in dones.iter_mut().enumerate() {
if index == done_curr {
if editing {
if let EditMode::Editing = editing {
ui.edit_field(done, &mut editing_cursor, x / 2);

if let Some('\n') = ui.key.take().map(|x| x as u8 as char) {
editing = false;
if let Some('\n') | Some('\u{1b}') =
ui.key.take().map(|x| x as u8 as char)
{
editing = EditMode::Not;
}
} else {
ui.label_fixed_width(
Expand All @@ -493,7 +513,7 @@ fn main() {
HIGHLIGHT_PAIR,
);
if let Some('r') = ui.key.map(|x| x as u8 as char) {
editing = true;
editing = EditMode::Editing;
editing_cursor = done.len();
ui.key = None;
}
Expand Down