Skip to content

Instantly share code, notes, and snippets.

@UrGuardian4ngel
Last active Feb 13, 2021
Embed
What would you like to do?
Print the colorized type of a command via bat

btype.fish

Print the colorized type of a command via bat.

This Fish function wraps the builtin type function, and colorizes its output using bat's syntax highlighting.

Installation

# Install the function
$ curl -sL https://gist.githubusercontent.com/UrGuardian4ngel/1e2aedb27f30f5595ec09b484857593f/raw/btype.fish --output $__fish_config_dir/functions/bfish.fish

# (optional) Install completions
$ curl -sL https://gist.githubusercontent.com/UrGuardian4ngel/1e2aedb27f30f5595ec09b484857593f/raw/btype.completions.fish --output $__fish_config_dir/completions/bfish.fish
complete -c btype -a "(builtin -n)" -d "Builtin"
complete -c btype -a "(functions -n)" -d "Function"
complete -c btype -a "(__fish_complete_command)" -x
function btype --argument-names name --description 'Print the colorized type of a command'
# Require a command name.
test -n "$name"; or return 1
set --local func (builtin functions --details --verbose $name)
set --local func_path $func[1]
set --local start_line $func[3]
# When the command name is not a known function, there is nothing to enhance.
if contains -- "$func_path" "-" "n/a" "stdin"
type $name
return $status
end
# Show function definition in pager.
if test $start_line -gt 1
# Workaround to fix line numbering offset in bat pager.
string join -- \n (string repeat --no-newline --count (math $start_line - 1) \n) (type $name | tail -n +3) | bat --language=fish --file-name="$func_path" --line-range="$start_line:"
else
type $name | tail -n +3 | bat --language=fish --file-name="$func_path"
end
return $status
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment