feat: add set-preview-cmd action to change preview (#969)

* feat: add set-preview-cmd action to change preview

* chore: update manpage

* chore: generate completions & manpage

---------

Co-authored-by: Skim bot <skim-bot@skim-rs.github.io>
This commit is contained in:
LoricAndre 2026-02-13 09:32:13 +01:00 committed by GitHub
parent de7eab7619
commit 98a9184055
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 114 additions and 1 deletions

View file

@ -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

View file

@ -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,

View file

@ -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

View file

@ -40,6 +40,7 @@ fn parse_delimiter_value(s: &str) -> Result<Regex, String> {
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<BorderType>,
/// 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<RefCell<dyn CommandCollector>>))]
#[builder(setter(into = false))]
#[debug(skip)]
pub cmd_collector: Rc<RefCell<dyn CommandCollector>>,
/// 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<Rc<dyn Selector>>,
/// Preview Callback
///
@ -885,6 +889,7 @@ pub struct SkimOptions {
/// The function will take a `Vec<Arc<dyn SkimItem>>>` containing the currently selected items
/// and return a Vec<String> with the lines to display in UTF-8
#[cfg_attr(feature = "cli", clap(skip))]
#[debug(skip)]
pub preview_fn: Option<PreviewCallback>,
/// The internal (parsed) keymap

View file

@ -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();

View file

@ -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<Action> {
"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),

View file

@ -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;
});

View file

@ -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)

View file

@ -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)

View file

@ -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)