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

Enable completions and add description for lfcd.fish and simplify the code #1503

Merged
merged 7 commits into from
Dec 11, 2023

Conversation

postsolar
Copy link
Contributor

@postsolar postsolar commented Nov 18, 2023

A simple addition of --wraps=lf to the function definition will enable the standard lf completions to be used for lfcd function as well.

I also added a description to the function, so that when the used types lf and hits tab, a brief description of what the function does is show. I simplified the code to just bind a var to stdout instead of creating a temp file, then read this var and cd to the selected directory.

A simple addition of `--wraps=lf` to the function definition will enable the standard lf completions to be used for `lfcd` function as well.
ilyagr
ilyagr previously approved these changes Nov 18, 2023
Copy link
Collaborator

@ilyagr ilyagr left a comment

Choose a reason for hiding this comment

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

Thank you!

@ilyagr
Copy link
Collaborator

ilyagr commented Nov 18, 2023

This will only help if you manually set up fish completions for lf, I think, but it can't hurt.

@ilyagr ilyagr dismissed their stale review November 18, 2023 05:13

PR changed

@postsolar postsolar changed the title Enable completions and add description for lfcd.fish Enable completions and add description for lfcd.fish and simplify the code Nov 18, 2023
@ilyagr
Copy link
Collaborator

ilyagr commented Nov 18, 2023

Now that there are more changes, I can't review it as immediately.

@postsolar
Copy link
Contributor Author

This will only help if you manually set up fish completions for lf, I think, but it can't hurt.

Depends on the installation method. For example Arch packages include completions so they're automatically enabled on install.

Now that there are more changes, I can't review it as immediately.

Yep, no worries. Thank you.

etc/lfcd.fish Outdated Show resolved Hide resolved
etc/lfcd.fish Outdated Show resolved Hide resolved
@joelim-work
Copy link
Collaborator

I have left a couple of comments. Feel free to correct me on anything - I don't have much experience with fish.

joelim-work
joelim-work previously approved these changes Nov 18, 2023
Copy link
Collaborator

@joelim-work joelim-work left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for your contribution!

etc/lfcd.fish Outdated
end
end
end
cd (command lf -print-last-dir $argv)
Copy link
Collaborator

Choose a reason for hiding this comment

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

What would happen if lf didn't print anything to stdout, e.g. crashed? I wouldn't want to cd to the home dir in that case.

Other than that, this lgtm; I think getting an error from cd when the dir is not valid is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated it 👍

Copy link
Contributor Author

@postsolar postsolar Nov 20, 2023

Choose a reason for hiding this comment

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

Although something like lfcd --doc or lfcd -log=/dev/stdout produces errors, do you think it falls under "I think getting an error from cd when the dir is not valid is fine"?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The updated code looks better to me. Apart from not cd-ing on failure, I'm more interested in that the following does not cd you to the home dir:

🐟 set -g outdir ""
🐟 cd $outdir
cd: Empty directory '' does not exist

Alternatively, you could insert a check that $outdir is not empty. But, at that point, you could just again check whether the dir exists. I think I'm OK either way.

Thank you!


As an aside, I checked that both of the following cd you to the home dir, so the fix is useful.

cd (printf "")
cd (printf ""; false)

Copy link
Collaborator

Choose a reason for hiding this comment

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

@ilyagr Good catch about cd-ing back to the home directory in case lf crashes, I didn't consider that. Just out of curiosity, would it not be better to quote the command substitution (i.e. "$(...)"), to ensure the output is a blank string instead of an empty list?

I found the following commands result in a cd: Empty directory '' does not exist error:

cd "$(printf '')"
cd "$(false)"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ilyagr sorry, I don't follow: are you suggesting to introduce a check for whether the resulting dir variable is empty?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@postsolar I think the point @ilyagr is trying to make is that under no circumstances should lfcd change to the home directory in the case that lf crashes or otherwise prints nothing. I think a local variable is not needed and the below should work:

cd "$(command lf -print-last-dir $argv)"

Copy link
Collaborator

@ilyagr ilyagr Dec 3, 2023

Choose a reason for hiding this comment

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

@postsolar, I'm sorry I missed your question. I think @joelim-work covered what my concern was. I also believe the command he suggested should work fine. Thanks Joe!

One other thing I would add is a comment before that line, along the lines of:

The quotes cause cd to fail if lf prints nothing to stdout

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@joelim-work @ilyagr thank you for clarifying! I updated the code and added a comment

@joelim-work joelim-work dismissed their stale review November 20, 2023 03:10

Dismissing stale review (forgot out possibility of lf crashing

Copy link
Collaborator

@ilyagr ilyagr left a comment

Choose a reason for hiding this comment

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

👍

etc/lfcd.fish Outdated
end
end
function lfcd --wraps="lf" --description="lf - Terminal file manager (changing directory on exit)"
# `command` is needed in case `lfcd` is aliased to `lf`,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Minor nit: Instead of , and quotes, I'd start another sentence and write . Quotes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, looks better with a new sentence. I also now think the wording "cause cd to fail" is a bit vague (without context it might not be clear why exactly this is desired) so expanded on this bit too.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks! I do now have an even more minor nit: if we focus on not changing the directory, I'd say:

Quotes will prevent cd from changing the directory if lf prints nothing to stdout due to an error.

I also think that I mildly prefer talking about cd failing, since the user will probably see the error message in that case.

But it's pretty much OK as is, as well.

@joelim-work joelim-work merged commit e9571ab into gokcehan:master Dec 11, 2023
4 checks passed
@gokcehan gokcehan mentioned this pull request Mar 31, 2024
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.

3 participants