refactor(tui): split exit functionality into exit and cancel commands

This commit is contained in:
Orhun Parmaksız 2022-02-07 20:27:39 +03:00
parent 28593c1356
commit f3d8bf3888
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
3 changed files with 16 additions and 7 deletions

View file

@ -389,7 +389,7 @@ impl<'a> App<'a> {
}
});
}
Command::Exit => {
Command::Cancel => {
if self.input.is_some() {
self.input = None;
self.input_time = None;
@ -397,6 +397,9 @@ impl<'a> App<'a> {
self.running = false;
}
}
Command::Exit => {
self.running = false;
}
Command::Nothing => {}
}
if hide_options {

View file

@ -25,6 +25,8 @@ pub enum Command {
Copy,
/// Refresh the application.
Refresh,
/// Cancel the operation.
Cancel,
/// Exit the application.
Exit,
/// Do nothing.
@ -39,7 +41,8 @@ impl FromStr for Command {
"select" => Ok(Command::Select),
"copy" => Ok(Command::Copy),
"refresh" => Ok(Command::Refresh),
"exit" | "quit" | "q" | "q!" => Ok(Command::Exit),
"cancel" | "q" => Ok(Command::Cancel),
"exit" | "quit" | "q!" => Ok(Command::Exit),
_ => {
if s.starts_with("set") {
let mut values = s.trim_start_matches("set").trim().split_whitespace();
@ -73,7 +76,7 @@ impl Command {
Key::Delete => Command::ClearInput(true),
Key::Left => Command::MoveCursor(Direction::Left),
Key::Right => Command::MoveCursor(Direction::Right),
Key::Esc => Command::Exit,
Key::Esc => Command::Cancel,
_ => Command::Nothing,
}
} else {
@ -97,7 +100,8 @@ impl Command {
Key::Char('\n') => Command::Select,
Key::Char('c') => Command::Copy,
Key::Char('r') | Key::F(5) => Command::Refresh,
Key::Esc => Command::Exit,
Key::Esc => Command::Cancel,
Key::Ctrl('c') | Key::Ctrl('d') => Command::Exit,
_ => Command::Nothing,
}
}
@ -123,6 +127,7 @@ mod tests {
(Command::Select, "select"),
(Command::Copy, "copy"),
(Command::Refresh, "refresh"),
(Command::Cancel, "cancel"),
(Command::Exit, "exit"),
(
Command::Set(String::from("a"), String::from("b")),
@ -156,7 +161,7 @@ mod tests {
Key::Delete => Command::ClearInput(true),
Key::Left => Command::MoveCursor(Direction::Left),
Key::Right => Command::MoveCursor(Direction::Right),
Key::Esc => Command::Exit,
Key::Esc => Command::Cancel,
}
assert_command_parser! {
input_mode: false,
@ -175,7 +180,8 @@ mod tests {
Key::Char('\n') => Command::Select,
Key::Char('c') => Command::Copy,
Key::Char('r') => Command::Refresh,
Key::Esc => Command::Exit,
Key::Esc => Command::Cancel,
Key::Ctrl('c') => Command::Exit,
}
assert_eq!(Command::Nothing, Command::parse(Key::PageDown, true));
assert_eq!(Command::Nothing, Command::parse(Key::Char('#'), false));

View file

@ -162,7 +162,7 @@ fn test_render_tui() -> Result<()> {
)?;
app.run_command(Command::Search)?;
app.run_command(Command::Exit)?;
app.run_command(Command::Cancel)?;
app.run_command(Command::Copy)?;
terminal.draw(|frame| render(frame, &mut app))?;
assert_buffer(