Skip to content

Commit

Permalink
feat: add reload action
Browse files Browse the repository at this point in the history
  • Loading branch information
LoricAndre committed Nov 21, 2024
1 parent 7df8b77 commit ce90373
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions skim/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub enum Event {
EvActPreviewPageDown(i32),
EvActPreviousHistory,
EvActRedraw,
EvActReload(Option<String>),
EvActRefreshCmd,
EvActRefreshPreview,
EvActRotateMode,
Expand Down Expand Up @@ -129,6 +130,7 @@ pub fn parse_event(action: &str, arg: Option<String>) -> Option<Event> {
"previous-history" => Some(Event::EvActPreviousHistory),
"refresh-cmd" => Some(Event::EvActRefreshCmd),
"refresh-preview" => Some(Event::EvActRefreshPreview),
"reload" => Some(Event::EvActReload(arg.clone())),
"scroll-left" => Some(Event::EvActScrollLeft(arg.and_then(|s|s.parse().ok()).unwrap_or(1))),
"scroll-right" => Some(Event::EvActScrollRight(arg.and_then(|s|s.parse().ok()).unwrap_or(1))),
"select-all" => Some(Event::EvActSelectAll),
Expand Down
22 changes: 22 additions & 0 deletions skim/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,24 @@ impl Model {
let _ = Command::new(shell).arg("-c").arg(cmd).status();
}

fn act_reload(&mut self, cmd_opt: Option<String>) {
let cmd = match cmd_opt {
Some(s) => s,
None => self.query.get_cmd(),
};
debug!("command to execute: [{}]", cmd);
let mut env = ModelEnv {
cmd: cmd.to_string(),
cmd_query: self.query.get_cmd_query(),
query: self.query.get_fz_query(),
clear_selection: ClearStrategy::ClearIfNotNull,
in_query_mode: self.query.in_query_mode(),
};

self.selection.clear();
self.on_cmd_query_change(&mut env);
}

#[allow(clippy::trivial_regex)]
fn act_append_and_select(&mut self, env: &mut ModelEnv) {
let query = self.query.get_fz_query();
Expand Down Expand Up @@ -567,6 +585,10 @@ impl Model {
self.act_execute_silent(cmd);
}

Event::EvActReload(ref cmd) => {
self.act_reload(cmd.clone());
}

Event::EvActAppendAndSelect => {
self.act_append_and_select(&mut env);
}
Expand Down
4 changes: 3 additions & 1 deletion skim/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ pub struct SkimOptions {
/// preview-page-down
/// preview-page-up
/// previous-history (ctrl-p on --history or --cmd-history)
/// reload(...)
/// select-all
/// toggle
/// toggle-all
Expand All @@ -263,10 +264,11 @@ pub struct SkimOptions {
///
/// sk --bind 'ctrl-a:select-all+accept'
///
/// With execute(...) action, you can execute arbitrary commands without leaving sk. For example,
/// With execute(...) and reload(...) action, you can execute arbitrary commands without leaving sk. For example,
/// you can turn sk into a simple file browser by binding enter key to less command like follows.
///
/// sk --bind "enter:execute(less {})"
/// Note: if no argument is supplied to reload, the default command is run.
///
/// You can use the same placeholder expressions as in --preview.
///
Expand Down

0 comments on commit ce90373

Please sign in to comment.