diff --git a/functions/__fzf_preview_file.fish b/functions/__fzf_preview_file.fish index 8f7247ea..275cb0b3 100644 --- a/functions/__fzf_preview_file.fish +++ b/functions/__fzf_preview_file.fish @@ -23,6 +23,6 @@ function __fzf_preview_file --argument-names file_path --description "Print a pr else if test -p "$file_path" __fzf_report_file_type "$file_path" "named pipe" else - echo "File doesn't exist." >&2 + echo "File doesn't exist or is empty." >&2 end end diff --git a/functions/__fzf_search_current_dir.fish b/functions/__fzf_search_current_dir.fish index 38f0306e..76401d36 100644 --- a/functions/__fzf_search_current_dir.fish +++ b/functions/__fzf_search_current_dir.fish @@ -5,7 +5,8 @@ function __fzf_search_current_dir --description "Search the current directory. R set fd_arguments --hidden --color=always --exclude=.git set fzf_arguments --multi --ansi - set token (commandline --current-token | string unescape) + set current_token (commandline --current-token) + set token (string unescape $current_token) # need to expand ~ in the directory name since fd can't expand it set expanded_token (string replace --regex -- "^~/" $HOME/ $token) @@ -15,7 +16,7 @@ function __fzf_search_current_dir --description "Search the current directory. R set --append fd_arguments --base-directory=$expanded_token # use the directory name as fzf's prompt to indicate the search is limited to that directory set --append fzf_arguments --prompt=$token --preview="__fzf_preview_file $token{}" - set file_paths_selected $token(fd $fd_arguments 2>/dev/null | fzf $fzf_arguments) + set file_paths_selected $expanded_token(fd $fd_arguments 2>/dev/null | fzf $fzf_arguments) else set --append fzf_arguments --query=$token --preview='__fzf_preview_file {}' set file_paths_selected (fd $fd_arguments 2>/dev/null | fzf $fzf_arguments) @@ -23,14 +24,17 @@ function __fzf_search_current_dir --description "Search the current directory. R if test $status -eq 0 - # If the user was in the middle of inputting the first token and only one path is selected, - # then prepend ./ to the selected path so that - # - if the path is an executable, the user can hit Enter one more time to immediately execute it - # - if the path is a directory, the user can hit Enter one more time to immediately cd into it (fish will - # attempt to cd implicitly if a directory name starts with a dot) - if test (count $file_paths_selected) = 1 + # Fish will implicitly take action on a path when a path is provided as the first token and it + # begins with a dot or slash. If the path is a directory, Fish will cd into it. If the path is + # an executable, Fish will execute it. To help users harness this convenient behavior, we + # automatically prepend ./ to the selected path if + # - only one path was selected, + # - the user was in the middle of inputting the first token, + # - and the path doesn't already begin with a dot or slash + # Then, the user only needs to hit Enter once more to potentially cd into or execute that path. + if test (count $file_paths_selected) = 1 \ + && not string match --quiet --regex "^[.|/]" $file_paths_selected set commandline_tokens (commandline --tokenize) - set current_token (commandline --current-token) if test "$commandline_tokens" = "$current_token" set file_paths_selected ./$file_paths_selected end diff --git a/tests/search_current_dir/no_dot_slash_if_unneeded.fish b/tests/search_current_dir/no_dot_slash_if_unneeded.fish new file mode 100644 index 00000000..3e3e1e21 --- /dev/null +++ b/tests/search_current_dir/no_dot_slash_if_unneeded.fish @@ -0,0 +1,9 @@ +# set everything up so that a path starting with / is outputted by fzf +mock commandline \* "" +mock commandline "--current-token --replace --" "echo \$argv" +mock fd \* "" +mock fzf \* "echo /Users/patrickf" + +# since there is already a /, no ./ should be prepended +set result (__fzf_search_current_dir) +@test "doesn't prepend ./ if path already starts with /" $result = "/Users/patrickf"