diff --git a/man/man1/sk.1 b/man/man1/sk.1 index 971762a9..65137710 100644 --- a/man/man1/sk.1 +++ b/man/man1/sk.1 @@ -778,6 +778,8 @@ Actions can take arguments, specified either between parentheses `reload(ls)` or .br * select\-row .br +* set\-preview\-cmd(...): *arg will be a expanded expression, see COMMAND EXPANSION for details +.br * set\-query(...): *arg will be a expanded expression, see COMMAND EXPANSION for details .br * toggle diff --git a/src/completions.rs b/src/completions.rs index 8ae4a76c..c2022a18 100644 --- a/src/completions.rs +++ b/src/completions.rs @@ -5,7 +5,7 @@ use clap::CommandFactory; use crate::SkimOptions; /// Available shells for completion generation -#[derive(Clone, clap::ValueEnum, PartialEq)] +#[derive(Clone, clap::ValueEnum, PartialEq, Debug)] pub enum Shell { /// Bourne Again SHell Bash, diff --git a/src/manpage.rs b/src/manpage.rs index ebfa2d8d..1815c2e9 100644 --- a/src/manpage.rs +++ b/src/manpage.rs @@ -165,6 +165,7 @@ const ACTIONS_SS: &str = " * reload(...) * select-all * select-row +* set-preview-cmd(...): *arg will be a expanded expression, see COMMAND EXPANSION for details * set-query(...): *arg will be a expanded expression, see COMMAND EXPANSION for details * toggle * toggle-all diff --git a/src/options.rs b/src/options.rs index dc50ebbd..55ef2385 100644 --- a/src/options.rs +++ b/src/options.rs @@ -40,6 +40,7 @@ fn parse_delimiter_value(s: &str) -> Result { feature = "cli", command(name = "sk", args_override_self = true, verbatim_doc_comment, version, about) )] +#[derive(derive_more::Debug)] pub struct SkimOptions { // --- Search --- /// Show results in reverse order @@ -470,6 +471,7 @@ pub struct SkimOptions { feature = "cli", arg(long, default_missing_value = "plain", help_heading = "Display", num_args=0..) )] + #[debug(skip)] pub border: Option, /// Wrap items in the item list @@ -867,6 +869,7 @@ pub struct SkimOptions { /// Command collector for reading items from commands #[cfg_attr(feature = "cli", clap(skip = Rc::new(RefCell::new(SkimItemReader::default())) as Rc>))] #[builder(setter(into = false))] + #[debug(skip)] pub cmd_collector: Rc>, /// Query history entries loaded from history file #[cfg_attr(feature = "cli", clap(skip))] @@ -877,6 +880,7 @@ pub struct SkimOptions { /// Selector for pre-selecting items #[cfg_attr(feature = "cli", clap(skip))] #[builder(setter(into = false))] + #[debug(skip)] pub selector: Option>, /// Preview Callback /// @@ -885,6 +889,7 @@ pub struct SkimOptions { /// The function will take a `Vec>>` containing the currently selected items /// and return a Vec with the lines to display in UTF-8 #[cfg_attr(feature = "cli", clap(skip))] + #[debug(skip)] pub preview_fn: Option, /// The internal (parsed) keymap diff --git a/src/tui/app.rs b/src/tui/app.rs index ad864242..dd26ad33 100644 --- a/src/tui/app.rs +++ b/src/tui/app.rs @@ -1044,6 +1044,10 @@ impl App { self.item_list.select(); return self.on_selection_changed(); } + SetPreviewCmd(cmd) => { + self.options.preview = Some(cmd.to_owned()); + return Ok(vec![Event::RunPreview]); + } SetQuery(value) => { self.input.value = self.expand_cmd(value, false); self.input.move_to_end(); diff --git a/src/tui/event.rs b/src/tui/event.rs index 9f0eb3dc..8c2446f9 100644 --- a/src/tui/event.rs +++ b/src/tui/event.rs @@ -190,6 +190,8 @@ pub enum Action { SelectRow(usize), /// Select current item Select, + /// Set the preview cmd and rerun preview + SetPreviewCmd(String), /// Set the query to the expanded value SetQuery(String), /// Toggle selection of current item @@ -326,6 +328,7 @@ pub fn parse_action(raw_action: &str) -> Option { "select" => Some(Select), "select-all" => Some(SelectAll), "select-row" => Some(SelectRow(arg.and_then(|s| s.parse().ok()).unwrap_or_default())), + "set-preview-cmd" => Some(SetPreviewCmd(arg.expect("set-preview-cmd action needs a value"))), "set-query" => Some(SetQuery(arg.expect("set-query action needs a value"))), "toggle" => Some(Toggle), "toggle-all" => Some(ToggleAll), diff --git a/tests/binds.rs b/tests/binds.rs index e95f8603..7882d007 100644 --- a/tests/binds.rs +++ b/tests/binds.rs @@ -97,3 +97,11 @@ insta_test!(bind_toggle_interactive_queries, @interactive, &["--bind", "ctrl-a:t @ctrl 'a'; @snap; }); + +insta_test!(bind_set_preview_cmd, ["a", "b", "c"], &["--preview", "echo initial {}", "--bind", "ctrl-a:set-preview-cmd(echo new {})"], { + @snap; + @ctrl 'a'; + @snap; + @key Up; + @snap; +}); diff --git a/tests/snapshots/binds__bind_set_preview_cmd-2.snap b/tests/snapshots/binds__bind_set_preview_cmd-2.snap new file mode 100644 index 00000000..5ed09757 --- /dev/null +++ b/tests/snapshots/binds__bind_set_preview_cmd-2.snap @@ -0,0 +1,30 @@ +--- +source: tests/binds.rs +assertion_line: 101 +expression: buf + & cursor_pos +--- +" │new a " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" c │ " +" b │ " +"> a │ " +" 3/3 0/0│ " +"> │ " +cursor: (24, 3) diff --git a/tests/snapshots/binds__bind_set_preview_cmd-3.snap b/tests/snapshots/binds__bind_set_preview_cmd-3.snap new file mode 100644 index 00000000..8dff2830 --- /dev/null +++ b/tests/snapshots/binds__bind_set_preview_cmd-3.snap @@ -0,0 +1,30 @@ +--- +source: tests/binds.rs +assertion_line: 101 +expression: buf + & cursor_pos +--- +" │new b " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" c │ " +"> b │ " +" a │ " +" 3/3 1/0│ " +"> │ " +cursor: (24, 3) diff --git a/tests/snapshots/binds__bind_set_preview_cmd.snap b/tests/snapshots/binds__bind_set_preview_cmd.snap new file mode 100644 index 00000000..1d502df0 --- /dev/null +++ b/tests/snapshots/binds__bind_set_preview_cmd.snap @@ -0,0 +1,30 @@ +--- +source: tests/binds.rs +assertion_line: 101 +expression: buf + & cursor_pos +--- +" │initial a " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" │ " +" c │ " +" b │ " +"> a │ " +" 3/3 0/0│ " +"> │ " +cursor: (24, 3)