Skip to content

Commit

Permalink
clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninjani committed Dec 27, 2023
1 parent 70db0c3 commit 5fd7987
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 27 deletions.
35 changes: 22 additions & 13 deletions src/the_way/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,32 @@ 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,
exact,
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,
filters,
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,
Expand All @@ -116,15 +125,21 @@ 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,
filters,
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 {
Expand Down Expand Up @@ -392,21 +407,15 @@ 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));
self.make_search(
snippets,
self.highlighter.skim_theme.clone(),
self.highlighter.selection_style,
exact,
command,
stdout,
force,
search_options,
)?;
Ok(())
}
Expand Down
49 changes: 35 additions & 14 deletions src/the_way/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -164,10 +186,7 @@ impl TheWay {
snippets: Vec<Snippet>,
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();

Expand All @@ -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),
Expand All @@ -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::<Vec<_>>();
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::<Vec<_>>()
.join(", "),
Expand All @@ -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))
Expand All @@ -238,12 +259,12 @@ impl TheWay {
.downcast_ref::<SearchSnippet>()
.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)?;
Expand All @@ -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)?;
Expand Down

0 comments on commit 5fd7987

Please sign in to comment.