Compare commits

...

10 commits

Author SHA1 Message Date
1386f61c55 update README 2023-01-15 20:48:33 +01:00
Denis Lehmann
bc9adccdf4 fold sfextra 2023-01-13 10:26:26 +01:00
Denis Lehmann
6c19d9b812 correct comment 2023-01-13 10:26:15 +01:00
Denis Lehmann
8d8e409bf7 shellcheck 2022-07-27 16:59:39 +02:00
3b2efe15bc adjust error output 2022-06-19 03:35:41 +02:00
eb904a2059 correct escapes 2022-05-19 01:05:17 +02:00
dd97cb372f update text formatting variable 2022-05-15 21:11:36 +02:00
aa92dda307 add clear example 2022-04-23 10:27:28 +02:00
0a23684b82 allow empty argument arrays 2022-04-08 00:05:53 +02:00
24715e6867 remove unnecessary flag 2022-04-06 19:42:10 +02:00
3 changed files with 127 additions and 28 deletions

File diff suppressed because one or more lines are too long

32
examples/clear Executable file
View file

@ -0,0 +1,32 @@
#!/usr/bin/env bash
# ----------------------
# sf -- script framework
# ----------------------
# Declare sf variables
sfdesc="Show the usage of color variables and \$sftclr."
# Include sf, this could be replaced with a long oneliner
source "$(dirname $0)/../sf"
# ----------------------
# Actual script
# ----------------------
echo -n "${sftbf}" # Output everything from here bold
echo "${sftr}These" # Red
sleep 0.5
echo "${sftm}lines" # Magenta
sleep 0.5
echo "${sftb}will" # Blue
sleep 0.5
echo "${sftc}delete" # Cyan
sleep 0.5
echo "${sftg}themselves" # Green
sleep 1
echo "${sfty}now!" # Yellow
sleep 0.5
echo -n "${sftclr}${sftclr}${sftclr}${sftclr}${sftclr}${sftclr}" # Clear six lines
echo "${sftblk}${sftr}T${sftm}a${sftb}d${sftc}a${sftg}a${sfty}!" # Blinking colorful
echo -n "${sftrs}" # Reset text formatting

35
sf
View file

@ -9,7 +9,7 @@ sftdim=$'\e[2m'
sftul=$'\e[4m'
sftblk=$'\e[5m'
sftinv=$'\e[7m'
sfthd=$'\e[8m'
sfthdn=$'\e[8m'
sftclr=$'\e[1A\e[K'
sftk=$'\e[30m'
sftr=$'\e[31m'
@ -22,21 +22,23 @@ sftw=$'\e[97m'
# Public output functions
function sferr {
echo "${sftbf}${sftr}ERROR${sftrs} $1"
echo -e "${sftbf}${sftr}ERROR${sftrs} $1"
[ -z "$2" ] && exit 1
}
function sfwarn {
echo "${sftbf}${sfty}WARNING${sftrs} $1"
echo -e "${sftbf}${sfty}WARNING${sftrs} $1"
}
# Public input functions
function sfask {
if [ -n "$2" ]; then
read -r -p "$1? [${sftbf}y${sftrs}/${sftbf}N${sftrs}] " sfin
echo -ne "$1? [${sftbf}y${sftrs}/${sftbf}N${sftrs}] "
read -r sfin
[[ "$sfin" =~ n|N|^$ ]] && sfin=false || sfin=true
else
read -r -p "$1? [${sftbf}Y${sftrs}/${sftbf}n${sftrs}] " sfin
echo -ne "$1? [${sftbf}Y${sftrs}/${sftbf}n${sftrs}] "
read -r sfin
[[ "$sfin" =~ y|Y|^$ ]] && sfin=true || sfin=false
fi
}
@ -76,7 +78,7 @@ for a in "${sfargs[@]}"; do
# Get amount of semicolons
_sfsubst=${a//";"}
_sfcount="$(((${#a} - ${#_sfsubst})))"
_sfcount="$(( ${#a} - ${#_sfsubst} ))"
# Positional arguments
if [ "$_sfcount" -eq 1 ]; then
@ -150,7 +152,7 @@ for e in "${sfexamples[@]}"; do
# Get amount of ;
_sfsubst=${e//";"}
_sfcount="$(((${#e} - ${#_sfsubst})))"
_sfcount="$(( ${#e} - ${#_sfsubst} ))"
if [ "$_sfcount" -eq 1 ]; then
@ -173,6 +175,9 @@ IFS=$OLDIFS
# Check if at least one positional argument is set if 'sfparr' is used
[ "$sfparr" == true ] && [ "${#_sfpargs[@]}" == 0 ] && _sferr "At least one positional argument must be used with ${sftbf}sfparr${sftrs}"
# Correct positional argument description if 'sfparr' is used
[ "$sfparr" == true ] && _sfpusage="${_sfpusage% *} [${_sfpusage##* } ...]"
# Correct offsets
_sfpoffset=$(( "_sfpoffset" + 3 ))
_sfooffset=$(( "_sfooffset" + 3 ))
@ -224,24 +229,21 @@ done
function _sfusage {
echo -n "Usage: $(basename "$0") [OPTIONS]"
echo -ne "$_sfpusage"
[ "$sfparr" == true ] && echo -n " ..."
echo
[ -n "${sfdesc}" ] && echo -e "\n$sfdesc" | fold -s -w "$_sfwidth"
if [ "$_sfpdesc" != "" ]; then
echo -e "\nPOSITIONAL ARGUMENTS"
echo -e "$_sfpdesc" | column -s ";" -t -W 2
echo -e "$_sfpdesc" | column -s ";" -t
fi
if [ "$_sfodesc" != "" ]; then
echo -e "\nOPTIONS"
echo -e "$_sfodesc" | column -s ";" -t -W 2
echo -e "$_sfodesc" | column -s ";" -t
fi
if [ "$_sfexamples" != "" ]; then
echo -e "\nEXAMPLES"
echo -e "$_sfexamples" | column -s ";" -t -W 2
fi
if [ -n "${sfextra}" ]; then
echo -e "\n$sfextra"
echo -e "$_sfexamples" | column -s ";" -t
fi
[ -n "${sfextra}" ] && echo -e "\n$sfextra" | fold -s -w "$_sfwidth"
exit 0
}
@ -282,7 +284,7 @@ while (( "$#" )); do
else
# Check if arg starts with -
if [ "${1:0:1}" == "-" ]; then
sferr "Unsupported argument/flag: ${sftbf}${1}${sftrs}"
sferr "Unsupported argument/flag ${sftbf}${1}${sftrs}"
else
# Set positional argument
if [ "${#_sfpargs[@]}" != 0 ]; then
@ -302,6 +304,9 @@ done
# Parse additional arguments if 'sfparr' is set
[ "$sfparr" == true ] && [ "${#_sfparr[@]}" -gt 0 ] && read -r -a "${_sfplast?}" <<< "${_sfparr[@]}"
# Remove last missing argument to allow empty arrays if 'sfparr' is set
[ "$sfparr" == true ] && [ "${#_sfpargs[@]}" -gt 0 ] && unset '_sfpargs[${#_sfpargs[@]}-1]'
# Check if positional arguments left
if [ "${#_sfpargs[@]}" -gt 0 ]; then
for p in "${_sfpargs[@]}"; do