home-config/fish/functions/_fzf_preview_file.fish
new-bootloader-next
· 1.8 KB · 43 lines
raw
| 1 | # helper function for _fzf_search_directory and _fzf_search_git_status |
| 2 | function _fzf_preview_file --description "Print a preview for the given file based on its file type." |
| 3 | # because there's no way to guarantee that _fzf_search_directory passes the path to _fzf_preview_file |
| 4 | # as one argument, we collect all the arguments into one single variable and treat that as the path |
| 5 | set -f file_path $argv |
| 6 | |
| 7 | if test -L "$file_path" # symlink |
| 8 | # notify user and recurse on the target of the symlink, which can be any of these file types |
| 9 | set -l target_path (realpath "$file_path") |
| 10 | |
| 11 | set_color yellow |
| 12 | echo "'$file_path' is a symlink to '$target_path'." |
| 13 | set_color normal |
| 14 | |
| 15 | _fzf_preview_file "$target_path" |
| 16 | else if test -f "$file_path" # regular file |
| 17 | if set --query fzf_preview_file_cmd |
| 18 | # need to escape quotes to make sure eval receives file_path as a single arg |
| 19 | eval "$fzf_preview_file_cmd '$file_path'" |
| 20 | else |
| 21 | bat --style=numbers --color=always "$file_path" |
| 22 | end |
| 23 | else if test -d "$file_path" # directory |
| 24 | if set --query fzf_preview_dir_cmd |
| 25 | # see above |
| 26 | eval "$fzf_preview_dir_cmd '$file_path'" |
| 27 | else |
| 28 | # -A list hidden files as well, except for . and .. |
| 29 | # -F helps classify files by appending symbols after the file name |
| 30 | command ls -A -F "$file_path" |
| 31 | end |
| 32 | else if test -c "$file_path" |
| 33 | _fzf_report_file_type "$file_path" "character device file" |
| 34 | else if test -b "$file_path" |
| 35 | _fzf_report_file_type "$file_path" "block device file" |
| 36 | else if test -S "$file_path" |
| 37 | _fzf_report_file_type "$file_path" socket |
| 38 | else if test -p "$file_path" |
| 39 | _fzf_report_file_type "$file_path" "named pipe" |
| 40 | else |
| 41 | echo "$file_path doesn't exist." >&2 |
| 42 | end |
| 43 | end |