From 5fd7987db75ec419443d563a3e3deb30b626c6fc Mon Sep 17 00:00:00 2001 From: Ninjani <48680156+Ninjani@users.noreply.github.com> Date: Wed, 27 Dec 2023 22:19:23 +0100 Subject: [PATCH] clean up code --- src/the_way/mod.rs | 35 +++++++++++++++++++------------ src/the_way/search.rs | 49 ++++++++++++++++++++++++++++++------------- 2 files changed, 57 insertions(+), 27 deletions(-) diff --git a/src/the_way/mod.rs b/src/the_way/mod.rs index 9ee6bbf..e5fd86d 100644 --- a/src/the_way/mod.rs +++ b/src/the_way/mod.rs @@ -91,7 +91,10 @@ impl TheWay { exact, stdout, force, - } => self.search(&filters, exact, search::SkimCommand::All, stdout, force), + } => self.search( + &filters, + search::SearchOptions::new(search::SkimCommand::All, exact, stdout, force), + ), TheWaySubcommand::Cp { index, filters, @@ -99,7 +102,10 @@ impl TheWay { stdout, } => match index { Some(index) => self.copy(index, stdout), - None => self.search(&filters, exact, search::SkimCommand::Copy, stdout, false), + None => self.search( + &filters, + search::SearchOptions::new(search::SkimCommand::Copy, exact, stdout, false), + ), }, TheWaySubcommand::Edit { index, @@ -107,7 +113,10 @@ impl TheWay { exact, } => match index { Some(index) => self.edit(index), - None => self.search(&filters, exact, search::SkimCommand::Edit, false, false), + None => self.search( + &filters, + search::SearchOptions::new(search::SkimCommand::Edit, exact, false, false), + ), }, TheWaySubcommand::Del { index, @@ -116,7 +125,10 @@ impl TheWay { force, } => match index { Some(index) => self.delete(index, force), - None => self.search(&filters, exact, search::SkimCommand::Delete, false, force), + None => self.search( + &filters, + search::SearchOptions::new(search::SkimCommand::Delete, exact, false, force), + ), }, TheWaySubcommand::View { index, @@ -124,7 +136,10 @@ impl TheWay { exact, } => match index { Some(index) => self.view(index), - None => self.search(&filters, exact, search::SkimCommand::View, false, false), + None => self.search( + &filters, + search::SearchOptions::new(search::SkimCommand::View, exact, false, false), + ), }, TheWaySubcommand::List { filters } => self.list(&filters, ListType::Snippet), TheWaySubcommand::Import { @@ -392,10 +407,7 @@ impl TheWay { fn search( &mut self, filters: &Filters, - exact: bool, - command: search::SkimCommand, - stdout: bool, - force: bool, + search_options: search::SearchOptions, ) -> color_eyre::Result<()> { let mut snippets = self.filter_snippets(filters)?; snippets.sort_by(|a, b| a.index.cmp(&b.index)); @@ -403,10 +415,7 @@ impl TheWay { snippets, self.highlighter.skim_theme.clone(), self.highlighter.selection_style, - exact, - command, - stdout, - force, + search_options, )?; Ok(()) } diff --git a/src/the_way/search.rs b/src/the_way/search.rs index 6a90335..ddbf778 100644 --- a/src/the_way/search.rs +++ b/src/the_way/search.rs @@ -155,6 +155,28 @@ impl SkimCommand { } } +pub(crate) struct SearchOptions { + /// Search command + command: SkimCommand, + /// Use exact search + exact: bool, + /// Use stdout + stdout: bool, + /// Force delete + force: bool, +} + +impl SearchOptions { + pub fn new(command: SkimCommand, exact: bool, stdout: bool, force: bool) -> Self { + Self { + command, + exact, + stdout, + force, + } + } +} + impl TheWay { /// Converts a list of snippets into searchable objects and opens a fuzzy search window with the /// bottom panel listing each snippet's index, description, language and tags @@ -164,10 +186,7 @@ impl TheWay { snippets: Vec, skim_theme: String, selection_style: Style, - exact: bool, - command: SkimCommand, - stdout: bool, - force: bool, + search_options: SearchOptions, ) -> color_eyre::Result<()> { let default_language = Language::default(); @@ -186,7 +205,7 @@ impl TheWay { code_fragments, selection_style, code_highlight, - exact, + exact: search_options.exact, }, text_highlight: utils::highlight_strings( &snippet.pretty_print_header(&self.highlighter, language), @@ -195,17 +214,19 @@ impl TheWay { index: snippet.index, }); } - let bind = command + let bind = search_options + .command .keys() .into_iter() .map(|s| format!("{s}:accept")) .collect::>(); let header = format!( "Press {}", - command + search_options + .command .keys() .into_iter() - .zip(command.names().into_iter()) + .zip(search_options.command.names().into_iter()) .map(|(key, name)| format!("{key} to {name}")) .collect::>() .join(", "), @@ -217,7 +238,7 @@ impl TheWay { .preview_window(Some("up:70%:wrap")) .bind(bind.iter().map(|s| s.as_ref()).collect()) .header(Some(&header)) - .exact(exact) + .exact(search_options.exact) .multi(true) .reverse(true) .color(Some(&skim_theme)) @@ -238,12 +259,12 @@ impl TheWay { .downcast_ref::() .ok_or(LostTheWay::SearchError)?; - match (command, key) { + match (search_options.command, key) { (SkimCommand::Copy, Key::Enter) => { - self.copy(snippet.index, stdout)?; + self.copy(snippet.index, search_options.stdout)?; } (SkimCommand::Delete, Key::Enter) => { - self.delete(snippet.index, force)?; + self.delete(snippet.index, search_options.force)?; } (SkimCommand::Edit, Key::Enter) => { self.edit(snippet.index)?; @@ -252,10 +273,10 @@ impl TheWay { self.view(snippet.index)?; } (SkimCommand::All, Key::Enter) => { - self.copy(snippet.index, stdout)?; + self.copy(snippet.index, search_options.stdout)?; } (SkimCommand::All, Key::ShiftLeft) => { - self.delete(snippet.index, force)?; + self.delete(snippet.index, search_options.force)?; } (SkimCommand::All, Key::ShiftRight) => { self.edit(snippet.index)?;