Skip to content

Commit

Permalink
feat: add reload action (#604)
Browse files Browse the repository at this point in the history
* feat: add reload action

* add tests for reload

---------

Co-authored-by: LoricAndre <loric.andre@pm.me>
  • Loading branch information
LoricAndre and LoricAndre authored Nov 24, 2024
1 parent 4f1b2e1 commit 4b47244
Show file tree
Hide file tree
Showing 4 changed files with 48 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
21 changes: 21 additions & 0 deletions test/test_skim.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,27 @@ def test_issue_361_literal_space(self):
# revert option back
self.tmux.send_keys(f"""set -o histexpand""", Key('Enter'))

def test_reload_no_arg(self):
args = "--bind 'ctrl-a:reload'"
sk = self.sk(args).replace('SKIM_DEFAULT_COMMAND=',
"SKIM_DEFAULT_COMMAND='echo hello'")

self.tmux.send_keys(
f"""echo -e 'a\\nb\\nc' | {sk}""", Key('Enter'))
self.tmux.until(lambda lines: lines.ready_with_matches(3))
self.tmux.send_keys(Ctrl('a'))
self.tmux.until(lambda lines: lines.ready_with_matches(1))

def test_reload_arg(self):
args = "--bind 'ctrl-a:reload(echo hello)'"
sk = self.sk(args)

self.tmux.send_keys(
f"""echo -e 'a\\nb\\nc' | {sk}""", Key('Enter'))
self.tmux.until(lambda lines: lines.ready_with_matches(3))
self.tmux.send_keys(Ctrl('a'))
self.tmux.until(lambda lines: lines.ready_with_matches(1))

def test_version(self):
self.tmux.send_keys(self.sk('--version'), Key('Enter'))
time.sleep(0.1)
Expand Down

0 comments on commit 4b47244

Please sign in to comment.