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

Allow quick open with pre-populated query and non-directories #76

Merged
merged 15 commits into from
Jan 13, 2021
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Use `fzf.fish` to interactively find and insert into the command line:
- **Key binding and mnemonic:** <kbd>Ctrl</kbd>+<kbd>F</kbd> (`F` for file)
- **Preview window:** file with syntax highlighting, directory contents, or file type
- **Remarks**
- appends `./` to the selection if the selection is a single directory, allowing for quick cd into that directory (see [cd docs][])
- prepends `./` to the selection if only one selection is made, allowing for quickly execute a program or cd into a directory (see [cd docs][])
- ignores files that are also ignored by git
- <kbd>Tab</kbd> to multi-select

Expand Down
16 changes: 12 additions & 4 deletions functions/__fzf_search_current_dir.fish
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ function __fzf_search_current_dir --description "Search the current directory us
)

if test $status -eq 0
# If this function was triggered with an empty commandline and the only thing selected is a directory, then
# prepend ./ to the dir path. Because fish will attempt to cd implicitly if a directory name starting with a dot
# is provided, this allows the user to hit Enter one more time to quickly cd into the selected directory.
if test (count (commandline --tokenize)) = 0 && test (count $file_paths_selected) = 1 && test -d $file_paths_selected
# If this function was triggered when the user is inputing the first token and only one path is selected,
# then prepend ./ to the selected path.
# If the path is an executable, the user can hit Enter one more time to execute it.
# If the path is a directory, the user can hit Enter one more time to quickly cd into it, because fish will
# attempt to cd implicitly if a directory name starts with a dot.
set first_token_length (string length (commandline --tokenize)[1])

if test -z "$first_token_length"
set first_token_length 0
end

if test (string length (commandline)) = $first_token_length && test (count $file_paths_selected) = 1
Copy link
Owner

Choose a reason for hiding this comment

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

Can you reverse the order of the if statement for performance reasons?

Copy link
Owner

Choose a reason for hiding this comment

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

Also, can't you just test (count (commandline --tokenize) = 1) to tell if the user only inputted one token?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you reverse the order of the if statement for performance reasons?

As you wish, but two builtins vs one builtin doesn't make any difference.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

just test (count (commandline --tokenize) = 1) to tell if the user only inputted one token

  • This doesn't allow it being triggered with an empty command line
  • <command> | (| indicates cursor position) is considered one token by commandline --tokenize, which is not desirable.

Copy link
Owner

@PatrickF1 PatrickF1 Jan 10, 2021

Choose a reason for hiding this comment

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

Oh good point, forgot commandline was a builtin too.

I still find the logic convoluted though. If all we want to know is if the user is still typing the first token, can't we just compare string trim (commandline) to string trim (commandline --current-token)?

Copy link
Owner

Choose a reason for hiding this comment

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

Or even test (commandline --tokenize) = (commandline --current-token)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

test (commandline --tokenize) = (commandline --current-token)

Brilliant! We can even drop --tokenize.

Copy link
Owner

Choose a reason for hiding this comment

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

Actually, we should keep tokenize for situations where the user accidentally included a whitespace in the front or back for whatever reason but still has the cursor on the current token. I'll add it in and test.

Copy link
Owner

Choose a reason for hiding this comment

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

Oh darn, now it doesn't work if the input is empty. Hmm...

Copy link
Owner

Choose a reason for hiding this comment

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

Oh it's because if the commandline is empty, then (commandline --tokenize) is empty and test doesn't work

test: Missing argument at index 2

~/.config/fish/functions/__fzf_search_current_dir.fish (line 29):
        if test (count $file_paths_selected) = 1 && test ""(commandline --tokenize) = ""(commandline --current-token)
                                                    ^
in function '__fzf_search_current_dir'

set file_paths_selected ./$file_paths_selected
end

Expand Down