Skip to content

Instantly share code, notes, and snippets.

@CognitiveDisson
Last active July 29, 2023 22:54
Show Gist options
  • Save CognitiveDisson/9e866f9c6b3016913c04005d01ac294d to your computer and use it in GitHub Desktop.
Save CognitiveDisson/9e866f9c6b3016913c04005d01ac294d to your computer and use it in GitHub Desktop.
Bazel fuzzy finder ( fzf ) with bat preview
# Example: vim (bzqfzf //...)
# Description: Fish functions (should be easily converted to zsh) for Basel fuzzy search.
# Requirements:
# bazel
# brew install fzf
# brew install bat
# Note: Put each function to separate `.fish` file.
# functions/bzqfzf.fish
function bzqfzf --description 'Fuzzy finder with preview for Bazel query'
# Perform Bazel query and pass the results to fzf with custom preview function and key binding
bazel query $argv | fzf --preview 'bat_preview_for_bazel {}' --bind 'enter:become bazel_file_from_target {}'
end
# functions/bazel_file_from_target.fish
function bazel_file_from_target
set bazel_target "$argv[1]"
set bazel_output (bazel query $bazel_target --noshow_progress --output=build | head -1 | sed -e "s/^#[[:space:]]//")
set bazel_file (echo "$bazel_output" | cut -d: -f1)
echo "$bazel_file"
end
# functions/bat_preview_for_bazel.fish
function bat_preview_for_bazel
set bazel_target "$argv[1]"
set bazel_output (bazel query $bazel_target --noshow_progress --output=build | head -1 | sed -e "s/^#[[:space:]]//")
set bazel_file (echo "$bazel_output" | cut -d: -f1)
set line (echo "$bazel_output" | cut -d: -f2)
bat --style=numbers --color=always --line-range $line: "$bazel_file"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment