Skip to content

Commit

Permalink
refactor(interactive search): move options to struct
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonThormeyer committed Nov 1, 2024
1 parent e38c298 commit 4fd6c45
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions litt/src/interactive_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@ use crate::InteractiveSearchInput::{*};
use crate::SearchOptions;

pub(super) struct InteractiveSearch {
state: InteractiveSearchState
state: InteractiveSearchState,
options: SearchOptions,
}

pub(super) enum InteractiveSearchState {
WaitingForInitialInput {
options: SearchOptions,
},
WaitingForInitialInput,
SearchInProgress {
search_term: String,
options: SearchOptions,
},
OpenPdf {
OpenPdf {
last_result_num: u32,
options: SearchOptions,
},
Finished,
}
Expand All @@ -41,7 +38,8 @@ pub(super) enum InteractiveSearchInput {
impl InteractiveSearch {
pub(super) fn new(options: SearchOptions) -> Self {
Self {
state: WaitingForInitialInput { options },
state: WaitingForInitialInput,
options
}
}

Expand All @@ -50,16 +48,17 @@ impl InteractiveSearch {
}

pub(super) fn display_instructions(&self, index_name: &str) {
let opts = self.options;
match &self.state {
WaitingForInitialInput { options: opts, .. } => {
WaitingForInitialInput => {
println!(
"Interactive search in \"{}\" (limit={}, distance={}; type \"#set <variable> \
<value>\" to change, \"q\" to quit, start search-term with \"~\" for \
fuzzy-search)",
index_name, opts.limit, opts.distance
);
}
SearchInProgress {options: opts, ..} | OpenPdf { options: opts, .. } => {
SearchInProgress { .. } | OpenPdf { .. } => {
println!(
"Interactive search in \"{}\" (showing results {} to {}; type \"\" for next, \
\"\" for previous {} results, \"\"|\"\" to cycle history, \"q\" to quit)",
Expand All @@ -75,16 +74,17 @@ impl InteractiveSearch {

/// Transition the interactive search state machine.
pub(super) fn state_transition(&mut self, input: &InteractiveSearchInput) {
let mut options = &mut self.options;
match (&mut self.state, input) {
// No state change when input is empty
(_, Empty) => {}
(_, Quit) => {
self.state = Finished;
}
// Open pdf/ result
(WaitingForInitialInput { options } | SearchInProgress { options, .. } | OpenPdf { options, .. }, LastSearchResult(last_number_num)) => {
(WaitingForInitialInput | SearchInProgress {..} | OpenPdf { .. }, LastSearchResult(last_number_num)) => {
self.state = OpenPdf{
last_result_num: *last_number_num, options: *options
last_result_num: *last_number_num,
}
}
// Trying to browse results without having searched; print warning and do nothing.
Expand All @@ -93,13 +93,13 @@ impl InteractiveSearch {
}
// Browsing results
(
SearchInProgress { ref mut options, .. } | OpenPdf { ref mut options, .. },
SearchInProgress { .. } | OpenPdf { .. },
BrowseForward,
) => {
options.offset += options.limit;
}
(
SearchInProgress { ref mut options, .. } | OpenPdf { ref mut options, .. },
SearchInProgress { .. } | OpenPdf { .. },
BrowseBackward,
) => {
if options.offset == 0 {
Expand All @@ -110,13 +110,9 @@ impl InteractiveSearch {
}
// Change options or fuzzy search
(
WaitingForInitialInput {
ref mut options, ..
}
| SearchInProgress {
ref mut options, ..
}
| OpenPdf { ref mut options, .. },
WaitingForInitialInput
| SearchInProgress { .. }
| OpenPdf { .. },
SearchOptionUpdate(update),
) => match update {
Limit(limit) => {
Expand All @@ -129,18 +125,16 @@ impl InteractiveSearch {
options.fuzzy = true;
self.state = SearchInProgress {
search_term: term.to_string(),
options: *options,
}
}
},
// Normal search
(
SearchInProgress { options, .. } | WaitingForInitialInput { options } | OpenPdf { options, .. },
SearchInProgress { .. } | WaitingForInitialInput | OpenPdf { .. },
SearchTerm(term),
) => {
self.state = SearchInProgress {
search_term: term.to_string(),
options: *options
search_term: term.to_string()
};
}
(Finished, _) => unreachable!(),
Expand Down

0 comments on commit 4fd6c45

Please sign in to comment.