diff options
| author | Sam Lanning <sam@samlanning.com> | 2022-11-06 07:34:14 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-06 07:34:14 +0000 |
| commit | 4768b16b7409dd6ef8467037e46d60fd0c722d8c (patch) | |
| tree | 0ff908c1c27f6cd0692c468c78be0b30e804a483 /src/command | |
| parent | fix #596: Avoid drawing a command section beyond the width of the list (#597) (diff) | |
| download | atuin-4768b16b7409dd6ef8467037e46d60fd0c722d8c.zip | |
Add setting for keeping typed query when exiting (#451)
* Add option for keeping typed query on escape
fixes #422
* chore: Address duplicate if statement blocks
Diffstat (limited to '')
| -rw-r--r-- | src/command/client/search/interactive.rs | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/src/command/client/search/interactive.rs b/src/command/client/search/interactive.rs index 950a971d..36497db0 100644 --- a/src/command/client/search/interactive.rs +++ b/src/command/client/search/interactive.rs @@ -21,7 +21,7 @@ use atuin_client::{ database::Context, database::Database, history::History, - settings::{FilterMode, SearchMode, Settings}, + settings::{ExitMode, FilterMode, SearchMode, Settings}, }; use super::{ @@ -31,6 +31,9 @@ use super::{ }; use crate::VERSION; +const RETURN_ORIGINAL: usize = usize::MAX; +const RETURN_QUERY: usize = usize::MAX - 1; + struct State { history_count: i64, input: Cursor, @@ -59,9 +62,20 @@ impl State { Ok(results) } - fn handle_input(&mut self, input: &TermEvent, len: usize) -> Option<usize> { + fn handle_input( + &mut self, + settings: &Settings, + input: &TermEvent, + len: usize, + ) -> Option<usize> { match input { - TermEvent::Key(Key::Esc | Key::Ctrl('c' | 'd' | 'g')) => return Some(usize::MAX), + TermEvent::Key(Key::Ctrl('c' | 'd' | 'g')) => return Some(RETURN_ORIGINAL), + TermEvent::Key(Key::Esc) => { + return Some(match settings.exit_mode { + ExitMode::ReturnOriginal => RETURN_ORIGINAL, + ExitMode::ReturnQuery => RETURN_QUERY, + }) + } TermEvent::Key(Key::Char('\n')) => { return Some(self.results_state.selected()); } @@ -323,14 +337,14 @@ pub async fn history( // Handle input if let Event::Input(input) = events.next()? { - if let Some(i) = app.handle_input(&input, results.len()) { + if let Some(i) = app.handle_input(settings, &input, results.len()) { break 'render i; } } // After we receive input process the whole event channel before query/render. while let Ok(Event::Input(input)) = events.try_next() { - if let Some(i) = app.handle_input(&input, results.len()) { + if let Some(i) = app.handle_input(settings, &input, results.len()) { break 'render i; } } @@ -356,11 +370,12 @@ pub async fn history( if index < results.len() { // index is in bounds so we return that entry Ok(results.swap_remove(index).command) - } else if index == usize::MAX { - // index is max which implies an early exit + } else if index == RETURN_ORIGINAL { Ok(String::new()) } else { - // out of bounds usually implies no selected entry so we return the input + // Either: + // * index == RETURN_QUERY, in which case we should return the input + // * out of bounds -> usually implies no selected entry so we return the input Ok(app.input.into_inner()) } } |
