Skip to content

Instantly share code, notes, and snippets.

@Tedfulk
Created March 11, 2024 19:43
Show Gist options
  • Save Tedfulk/04ba18187bf53925dabfae301108ba10 to your computer and use it in GitHub Desktop.
Save Tedfulk/04ba18187bf53925dabfae301108ba10 to your computer and use it in GitHub Desktop.
formatting and coloring my alias output
function format_aliases
# Define colors
set cyan (set_color cyan)
set yellow (set_color yellow)
set blue (set_color blue)
set reset_color (set_color normal)
# Find the longest alias name for alignment
set -l longest 0
for line in (alias)
set -l current (string length (string match -r "alias ([^ ]+)" $line)[2])
if test $current -gt $longest
set longest $current
end
end
# The total length will be longest alias name + 5 spaces
set longest (math $longest + 5)
for line in (alias)
# Extract the alias name and command
set -l alias_name (string match -r "alias ([^ ]+)" $line)[2]
set -l command (string match -r "'(.*)'" $line)[2]
# Check if command is not captured, which happens with commands starting with __
if test -z "$command"
set command (string match -r "alias [^ ]+ (.*)" $line)[2]
end
# Determine command color based on prefix
set -l command_color $yellow
if string match -q "__*" $command
set command_color $blue
end
# Calculate padding
set -l padding_length (math $longest - (string length $alias_name))
set -l padding (string repeat -n $padding_length " ")
# Output alias name in cyan with appropriate padding and command in either yellow or blue
echo "$cyan$alias_name$padding$reset_color$command_color$command$reset_color"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment