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

Update fish completions #15174

Merged
merged 1 commit into from Apr 12, 2023
Merged

Conversation

razvanazamfirei
Copy link
Member

@razvanazamfirei razvanazamfirei commented Apr 8, 2023

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same change?
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes? Here's an example.
  • Have you successfully run brew style with your changes locally?
  • Have you successfully run brew typecheck with your changes locally?
  • Have you successfully run brew tests with your changes locally?

Updating fish completions to remove expensive brew list calls

@razvanazamfirei razvanazamfirei marked this pull request as ready for review April 8, 2023 23:34
Copy link
Contributor

@apainintheneck apainintheneck left a comment

Choose a reason for hiding this comment

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

This generally seems to be the approach taken with the Bash completions so seems like the right approach. It'd be nice to have some benchmarks though.

__brew_complete_installed_formulae() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local installed_formulae
installed_formulae="$(command ls "${HOMEBREW_CELLAR:-$(brew --cellar)}" 2>/dev/null)"
while read -r line; do COMPREPLY+=("${line}"); done < <(compgen -W "${installed_formulae}" -- "${cur}")
}
__brew_complete_installed_casks() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local installed_casks
installed_casks="$(command ls "$(brew --caskroom)" 2>/dev/null)"
while read -r line; do COMPREPLY+=("${line}"); done < <(compgen -W "${installed_casks}" -- "${cur}")
}

Comment on lines 114 to 119
else if test -n "$HOMEBREW_NO_INSTALL_FROM_API"
command ls -1 (brew --prefix)/Library/Taps/homebrew/homebrew-core
else if test -f "$__brew_cache_path/api/formula_names.txt"
command cat $__brew_cache_path/api/formula_names.txt
else
# fallback on brew formulae if homebrew-core is not tapped
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this ignore non-core formulae after this change?

Copy link
Member

Choose a reason for hiding this comment

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

Good question.

Copy link
Member Author

Choose a reason for hiding this comment

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

It would. brew formulae is actually decently fast so no need for this.

Copy link
Member

@MikeMcQuaid MikeMcQuaid left a comment

Choose a reason for hiding this comment

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

Great work so far!

Library/Homebrew/completions/fish.erb Outdated Show resolved Hide resolved
Comment on lines 114 to 119
else if test -n "$HOMEBREW_NO_INSTALL_FROM_API"
command ls -1 (brew --prefix)/Library/Taps/homebrew/homebrew-core
else if test -f "$__brew_cache_path/api/formula_names.txt"
command cat $__brew_cache_path/api/formula_names.txt
else
# fallback on brew formulae if homebrew-core is not tapped
Copy link
Member

Choose a reason for hiding this comment

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

Good question.

end

function __fish_brew_suggest_formulae_installed
brew list --formula
command ls -1 (brew --cellar)
Copy link
Member Author

Choose a reason for hiding this comment

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

❯ hyperfine 'brew list --formula' 'command ls -1 (brew --cellar)' --shell fish
Benchmark 1: brew list --formula
  Time (mean ± σ):     473.1 ms ±  26.9 ms    [User: 248.4 ms, System: 107.8 ms]
  Range (min … max):   443.6 ms … 542.5 ms    10 runs

Benchmark 2: command ls -1 (brew --cellar)
  Time (mean ± σ):      10.0 ms ±   0.9 ms    [User: 3.4 ms, System: 4.3 ms]
  Range (min … max):     8.7 ms …  14.2 ms    112 runs

Summary
  'command ls -1 (brew --cellar)' ran
   47.16 ± 5.01 times faster than 'brew list --formula'

Copy link
Member

Choose a reason for hiding this comment

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

@razvanazamfirei Is this a strategy that could be used in bash/zsh completions too? May want to split it out into a command like brew formulae if so.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll have to check if it leads to any performance improvements since zsh caches some completions.

Copy link
Member

Choose a reason for hiding this comment

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

@razvanazamfirei Thanks! That could be a follow-up PR, doesn't need to block this one.

Library/Homebrew/completions/fish.erb Show resolved Hide resolved
Library/Homebrew/completions/fish.erb Show resolved Hide resolved
Comment on lines 148 to 150
command find (brew --prefix)/Library/Taps -mindepth 2 -maxdepth 2 -type d \
| string replace homebrew- "" \
| string replace (brew --prefix)/Library/Taps/ ""
Copy link
Member Author

Choose a reason for hiding this comment

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

❯ hyperfine 'brew tap' 'command find (brew --prefix)/Library/Taps -mindepth 2 -maxdepth 2 -type d    | string replace homebrew- "" | string replace (brew --prefix)/Library/Taps/ ""' --shell fish --warmup 1
Benchmark 1: brew tap
  Time (mean ± σ):     455.4 ms ±  11.8 ms    [User: 240.9 ms, System: 102.5 ms]
  Range (min … max):   441.5 ms … 474.4 ms    10 runs

Benchmark 2: command find (brew --prefix)/Library/Taps -mindepth 2 -maxdepth 2 -type d    | string replace homebrew- "" | string replace (brew --prefix)/Library/Taps/ ""
  Time (mean ± σ):      18.7 ms ±   1.2 ms    [User: 6.1 ms, System: 8.3 ms]
  Range (min … max):    16.7 ms …  23.0 ms    94 runs

Summary
  'command find (brew --prefix)/Library/Taps -mindepth 2 -maxdepth 2 -type d    | string replace homebrew- "" | string replace (brew --prefix)/Library/Taps/ ""' ran
   24.40 ± 1.72 times faster than 'brew tap'

Copy link
Member

Choose a reason for hiding this comment

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

@razvanazamfirei Is this a strategy that could be used in bash/zsh completions too? May want to split it out into a command like brew formulae if so.

Copy link
Contributor

Choose a reason for hiding this comment

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

The path for the find command seems to be broken for me locally when I run brew tap c and try to autocomplete.

/u/l/Homebrew (fish-completions|✔) $ brew tap cfind: /usr/local/Library/Taps: No such file or directory
find: /usr/local/Library/Taps: No such file or directory
find: /usr/local/Library/Taps: No such file or directory
find: /usr/local/Library/Taps: No such file or directory

Copy link
Contributor

Choose a reason for hiding this comment

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

I think maybe you should use brew --repo instead of brew --prefix here.

Copy link
Member Author

Choose a reason for hiding this comment

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

@apainintheneck, I made the change. Let me know if this fixes it and if you think I should add 2>/dev/null to the find command.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, this fixed it. Thanks. I'm not sure if we need to redirect stderr to /dev/null right now. It shouldn't print to stderr at all right?

@@ -104,22 +104,14 @@ function __fish_brew_ruby_parse_json -a file parser -d 'Parses given JSON file w
end
Copy link
Member Author

Choose a reason for hiding this comment

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

This function becomes redundant if changes below are made.

Library/Homebrew/completions/fish.erb Show resolved Hide resolved
end

function __fish_brew_suggest_formulae_installed
brew list --formula
command ls -1 (brew --cellar)
Copy link
Member

Choose a reason for hiding this comment

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

@razvanazamfirei Is this a strategy that could be used in bash/zsh completions too? May want to split it out into a command like brew formulae if so.

Comment on lines 148 to 150
command find (brew --prefix)/Library/Taps -mindepth 2 -maxdepth 2 -type d \
| string replace homebrew- "" \
| string replace (brew --prefix)/Library/Taps/ ""
Copy link
Member

Choose a reason for hiding this comment

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

@razvanazamfirei Is this a strategy that could be used in bash/zsh completions too? May want to split it out into a command like brew formulae if so.

docs/Manpage.md Outdated Show resolved Hide resolved
Copy link
Contributor

@apainintheneck apainintheneck left a comment

Choose a reason for hiding this comment

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

Looking good so far. The installed formulas and casks complete much faster with these changes.

Comment on lines 148 to 150
command find (brew --prefix)/Library/Taps -mindepth 2 -maxdepth 2 -type d \
| string replace homebrew- "" \
| string replace (brew --prefix)/Library/Taps/ ""
Copy link
Contributor

Choose a reason for hiding this comment

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

The path for the find command seems to be broken for me locally when I run brew tap c and try to autocomplete.

/u/l/Homebrew (fish-completions|✔) $ brew tap cfind: /usr/local/Library/Taps: No such file or directory
find: /usr/local/Library/Taps: No such file or directory
find: /usr/local/Library/Taps: No such file or directory
find: /usr/local/Library/Taps: No such file or directory

Comment on lines 148 to 150
command find (brew --prefix)/Library/Taps -mindepth 2 -maxdepth 2 -type d \
| string replace homebrew- "" \
| string replace (brew --prefix)/Library/Taps/ ""
Copy link
Contributor

Choose a reason for hiding this comment

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

I think maybe you should use brew --repo instead of brew --prefix here.

Comment on lines -107 to +102
# store the brew cache path in a var (because calling (brew --cache) is slow)
set -q __brew_cache_path
or set -gx __brew_cache_path (brew --cache)

if test -f "$__brew_cache_path/descriptions.json"
__fish_brew_ruby_parse_json "$__brew_cache_path/descriptions.json" \
'.each{ |k, v| puts([k, v].reject(&:nil?).join("\t")) }'
else
brew formulae
end
brew formulae
Copy link
Contributor

Choose a reason for hiding this comment

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

The results are different after this change because we no longer get any description related info.

Before

Screen Shot 2023-04-11 at 6 47 24 PM

After

Screen Shot 2023-04-11 at 6 48 24 PM

Not sure how important the descriptions are for most people though.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the descriptions came from $__brew_cache_path/descriptions.json. I don't have that file locally and haven't been able to figure out where it gets generated from.

Copy link
Member

Choose a reason for hiding this comment

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

Yeh, that file isn't generated by default. IMO the new behaviour makes more sense and the performance benefits are worth it.

Copy link
Member

@MikeMcQuaid MikeMcQuaid left a comment

Choose a reason for hiding this comment

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

Great work @razvanazamfirei!

Comment on lines -107 to +102
# store the brew cache path in a var (because calling (brew --cache) is slow)
set -q __brew_cache_path
or set -gx __brew_cache_path (brew --cache)

if test -f "$__brew_cache_path/descriptions.json"
__fish_brew_ruby_parse_json "$__brew_cache_path/descriptions.json" \
'.each{ |k, v| puts([k, v].reject(&:nil?).join("\t")) }'
else
brew formulae
end
brew formulae
Copy link
Member

Choose a reason for hiding this comment

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

Yeh, that file isn't generated by default. IMO the new behaviour makes more sense and the performance benefits are worth it.

end

function __fish_brew_suggest_formulae_installed
brew list --formula
command ls -1 (brew --cellar)
Copy link
Member

Choose a reason for hiding this comment

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

@razvanazamfirei Thanks! That could be a follow-up PR, doesn't need to block this one.

@@ -143,7 +130,7 @@ function __fish_brew_suggest_casks_all -d "Lists locally available casks"
end

function __fish_brew_suggest_casks_installed -d "Lists installed casks"
brew list --cask -1
command ls -1 (brew --caskroom)
Copy link
Member

Choose a reason for hiding this comment

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

Same deal here: in a future PR this would be nice to make a command implemented in Bash I think if it provides speedups to either Bash and/or ZSH.

@@ -153,7 +140,9 @@ function __fish_brew_suggest_casks_outdated -d "Lists outdated casks with the in
end

function __fish_brew_suggest_taps_installed -d "List all available taps"
brew tap
command find (brew --repo)/Library/Taps -mindepth 2 -maxdepth 2 -type d \
Copy link
Member

Choose a reason for hiding this comment

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

Same deal here: in a future PR this would be nice to make a command implemented in Bash I think if it provides speedups to either Bash and/or ZSH.

@MikeMcQuaid MikeMcQuaid merged commit 92d68e3 into Homebrew:master Apr 12, 2023
23 checks passed
@razvanazamfirei razvanazamfirei deleted the fish-completions branch April 12, 2023 15:35
@github-actions github-actions bot added the outdated PR was locked due to age label May 14, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated PR was locked due to age
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants