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
Merged

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

merged 15 commits into from
Jan 13, 2021

Conversation

kidonng
Copy link
Contributor

@kidonng kidonng commented Jan 7, 2021

This PR improves both #72 and #73.

@PatrickF1
Copy link
Owner

This makes the feature more consistent. The user doesn't have to remember that they had to select a folder to make it work, now they only needs to know "If I only made one selection, I can quickly open it!".

Not quite. Well, as you said it only works with executables. What about for text files which is probably the most common use case? I'm indifferent though, will probably still merge this after some more thought.

@kidonng
Copy link
Contributor Author

kidonng commented Jan 8, 2021

Well, as you said it only works with executables. What about for text files which is probably the most common use case?

That's why we are lifting the limit while still being generic: we don't prepend $EDITOR or something like that.

My example of executables may not be good, but I hope you can get what I mean. It's literally just, if you only selected one path, we prepend ./ for you, which you can make use of. There may be more use cases though.

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'

@PatrickF1
Copy link
Owner

@kidonng want me to finish this for you?

# attempt to cd implicitly if a directory name starts with a dot)
if test (count $file_paths_selected) = 1
set commandline_tokens (commandline --tokenize)
set current_token (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.

@kidonng please LMK if you have a better solution to command substitution resulting in an empty string. Seems fish doesn't allow you to do "(commandline --tokenize)" so I have to use variables.

Copy link
Owner

Choose a reason for hiding this comment

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

After further research I think this is the only way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you have to use a variable (which IMO is not bad), see https://fishshell.com/docs/current/index.html#cartesian-products.

@PatrickF1 PatrickF1 merged commit 5e15aeb into PatrickF1:main Jan 13, 2021
@PatrickF1 PatrickF1 deleted the cd-with-query branch January 13, 2021 18:02
@PatrickF1 PatrickF1 changed the title Allow quick open with pre-populated query Allow quick open with pre-populated query and non-directories Jan 13, 2021
@kidonng
Copy link
Contributor Author

kidonng commented Jan 14, 2021

Thanks for your work on this! ✨

@PatrickF1
Copy link
Owner

Thanks for getting it started @kidonng!

hrshtst pushed a commit to hrshtst/fzf.fish that referenced this pull request Apr 22, 2021
…kF1#76)

Make the quick cd behavior trigger even if the user was starting to type a token and even if the path is not a directory. Before, it would only prepend ./ if the command line was empty and the selected path is a directory.

Now, 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants