2024-01-27 21:15:25 -08:00
|
|
|
#!/usr/bin/env fish
|
|
|
|
|
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2024-01-29 19:54:57 -08:00
|
|
|
function print_in_color -a text color
|
2024-01-27 21:15:25 -08:00
|
|
|
printf '%b' \
|
2024-01-28 20:33:46 -08:00
|
|
|
"$(tput setaf $color 2> /dev/null)" \
|
|
|
|
$text \
|
2024-01-27 21:15:25 -08:00
|
|
|
"$(tput sgr0 2> /dev/null)"
|
|
|
|
end
|
|
|
|
|
2024-01-29 19:54:57 -08:00
|
|
|
function print_in_red -a text
|
2024-01-28 20:33:46 -08:00
|
|
|
print_in_color $text 1
|
2024-01-27 21:15:25 -08:00
|
|
|
end
|
|
|
|
|
2024-01-29 19:54:57 -08:00
|
|
|
function print_in_yellow -a text
|
2024-01-28 20:33:46 -08:00
|
|
|
print_in_color $text 3
|
2024-01-27 21:15:25 -08:00
|
|
|
end
|
|
|
|
|
2024-01-29 19:54:57 -08:00
|
|
|
function print_in_green -a text
|
2024-01-28 20:33:46 -08:00
|
|
|
print_in_color $text 2
|
2024-01-27 21:15:25 -08:00
|
|
|
end
|
|
|
|
|
2024-01-29 19:54:57 -08:00
|
|
|
function print_in_purple -a text
|
2024-01-28 20:33:46 -08:00
|
|
|
print_in_color $text 5
|
2024-01-27 21:15:25 -08:00
|
|
|
end
|
|
|
|
|
2024-01-29 19:54:57 -08:00
|
|
|
function print_title -a text
|
2024-01-28 20:33:46 -08:00
|
|
|
print_in_purple "\n • $text\n\n"
|
2024-01-27 21:15:25 -08:00
|
|
|
end
|
|
|
|
|
2024-01-29 19:54:57 -08:00
|
|
|
function print_success -a text
|
2024-01-28 20:33:46 -08:00
|
|
|
print_in_green " [✔] $text\n"
|
2024-01-27 21:15:25 -08:00
|
|
|
end
|
|
|
|
|
2024-01-29 19:54:57 -08:00
|
|
|
function print_warning -a text
|
2024-01-28 20:33:46 -08:00
|
|
|
print_in_yellow " [!] $text\n"
|
2024-01-27 21:15:25 -08:00
|
|
|
end
|
|
|
|
|
2024-01-29 19:54:57 -08:00
|
|
|
function print_error -a text text2
|
2024-01-28 20:33:46 -08:00
|
|
|
print_in_red " [✖] $text $text2\n"
|
2024-01-27 21:15:25 -08:00
|
|
|
end
|
|
|
|
|
2024-01-29 19:54:57 -08:00
|
|
|
function print_question -a text
|
2024-01-28 20:33:46 -08:00
|
|
|
print_in_yellow " [?] $text\n"
|
2024-01-27 21:15:25 -08:00
|
|
|
end
|
|
|
|
|
2024-01-29 19:54:57 -08:00
|
|
|
function print_result -a exit_code text
|
2024-01-28 20:33:46 -08:00
|
|
|
if [ "$exit_code" -eq 0 ]
|
|
|
|
print_success $text
|
2024-01-27 21:15:25 -08:00
|
|
|
else
|
2024-01-28 20:33:46 -08:00
|
|
|
print_error $text
|
2024-01-27 21:15:25 -08:00
|
|
|
end
|
|
|
|
|
2024-01-28 20:33:46 -08:00
|
|
|
return $exit_code
|
2024-01-27 21:15:25 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
function print_error_stream
|
|
|
|
while read -r line
|
|
|
|
print_error "↳ ERROR: $line"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2024-01-29 19:54:57 -08:00
|
|
|
function show_spinner -a pid cmds msg
|
2024-01-27 21:15:25 -08:00
|
|
|
set -l frames '/-\|'
|
|
|
|
|
|
|
|
set -l number_of_frames (string length $FRAMES)
|
|
|
|
|
|
|
|
set -l i 0
|
|
|
|
set -l frame_text ""
|
|
|
|
|
|
|
|
printf "\n\n\n"
|
|
|
|
tput cuu 3
|
|
|
|
tput sc
|
|
|
|
|
|
|
|
while kill -0 "$pid" &> /dev/null
|
|
|
|
set i math $i + 1
|
|
|
|
set -l num math % $number_of_frames
|
|
|
|
set -l frame (string sub -s $num -l 1)
|
|
|
|
set frame_text " [$frame] $msg"
|
|
|
|
|
|
|
|
printf '%s' $frame_text
|
|
|
|
|
|
|
|
sleep 0.2
|
|
|
|
|
|
|
|
tput rc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2024-01-29 19:54:57 -08:00
|
|
|
function set_trap -a sig func
|
2024-01-28 20:33:46 -08:00
|
|
|
trap -p "$sig" | grep "$func" &> /dev/null \
|
|
|
|
|| trap "$func" "$sig"
|
2024-01-27 21:15:25 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
function kill_all_subproccesses
|
|
|
|
set -l i ""
|
|
|
|
|
|
|
|
for i in (jobs -p)
|
|
|
|
kill "$i"
|
|
|
|
wait "$i" &> /dev/null
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2024-01-29 19:54:57 -08:00
|
|
|
function execute -a cmds msg
|
|
|
|
if ! set -q msg
|
|
|
|
set -f msg "$cmds"
|
|
|
|
end
|
2024-01-27 21:15:25 -08:00
|
|
|
|
2024-01-29 19:54:57 -08:00
|
|
|
set -l tmp_file "$(mktemp /tmp/XXXXX)"
|
2024-01-27 21:15:25 -08:00
|
|
|
|
|
|
|
set -l exit_code 0
|
|
|
|
|
|
|
|
set_trap 'EXIT' 'kill_all_subproccesses'
|
|
|
|
|
|
|
|
eval "$cmds" &> /dev/null 2> $tmp_file &
|
|
|
|
set cmds_pid $last_pid
|
|
|
|
|
|
|
|
show_spinner $cmds_pid $cmds $msg
|
|
|
|
|
|
|
|
wait $cmds_pid &> /dev/null
|
|
|
|
set exit_code $status
|
|
|
|
|
|
|
|
print_result $exit_code $msg
|
|
|
|
|
|
|
|
if [ $exit_code -ne 0 ]
|
|
|
|
print_error_stream < $tmp_file
|
|
|
|
end
|
|
|
|
|
|
|
|
rm -rf "$tmp_file"
|
|
|
|
|
|
|
|
return $exit_code
|
|
|
|
end
|
2024-01-29 19:54:57 -08:00
|
|
|
|
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
function cmd_exists -a cmd
|
|
|
|
command -v "$cmd" &>/dev/null
|
|
|
|
end
|