Created
May 1, 2025 16:13
-
-
Save lynsei/4dc2541a47e05a870e7f5bd378d97533 to your computer and use it in GitHub Desktop.
[fish-log] logging function with colors and accepting unlimited variadic key-value combinations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# multi-file copy to paths in the CSV file | |
# very useful if you are updating multiple paths from a source location often, especially useful if the source location changes or if there are many | |
function .copy.back -d "Multi-copy a file to template paths" -a source | |
# Multi-copy to locations listed in .targets.multi-copy.csv | |
source $PWD/scripts/.log.fish | |
# set the possible colors that alternate under the file copy list | |
set -a possible_colors blue | |
set -a possible_colors cyan | |
set -a possible_colors blue | |
set -a possible_colors cyan | |
set indx 1 | |
while read line | |
cp -rf $PWD/$source $line/$source | |
.log -c $possible_colors[$indx] -k Notice: -k "File Source:" -k "Target:" -v "Copied" -v $source -v $line/$source | |
set indx (math $indx + 1) | |
end < $PWD/scripts/.targets.multi-copy.csv | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function .log --description 'Logs: Pretty Print' | |
# force true color support | |
set -g fish_term24bit 1 | |
function .usage -d "Usage" | |
set -l usage_str "\ | |
Logging: Pretty Print Colors | |
True color logs in your terminal, made easier by Lyns. | |
Note: This function accepts -k/-v Key Value pair or a variadic argument for key/value. | |
@flags: -c/--color Color of Text | |
-b/--background Background Color | |
-d/--dim Dimming | |
-i/--italics Italics | |
-o/--bold Bold | |
-u/--underline Underline | |
-r/--reverse Reverse | |
@values: -v/--value | |
-k/--key | |
@required: -c/--color | |
-v/--value | |
" | |
echo $usage_str | |
end | |
# setup options | |
set -l keys (fish_opt -s k -l key --multiple-vals) | |
set -l vals (fish_opt -s v -l value --multiple-vals) | |
# setup variadic arguments | |
argparse $keys $vals 'c/color=' 'b/background=' 'd/dim' 'i/italics' 'o/bold' 'u/underline' 'r/reverse' -- $argv | |
set_color --background blue yellow; | |
set_color normal; | |
# required: check color not exists | |
if test -z "$_flag_c" | |
set_color --background black red; echo "-c/color required!"; | |
set_color -b black --dim --reverse grey; .usage | |
return; | |
end | |
# required: check value or multiple values not exist | |
if test -z "$_flag_v" | |
set_color --background black red; echo "-v/--value required!"; | |
set_color --underline --dim --background normal grey; .usage | |
return; | |
end | |
set_color normal; | |
# optional: | |
# dim | |
if set -q _flag_d | |
set -a flags "-d" | |
end | |
# italics | |
if set -q _flag_i | |
set -a flags "-i" | |
end | |
# bold | |
if set -q _flag_o | |
set -a flags "-o" | |
end | |
# underline | |
if set -q _flag_u | |
set -a flags "-u" | |
end | |
# reverse | |
if set -q _flag_r | |
set -a flags "-r" | |
end | |
# if values are set, output them with keys | |
if set -q _flag_v | |
# value count | |
set -l val_count (count $_flag_v) | |
set -l key_count (count $_flag_k) | |
if test [[ $val_count > 1 ]] | |
if test [[ $val_count == $key_count ]] | |
set -l counts_match true | |
else | |
set -l counts_match false | |
end | |
else | |
set -l no_counts true | |
end | |
# default count starting point | |
set -l i "1" | |
set flag_str (string collect -- $flags) | |
# combine flag_str and background/color | |
if set -q _flag_b | |
set set_str "--background" "$_flag_b" "$flag_str" "$_flag_c" | |
else | |
set set_str "$flag_str" "$_flag_c" | |
end | |
# standard set_val is all flags and the color: | |
set -l set_val (string collect -- $set_str) | |
set -l all_params "$set_val" | |
# if keys are set output them alongside values | |
if set -q _flag_k | |
# only one value | |
if test -n "$no_counts" | |
eval "set_color $all_params" | |
echo "$_flag_k: $_flag_v" | |
end | |
if test -n $counts_match | |
for k in $_flag_k | |
eval "set_color $all_params" | |
echo "$k: $_flag_v[$i]" | |
set i (math $i + 1) | |
end | |
end | |
else | |
# NO KEYS, VALS ONLY | |
for v in $_flag_v | |
eval "set_color $all_params" | |
echo $_flag_v[$i] | |
set i (math $i +1) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment