From 3285c08403d56d5370788ff419549ff3f1c8e66d Mon Sep 17 00:00:00 2001 From: Bjorn Neergaard Date: Fri, 7 May 2021 00:10:56 -0600 Subject: [PATCH] [Search directory] Allow customizing the file preview command (#156) To allow users to implement custom file previews (great ideas include previewing images or showing hexdumps), the command used to preview files is now configurable. To change it, set the variable fzf_preview_file_cmd. --- README.md | 16 +++++++++++++--- functions/__fzf_preview_file.fish | 9 +++++++-- tests/preview_file/custom_file_preview.fish | 6 ++++++ 3 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 tests/preview_file/custom_file_preview.fish diff --git a/README.md b/README.md index ad2b7075..2f929cdb 100644 --- a/README.md +++ b/README.md @@ -130,15 +130,25 @@ They are always appended last to fzf's argument list. Because fzf uses the optio - [re-populate fzf's input list on demand](https://github.com/junegunn/fzf/issues/1750) - change the search mode -### Change the command used to preview folders +### Change the command used to preview files -The search directory feature, by default, uses `ls` to preview the contents of a directory. To integrate with the variety of `ls` replacements available, the command used to preview directories is configurable through the `fzf_preview_dir_cmd` variable. For example, in your `config.fish`, you may put: +The search directory feature, by default, uses `bat` to preview the contents of files. `bat` is a well-adopted `cat` replacement with syntax highlighting, line numbers, and more. If you would like to change the preview tool (to `cat` to avoid installing a new dependency, or to add custom logic such as binary or image preview), you may set the `fzf_preview_file_cmd` variable. For example, in your `config.fish`, you may put: + +```fish +set fzf_preview_file_cmd cat +``` + +Do not specify a target path in the command, as `fzf.fish` will [prepend the file][custom preview command] to preview to the command itself. + +### Change the command used to preview directories + +The search directory feature, by default, uses `ls` to preview the contents of directories. To integrate with the variety of `ls` replacements available (e.g. exa, lsd, tree), the command used to preview directories is configurable through the `fzf_preview_dir_cmd` variable. Set `fzf_preview_dir_cmd` in your `config.fish`: ```fish set fzf_preview_dir_cmd exa --all --color=always ``` -Do not specify a target path in the command, as `fzf.fish` will [prepend the directory][custom preview command] to preview to the command itself. +As above, do not specify a target path in the command, as `fzf.fish` will [prepend the directory][custom preview command] to preview to the command itself. ### Change the files searched diff --git a/functions/__fzf_preview_file.fish b/functions/__fzf_preview_file.fish index 3b821573..9a83c49c 100644 --- a/functions/__fzf_preview_file.fish +++ b/functions/__fzf_preview_file.fish @@ -5,10 +5,15 @@ function __fzf_preview_file --description "Print a preview for the given file ba set file_path $argv if test -f "$file_path" # regular file - bat --style=numbers --color=always "$file_path" + if set --query fzf_preview_file_cmd + # need to escape quotes to make sure eval receives file_path as a single arg + eval $fzf_preview_file_cmd \"$file_path\" + else + bat --style=numbers --color=always "$file_path" + end else if test -d "$file_path" # directory if set --query fzf_preview_dir_cmd - # need to escape quotes to make sure eval receives file_path as a single arg + # see above eval $fzf_preview_dir_cmd \"$file_path\" else # -A list hidden files as well, except for . and .. diff --git a/tests/preview_file/custom_file_preview.fish b/tests/preview_file/custom_file_preview.fish new file mode 100644 index 00000000..15f8f74f --- /dev/null +++ b/tests/preview_file/custom_file_preview.fish @@ -0,0 +1,6 @@ +set multi_word_dir "tests/_resources/multi word dir" + +set fzf_preview_file_cmd rev + +set actual (__fzf_preview_file "$multi_word_dir/file 1.txt") +@test "correctly uses custom command to preview files" "$actual" = "1 elif si sihT"