Reescrevendo procedimentos

This commit is contained in:
Christian Tosta
2025-06-08 19:59:27 -03:00
parent d49c0e6b76
commit aa455e90bd
2 changed files with 405 additions and 133 deletions

View File

@@ -10,12 +10,12 @@ export C_YELLOW="\e[38;5;220m"
export C_PURPLE="\e[38;5;5m"
export S_DEBUG="${C_PURPLE}\u2691 [debug]${F_NONE} "
export S_CRIT="${C_RED}\u2691 [critical]${F_NONE} "
export S_CRIT="${F_BOLD}${C_RED}\u2691 [critical]${F_NONE} "
export S_ERROR="${C_RED}\u2691 [error]${F_NONE} "
export S_WARN="${C_ORANGE}\u2691 [warning]${F_NONE} "
export S_INFO="${C_BLUE}\u2691 [info]${F_NONE} "
export S_INFO="${F_BOLD}${C_BLUE}\u2691 [info]${F_NONE} "
export S_NOTICE="${C_BLUE}\u2691 [notice]${F_NONE} "
export S_SUCCESS="${C_GREEN}\u2691 [success]${F_NONE} "
export S_SUCCESS="${F_BOLD}${C_GREEN}\u2691 [success]${F_NONE} "
export U_ITEM="\u21e2"
export U_TICK="\u2713"
@@ -24,8 +24,14 @@ export U_FAIL="\u2715"
export COLS="$(( $(tput cols) - 1 ))"
export EL=$(tput el)
: "cli.clear"
function cli.clear {
clear
}
cli.cursor_pos() {
# Get the cursor current position and returns CURSOR_X, CURSOR_Y
: "cli.get_cursor"
cli.get_cursor() {
local _xy
echo -ne $"\E[6n"
@@ -33,42 +39,144 @@ cli.cursor_pos() {
_xy="${_xy#*[}"
export CURSOR_X=${_xy/*;}
export CURSOR_Y=${_xy/;*}
return 0
}
# Puts the cursor at x:y (defaults to 0:0)
: "cli.set_cursor <x> <y>"
cli.set_cursor() {
printf "\E[%sH" "${2:0};${1:0}"
}
# Just prints N blank lines
: "cli.jump <number_lines>"
cli.jump() {
for i in $(seq 1 ${1:-1}); do
printf "%s${EL}\n"
done
}
# Just like printf
: "cli.print [<printf_format>] <arg1> [<arg2> ...]"
cli.print() {
printf $"${1}" ${@:2}
}
# Folds text at 'size' column
: "cli.fold <size> <text>"
cli.fold() {
printf "%s" "${@:2}" | fold -sw ${1}
}
: "cli.status <status> [<printf_format>] <arg>"
cli.status() {
printf "%0.s\n"
local fmt=${2//\\n}
cli.jump
case "${1:-info}" in
debug) printf "\e[0F${S_DEBUG}${2:-%s}" "${@:3}${EL}" ;;
crit*) printf "\e[0F${S_CRIT}${2:-%s}" "${@:3}${EL}" ;;
error) printf "\e[0F${S_ERROR}${2:-%s}" "${@:3}${EL}" ;;
warn*) printf "\e[0F${S_WARN}${2:-%s}" "${@:3}${EL}" ;;
info*) printf "\e[0F${S_INFO}${2:-%s}" "${@:3}${EL}" ;;
noti*) printf "\e[0F${S_NOTICE}${2:-%s}" "${@:3}${EL}" ;;
succ*) printf "\e[0F${S_SUCCESS}${2:-%s}" "${@:3}${EL}" ;;
*) printf "\e[0F${S_INFO}${2:-%s}" "${@:3}${EL}" ;;
debug) printf "\e[0F${S_DEBUG}${fmt:-%s}${@:3}${EL}\n" ;;
crit*) printf "\e[0F${S_CRIT}${fmt:-%s}${@:3}${EL}\n" ;;
error) printf "\e[0F${S_ERROR}${fmt:-%s}${@:3}${EL}\n" ;;
warn*) printf "\e[0F${S_WARN}${fmt:-%s}${@:3}${EL}\n" ;;
info*) printf "\e[0F${S_INFO}${fmt:-%s}${@:3}${EL}\n" ;;
noti*) printf "\e[0F${S_NOTICE}${fmt:-%s}${@:3}${EL}\n" ;;
succ*) printf "\e[0F${S_SUCCESS}${fmt:-%s}${@:3}${EL}\n" ;;
*) printf "\e[0F${S_INFO}${fmt:-%s}" "${@:3}" ;;
esac
return 0
}
: "cli.color <color> [<printf_format>] <arg1> [<arg2> ...]"
cli.color() {
case "${1:-info}" in
red) printf "${C_RED}${2:-%s}${C_NONE}" "${@:3}${EL}" ;;
blue) printf "${C_BLUE}${2:-%s}${C_NONE}" "${@:3}${EL}" ;;
green) printf "${C_GREEN}${2:-%s}${C_NONE}" "${@:3}${EL}" ;;
orange) printf "${C_ORANGE}${2:-%s}${C_NONE}" "${@:3}${EL}" ;;
yellow) printf "${C_YELLOW}${2:-%s}${C_NONE}" "${@:3}${EL}" ;;
purple) printf "${C_PURPLE}${2:-%s}${C_NONE}" "${@:3}${EL}" ;;
*) printf "${C_NONE}${2:-%s}${C_NONE}" "${@:3}${EL}" ;;
local fmt="${2:-}"
fmt="${fmt//\\n}"
case "${1:-}" in
red) printf "${C_RED}${fmt:-%s}${C_NONE}" ${@:3} ;;
blue) printf "${C_BLUE}${fmt:-%s}${C_NONE}" ${@:3} ;;
green) printf "${C_GREEN}${fmt:-%s}${C_NONE}" ${@:3} ;;
orange) printf "${C_ORANGE}${fmt:-%s}${C_NONE}" ${@:3} ;;
yellow) printf "${C_YELLOW}${fmt:-%s}${C_NONE}" ${@:3} ;;
purple) printf "${C_PURPLE}${fmt:-%s}${C_NONE}" ${@:3} ;;
*) printf "${C_NONE}${fmt:-%s}${C_NONE}" ${@:3} ;;
esac
return 0
}
: "cli.emphasis <color|bold> [<printf_format>] <arg1> [<arg2> ...]"
cli.emphasis() {
local fmt="${2:-}"
fmt="${fmt//\\n}"
case "${1:-bold}" in
red) printf "${C_RED}${F_BOLD}${fmt:-%s}${F_NONE}" "${@:3}" ;;
blue) printf "${C_BLUE}${F_BOLD}${fmt:-%s}${F_NONE}" "${@:3}" ;;
green) printf "${C_GREEN}${F_BOLD}${fmt:-%s}${F_NONE}" "${@:3}" ;;
orange) printf "${C_ORANGE}${F_BOLD}${fmt:-%s}${F_NONE}" "${@:3}" ;;
yellow) printf "${C_YELLOW}${F_BOLD}${fmt:-%s}${F_NONE}" "${@:3}" ;;
purple) printf "${C_PURPLE}${F_BOLD}${fmt:-%s}${F_NONE}" "${@:3}" ;;
bold|*) printf "${F_BOLD}${fmt:-%s}${F_NONE}" "${@:3}" ;;
esac
}
: "cli.bold <arg>"
cli.bold() {
printf "${F_BOLD}${1:-%s}${F_NONE}" "${@:2}${EL}"
return 0
cli.emphasis bold "%s" "${@}"
}
: "cli.tab <size>"
cli.tab() {
s=${1:-2}
! [[ -z "${s//[0-9]}" ]] && s=2
printf "%${s:-2}s" " "
}
: "cli.items [<printf_format>] <text> [<text> ...]"
cli.items() {
printf "${U_ITEM} ${1:-%s}" "${@:2}"
}
: "cli.items <text>"
cli.item() {
cli.items "%s\n" "${@}" | head -1
}
: "cli.title <text>"
cli.title() {
cli.items "%s" "$(cli.emphasis blue "%s" "${@^^}")"
cli.line
}
: "cli.subtitle <text>"
cli.subtitle() {
printf "%0.s\u2508%s"
cli.bold " ${@^^} "
cli.get_cursor
for i in $(seq 0 $((( ${COLS} - ${CURSOR_X} )))); do
printf "%0.s\u2508"
done
cli.jump
}
: "cli.prompt <tab_size> [<prompt_text>]"
cli.prompt() {
read -p "$(cli.print ${1:-})" k
echo ${k}
}
# Draws a horizontal line
: "cli.line [nobreak]"
cli.line() {
[[ -z "${1:-}" ]] && cli.jump
for i in $(seq 1 ${COLS}); do
printf "%0.s\u2504"
done
[[ -z "${1:-}" ]] && cli.jump
}
# Draws a thin horizontal divisor line
: "cli.boldline"
cli.boldline() {
for i in $(seq 1 ${COLS}); do
printf "%0.s\u2581"
done
}
: "cli.section <printf_format> <title> <subtitle> <content>"
cli.section() {
__section.header() {
__section.title() {
@@ -77,24 +185,18 @@ cli.section() {
$(echo -ne ${C_ORANGE}${@:3}${F_NONE})
}
__section.title.underline() {
for i in $(seq 1 ${COLS}); do \
printf "%s" $(echo -ne "\u2581"); \
done
cli.boldline
}
__section.title "${@}"
__section.title.underline
}
__section.content() {
printf "%2s\n"
printf "%s" "${@}" | fold -sw ${COLS}
cli.jump 2
cli.fold ${COLS} "${@}"
}
__section.footer() {
__section.footer.line() {
[ -z "${1:-}" ] && printf "%s\n"
for i in $(seq 1 ${COLS}); do \
printf "%s" $(echo -ne "\u2504${F_NONE}"); \
done && \
[ -z "${1:-}" ] && printf "%2s\n"
cli.line
}
__section.footer.line
}
@@ -102,3 +204,10 @@ cli.section() {
__section.content "${@:4}"
__section.footer
}
cli.self_compress() {
cat cli.bash | \
gzip -c9 - | \
base64 -w77 | \
xargs printf ' %s \\\n'
}