Initial commit - v1.0

This commit is contained in:
Manuele Sarfatti 2025-05-09 20:01:43 +02:00
commit 76d136f64c
20 changed files with 1350 additions and 0 deletions

38
src/02.ui/check.sh Normal file
View file

@ -0,0 +1,38 @@
#! /usr/bin/env bash
# check.sh - Print a success message
# @depends on:
# - pen.sh
# - movements.sh
# - _symbols.sh
# Print a checkmark with a message, and stop and replace the
# spinner if it's running (relies on the spinner being the last
# thing printed)
#
# Usage:
# check [options] text
# Options:
# [same as pen.sh]
# Examples:
# check "Success, world!"
# check bold "Success, world!"
# --or--
# spin "Installing dependencies..."
# sleep 2
# check "Dependancies installed."
check() {
# If there is a spinner running, stop it and clear the line
if spinning; then
spop
up
bol
cl
fi
pen -n green "${_mark:-} "
pen "$@"
}
# Export the check function so it can be used in other scripts
export -f check

25
src/02.ui/repen.sh Normal file
View file

@ -0,0 +1,25 @@
#! /usr/bin/env bash
# repen.sh - Overwrite the previous line with new text
# @depends on:
# - pen.sh
# - movements.sh
# Move up one line, move to the beginning, clear the line, and print the text.
#
# Usage:
# repen [options] text
# Options:
# [same as pen.sh]
# Examples:
# repen "Hello, world!"
# repen bold "Hello, world!"
# --or--
# repen "Hello, world!"
repen() {
upclear
pen "$@"
}
# Export the repen function so it can be used in other scripts
export -f repen

97
src/02.ui/spin.sh Normal file
View file

@ -0,0 +1,97 @@
#! /usr/bin/env bash
# spin.sh - Print a spinner with a message
# @depends on:
# - pen.sh
# - movements.sh
# - _symbols.sh
# Make sure the cursor is shown and the spinner stopped if the script exits abnormally
trap spop EXIT INT TERM
# Module state variables
_spinner_frame_duration=0.1
_spinner_pid=""
# Print a message with a spinner at the beginning
#
# Usage:
# spin [options] text
# Options:
# [same as pen.sh]
# Examples:
# spin "Installing dependencies..."
# sleep 2
# spop
# pen "Let's do something else now..."
# --or, better--
# spin "Installing dependencies..."
# sleep 2
# check "Dependancies installed."
spin() {
local message=("$@")
_spinner="${_spinner:-⣷⣯⣟⡿⢿⣻⣽⣾}"
# If there is already a spinner running, stop it
if spinning; then
spop --keep-cursor-hidden
fi
# Run the spinner in the background
(
hide_cursor
# Use a trap to catch USR1 signal for clean shutdown
trap "show_cursor; exit 0" USR1
# Print the first frame of the spinner
pen -n cyan "${_spinner:0:1} "
pen "${message[@]}"
while true; do
for ((i = 0; i < ${#_spinner}; i++)); do
frame="${_spinner:$i:1}"
up
bol
pen -n cyan "${frame} "
pen "${message[@]}"
sleep $_spinner_frame_duration
done
done
) &
_spinner_pid=$!
}
# Stop the spinner
spop() {
local keep_cursor_hidden=false
[[ "$1" == "--keep-cursor-hidden" ]] && keep_cursor_hidden=true
if spinning; then
# Signal spinner to exit gracefully
kill -USR1 "${_spinner_pid}" 2>/dev/null
# Wait briefly for cleanup
sleep $_spinner_frame_duration
# Ensure it's really gone
if ps -p "${_spinner_pid}" >/dev/null 2>&1; then
kill "${_spinner_pid}" 2>/dev/null
# Manually clean up display, unless asked not to do so
if [[ "$keep_cursor_hidden" == false ]]; then
show_cursor
fi
fi
_spinner_pid=""
fi
}
# Check if a spinner is running
spinning() {
[[ -n "${_spinner_pid}" ]]
}
# Export the functions so they are available to subshells
export -f spin spop spinning

38
src/02.ui/throw.sh Normal file
View file

@ -0,0 +1,38 @@
#! /usr/bin/env bash
# throw.sh - Print an throw message
# @depends on:
# - pen.sh
# - movements.sh
# - _symbols.sh
# Print an throwmark with a message, and stop and replace the
# spinner if it's running (relies on the spinner being the last
# thing printed)
#
# Usage:
# throw [options] text
# Options:
# [same as pen.sh]
# Examples:
# throw "Failed, world!"
# throw bold "Failed, world!"
# --or--
# spin "Installing dependencies..."
# sleep 2
# throw "Did you forget to feed the cat?"
throw() {
# If there is a spinner running, stop it and clear the line
if spinning; then
spop
up
bol
cl
fi
pen -n red "${_cross:-} "
pen "$@"
}
# Export the throw function so it can be used in other scripts
export -f throw

38
src/02.ui/warn.sh Normal file
View file

@ -0,0 +1,38 @@
#! /usr/bin/env bash
# warn.sh - Print a warning message
# @depends on:
# - pen.sh
# - movements.sh
# - _symbols.sh
# Print a "!" with a message, and stop and replace the
# spinner if it's running (relies on the spinner being
# the last thing printed)
#
# Usage:
# warn [options] text
# Options:
# [same as pen.sh]
# Examples:
# warn "Failed, world!"
# warn bold "Failed, world!"
# --or--
# spin "Installing dependencies..."
# sleep 2
# warn "Did you forget to feed the cat?"
warn() {
# If there is a spinner running, stop it and clear the line
if spinning; then
spop
up
bol
cl
fi
pen -n yellow bold italic "${_warn:-!} "
pen italic "$@"
}
# Export the warn function so it can be used in other scripts
export -f warn