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

62
src/01.core/pen.sh Normal file
View file

@ -0,0 +1,62 @@
#! /usr/bin/env bash
# pen.sh - Print pretty text
# @depends on:
# - _symbols.sh
# Print text with ANSI color codes and text formatting
#
# Usage:
# pen [options] text
# Options:
# -n: No newline after text
# bold: Bold text
# italic: Italic text
# underline: Underline text
# black|red|green|yellow|blue|purple|cyan|white|grey|gray: Color text
# [0-9]: ANSI 256 color number
# *: Any other text is printed as is
# Examples:
# pen "Hello, world!"
# pen -n "Hello, world!"
# pen bold "Hello, world!"
# pen italic blue "Hello, world!"
# pen -n 219 underline "Hello, world!"
pen() {
local new_line="\n"
local text="${*: -1}" # Get the last argument as the text
local args=("${@:1:$#-1}") # Get all arguments except the last one
local format_code=""
local reset_code="\033[0m"
for arg in "${args[@]}"; do
arg=${arg,,} # Convert to lowercase
case "$arg" in
-n) new_line="" ;;
bold) format_code+="\033[1m" ;;
italic) format_code+="\033[3m" ;;
underline) format_code+="\033[4m" ;;
black) format_code+="\033[30m" ;;
red) format_code+="\033[31m" ;;
green) format_code+="\033[32m" ;;
yellow) format_code+="\033[33m" ;;
blue) format_code+="\033[34m" ;;
purple) format_code+="\033[35m" ;;
cyan) format_code+="\033[36m" ;;
white) format_code+="\033[37m" ;;
grey | gray) format_code+="\033[90m" ;;
[0-9]*)
# Check if this is a valid ANSI 256 color number
if [[ "$arg" =~ ^[0-9]+$ ]] && [ "$arg" -ge 0 ] && [ "$arg" -le 255 ]; then
format_code+="\033[38;5;${arg}m"
fi
;;
*) ;; # Ignore invalid arguments
esac
done
printf "%b%s%b%b" "${format_code}" "${text}" "${reset_code}" "${new_line}"
}
# Export the pen function so it's available to subshells
export -f pen

59
src/01.core/run.sh Normal file
View file

@ -0,0 +1,59 @@
#! /usr/bin/env bash
# run.sh - Execute commands with output/error capture
# Execute a command with stdout and stderr capture capabilities
#
# Usage:
# run --out output_var --err error_var command [args...]
# run command [args...]
# Examples:
# run --out output --err error echo "Hello, world!"
# pen "You said: $output"
run() {
local outvar_name errvar_name
local -n outvar errvar # Declare namerefs (will be assigned below if needed)
local cmd
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--out)
outvar_name="$2"
shift 2
;;
--err)
errvar_name="$2"
shift 2
;;
*)
cmd=("$@")
break
;;
esac
done
# Set up namerefs if variable names are provided
[[ -n "${outvar_name}" ]] && local -n outvar="${outvar_name}"
[[ -n "${errvar_name}" ]] && local -n errvar="${errvar_name}"
# Temporary files for capture
local stdout_file stderr_file
stdout_file=$(mktemp)
stderr_file=$(mktemp)
# Execute command with redirection
"${cmd[@]}" >"${stdout_file}" 2>"${stderr_file}"
local exit_code=$?
# Assign outputs if requested
# shellcheck disable=SC2034
[[ -n "${outvar_name}" ]] && outvar="$(<"$stdout_file")"
# shellcheck disable=SC2034
[[ -n "${errvar_name}" ]] && errvar="$(<"$stderr_file")"
rm -f "${stdout_file}" "${stderr_file}"
return $exit_code
}
# Export the run function so it's available to subshells
export -f run