Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix search file feature unnecessarily prepending ./ #119

Merged
merged 8 commits into from
Feb 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion functions/__fzf_preview_file.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out this case is hit also when the file is empty.

end
end
22 changes: 13 additions & 9 deletions functions/__fzf_search_current_dir.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -15,22 +16,25 @@ 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)
end


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
Expand Down
9 changes: 9 additions & 0 deletions tests/search_current_dir/no_dot_slash_if_unneeded.fish
Original file line number Diff line number Diff line change
@@ -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"