dotfiles/script/utils.fish

175 lines
3.4 KiB
Fish
Raw Normal View History

#!/usr/bin/env fish
2024-01-30 19:49:07 -08:00
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function print_in_color -a text color
2024-02-03 12:05:02 -08:00
printf '%b' \
"$(tput setaf $color 2> /dev/null)" \
$text \
"$(tput sgr0 2> /dev/null)"
end
function print_in_red -a text
2024-02-03 12:05:02 -08:00
print_in_color $text 1
end
function print_in_yellow -a text
2024-02-03 12:05:02 -08:00
print_in_color $text 3
end
function print_in_green -a text
2024-02-03 12:05:02 -08:00
print_in_color $text 2
end
2024-01-30 19:49:07 -08:00
function print_in_cyan -a text
2024-02-03 12:05:02 -08:00
print_in_color $text 6
2024-01-30 19:49:07 -08:00
end
function print_in_purple -a text
2024-02-03 12:05:02 -08:00
print_in_color $text 5
end
function print_title -a text
2024-02-03 12:05:02 -08:00
print_in_purple "\n 󰣐 $text\n\n"
2024-01-30 19:49:07 -08:00
end
function print_subtitle -a text
2024-02-03 12:05:02 -08:00
print_in_cyan "\n  $text\n"
end
function print_success -a text
2024-02-03 12:05:02 -08:00
print_in_green " [✔] $text\n"
end
function print_warning -a text
2024-02-03 12:05:02 -08:00
print_in_yellow " [!] $text\n"
end
function print_error -a text text2
2024-02-03 12:05:02 -08:00
print_in_red " [✖] $text $text2\n"
end
function print_question -a text
2024-02-03 12:05:02 -08:00
print_in_yellow " [?] $text\n"
end
function print_result -a exit_code text
2024-02-03 12:05:02 -08:00
if [ "$exit_code" -eq 0 ]
print_success $text
else
print_error $text
end
2024-02-03 12:05:02 -08:00
return $exit_code
end
function print_error_stream
while read -l line
2024-02-03 12:05:02 -08:00
print_error "↳ ERROR: $line"
end
end
2024-01-30 19:49:07 -08:00
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function show_spinner -a pid cmds msg
set -f frames '/-\|'
set -f number_of_frames (string length $frames)
set -f i 0
set -f frame_text ""
2024-02-03 12:05:02 -08:00
printf "\n\n\n"
tput cuu 3
tput sc
2024-02-03 12:05:02 -08:00
while kill -0 "$pid" &>/dev/null
set i (math "$i + 1")
set -l num (math "($i % $number_of_frames) + 1")
set -l frame (string sub -s $num -l 1 $frames)
set frame_text " [$frame] $msg"
2024-02-03 12:05:02 -08:00
printf '%s' $frame_text
2024-02-03 12:05:02 -08:00
sleep 0.2
2024-02-03 12:05:02 -08:00
tput rc
end
end
2024-01-30 19:49:07 -08:00
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function set_trap -a sig func
2024-02-03 12:05:02 -08:00
trap -p "$sig" | grep "$func" &>/dev/null \
|| trap "$func" "$sig"
end
function kill_all_subproccesses
2024-02-03 12:05:02 -08:00
set -l i ""
2024-02-03 12:05:02 -08:00
for i in (jobs -p)
kill "$i"
wait "$i" &>/dev/null
end
end
2024-01-30 19:49:07 -08:00
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function execute -a cmds msg
2024-02-03 12:05:02 -08:00
if ! set -q msg
set -f msg "$cmds"
end
2024-02-03 12:05:02 -08:00
set -l tmp_file "$(mktemp /tmp/XXXXX)"
set -g exit_code 0
2024-02-03 12:05:02 -08:00
set_trap EXIT kill_all_subproccesses
fish -c "$cmds" >/dev/null 2>$tmp_file &
set cmds_pid (jobs -lp)
function _exited_$cmds_pid --on-process-exit $cmds_pid -S
set -g exit_code $argv[3]
functions -e _exited_$cmds_pid
end
2024-02-03 12:05:02 -08:00
show_spinner $cmds_pid $cmds $msg
2024-02-03 12:05:02 -08:00
wait $cmds_pid &>/dev/null
2024-02-03 12:05:02 -08:00
print_result $exit_code $msg
2024-02-03 12:05:02 -08:00
if [ $exit_code -ne 0 ]
print_error_stream <$tmp_file
end
2024-02-03 12:05:02 -08:00
rm -rf "$tmp_file"
2024-02-03 12:05:02 -08:00
return $exit_code
end
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function cmd_exists -a cmd
2024-02-03 12:05:02 -08:00
command -v "$cmd" &>/dev/null
end
2024-02-04 12:49:24 -08:00
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function ask_for_sudo
sudo -v &>/dev/null
# Update existing 'sudo' timestamp until this script has finished.
#
# https://gist.github.com/cowboy/3118588
while true
sudo -n true
sleep 60
kill -0 "$fish_pid" || exit
end &>/dev/null &
end