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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 7 additions & 18 deletions Library/Homebrew/completions/fish.erb
Expand Up @@ -98,28 +98,15 @@ end
######################
# These functions return lists of suggestions for arguments completion

function __fish_brew_ruby_parse_json -a file parser -d 'Parses given JSON file with Ruby'
# parser is any chain of methods to call on the parsed JSON
ruby -e "require('json'); JSON.parse(File.read('$file'))$parser"
end

function __fish_brew_suggest_formulae_all -d 'Lists all available formulae with their descriptions'
# 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
Comment on lines -107 to +102
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.

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.

end


function __fish_brew_suggest_formulae_outdated -d "List of outdated formulae with the information about potential upgrade"
brew outdated --formula --verbose \
razvanazamfirei marked this conversation as resolved.
Show resolved Hide resolved
# replace first space with tab to make the following a description in the completions list:
Expand All @@ -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)
razvanazamfirei marked this conversation as resolved.
Show resolved Hide resolved
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.

end

function __fish_brew_suggest_casks_outdated -d "Lists outdated casks with the information about potential upgrade"
Expand All @@ -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.

| string replace homebrew- "" \
| string replace (brew --repo)/Library/Taps/ ""
end

function __fish_brew_suggest_commands -d "Lists all commands names, including aliases"
Expand Down
25 changes: 7 additions & 18 deletions completions/fish/brew.fish
Expand Up @@ -85,28 +85,15 @@ end
######################
# These functions return lists of suggestions for arguments completion

function __fish_brew_ruby_parse_json -a file parser -d 'Parses given JSON file with Ruby'
# parser is any chain of methods to call on the parsed JSON
ruby -e "require('json'); JSON.parse(File.read('$file'))$parser"
end

function __fish_brew_suggest_formulae_all -d 'Lists all available formulae with their descriptions'
# 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
end

function __fish_brew_suggest_formulae_installed
brew list --formula
command ls -1 (brew --cellar)
end


function __fish_brew_suggest_formulae_outdated -d "List of outdated formulae with the information about potential upgrade"
brew outdated --formula --verbose \
# replace first space with tab to make the following a description in the completions list:
Expand All @@ -130,7 +117,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)
end

function __fish_brew_suggest_casks_outdated -d "Lists outdated casks with the information about potential upgrade"
Expand All @@ -140,7 +127,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 \
| string replace homebrew- "" \
| string replace (brew --repo)/Library/Taps/ ""
end

function __fish_brew_suggest_commands -d "Lists all commands names, including aliases"
Expand Down