diff --git a/README.org b/README.org index b9715ad..d03d0af 100644 --- a/README.org +++ b/README.org @@ -23,7 +23,7 @@ #+begin_src sh # sf -- script framework (https://github.com/Deleh/sf) - sftrs=$'\e[0m';sftbf=$'\e[1m';sftdim=$'\e[2m';sftul=$'\e[4m';sftblk=$'\e[5m';sftinv=$'\e[7m';sfthd=$'\e[8m';sftclr=$'\e[1A\e[K';sftk=$'\e[30m';sftr=$'\e[31m';sftg=$'\e[32m';sfty=$'\e[33m';sftb=$'\e[34m';sftm=$'\e[35m';sftc=$'\e[36m';sftw=$'\e[97m';function sferr { echo "${sftbf}${sftr}ERROR${sftrs} $1";[ -z "$2" ]&&exit 1;};function sfwarn { echo "${sftbf}${sfty}WARNING${sftrs} $1";};function sfask { if [ -n "$2" ];then read -r -p "$1? [${sftbf}y${sftrs}/${sftbf}N${sftrs}] " sfin;[[ "$sfin" =~ n|N|^$ ]]&&sfin=false||sfin=true;else read -r -p "$1? [${sftbf}Y${sftrs}/${sftbf}n${sftrs}] " sfin;[[ "$sfin" =~ y|Y|^$ ]]&&sfin=true||sfin=false;fi;};function sfget { if [ -n "$2" ];then read -r -p "$1 [${sftbf}$2${sftrs}]: " sfin;else read -r -p "$1: " sfin;fi;[ "$sfin" == "" ]&&[ "$2" != "" ]&&sfin="$2";};function _sferr { echo "${sftbf}${sftr}SF PARSE ERROR${sftrs} $1";exit 1;};OLDIFS=$IFS;IFS=";";_sfpargs=();_sfpheads=();_sfpoffset=0;_sfptails=();_sfpusage="";_sfoheads=();_sfooffset=0;_sfotails=();declare -A _sfflags;declare -A _sfargs;sfargs=("${sfargs[@]}" "help;h;Show this help message and exit");for a in "${sfargs[@]}";do _sfsubst=${a//";"};_sfcount="$(((${#a} - ${#_sfsubst})))";if [ "$_sfcount" -eq 1 ];then read -r -a _sfparsearr<<<"${a}";[[ " ${_sfpargs[*]} " =~ " ${_sfparsearr[0]} " ]]&&_sferr "${sftbf}${_sfparsearr[0]}${sftrs} is already set: ${sftbf}${a}${sftrs}";_sfpargs+=("${_sfparsearr[0]}");_sfpusage="$_sfpusage ${_sfparsearr[0]}";_sfphead="${_sfparsearr[0]}";[ "${#_sfphead}" -gt "${_sfpoffset}" ]&&_sfpoffset="${#_sfphead}";_sfpheads+=("$_sfphead");_sfptails+=("${_sfparsearr[1]}");elif [ "$_sfcount" -eq 2 ];then read -r -a _sfparsearr<<<"${a}";[ -n "${_sfflags["--${_sfparsearr[0]}"]}" ]&&_sferr "${sftbf}${_sfparsearr[0]}${sftrs} is already set: ${sftbf}${a}${sftrs}";_sfflags["--${_sfparsearr[0]}"]="${_sfparsearr[0]}";[ -n "${_sfflags["-${_sfparsearr[1]}"]}" ]&&_sferr "${sftbf}${_sfparsearr[1]}${sftrs} is already set: ${sftbf}${a}${sftrs}";_sfflags["-${_sfparsearr[1]}"]="${_sfparsearr[0]}";declare "${_sfparsearr[0]//-/_}"=false;_sfohead="-${_sfparsearr[1]}, --${_sfparsearr[0]}";[ "${#_sfohead}" -gt "${_sfooffset}" ]&&_sfooffset="${#_sfohead}";_sfoheads+=("$_sfohead");_sfotails+=("${_sfparsearr[2]}");elif [ "$_sfcount" -eq 4 ];then read -r -a _sfparsearr<<<"${a}";[ -n "${_sfargs["--${_sfparsearr[0]}"]}" ]&&_sferr "${sftbf}${_sfparsearr[0]}${sftrs} is already set: ${sftbf}${a}${sftrs}";_sfargs["--${_sfparsearr[0]}"]="${_sfparsearr[0]}";[ -n "${_sfargs["-${_sfparsearr[1]}"]}" ]&&_sferr "${sftbf}${_sfparsearr[1]}${sftrs} is already set: ${sftbf}${a}${sftrs}";_sfargs["-${_sfparsearr[1]}"]="${_sfparsearr[0]}";declare "${_sfparsearr[0]//-/_}"="${_sfparsearr[3]}";_sfohead="-${_sfparsearr[1]}, --${_sfparsearr[0]} ${_sfparsearr[2]}";[ "${#_sfohead}" -gt "${_sfooffset}" ]&&_sfooffset="${#_sfohead}";_sfoheads+=("$_sfohead");[ "${_sfparsearr[3]}" != "" ]&&_sfotails+=("${_sfparsearr[4]} (default: ${_sfparsearr[3]})")||_sfotails+=("${_sfparsearr[4]}");else _sferr "Wrong argument declaration: ${sftbf}${a}${sftrs}";fi;done;_sfeheads=();_sfetails=();_sfeoffset=0;for e in "${sfexamples[@]}";do _sfsubst=${e//";"};_sfcount="$(((${#e} - ${#_sfsubst})))";if [ "$_sfcount" -eq 1 ];then read -r -a _sfparsearr<<<"${e}";_sfehead="${_sfparsearr[0]}";[ "${#_sfehead}" -gt "${_sfeoffset}" ]&&_sfeoffset="${#_sfehead}";_sfeheads+=("$_sfehead");_sfetails+=("${_sfparsearr[1]}");else _sferr "Wrong example declaration: ${sftbf}${e}${sftrs}";fi;done;IFS=$OLDIFS;[ "$sfparr" == true ]&&[ "${#_sfpargs[@]}" == 0 ]&&_sferr "At least one positional argument must be used with ${sftbf}sfparr${sftrs}";_sfpoffset=$(("_sfpoffset" + 3));_sfooffset=$(("_sfooffset" + 3));_sfeoffset=$(("_sfeoffset" + 3));_sfwidth=$(stty size|cut -d ' ' -f 2);_sfpdesc="";for i in "${!_sfptails[@]}";do _sfptail="${_sfptails[$i]}";if [ "$((${#_sfptail} + _sfpoffset))" -gt "$_sfwidth" ];then _sftmpwidth="$((_sfwidth - _sfpoffset))";_sftmpwidth=$(echo -e "${_sftmpwidth}\n1"|sort -nr|head -n 1);_sfptail=$(echo "$_sfptail"|fold -s -w "$_sftmpwidth");_sfptail="${_sfptail//$' \n'/$'\n;'}";fi;_sfpdesc="${_sfpdesc} ${_sfpheads[$i]};${_sfptail}\n";done;_sfodesc="";for i in "${!_sfotails[@]}";do _sfotail="${_sfotails[$i]}";if [ "$((${#_sfotail} + _sfooffset))" -gt "$_sfwidth" ];then _sftmpwidth="$((_sfwidth - _sfooffset))";_sftmpwidth=$(echo -e "${_sftmpwidth}\n1"|sort -nr|head -n 1);_sfotail=$(echo "$_sfotail"|fold -s -w "$_sftmpwidth");_sfotail="${_sfotail//$' \n'/$'\n;'}";fi;_sfodesc="${_sfodesc} ${_sfoheads[$i]};${_sfotail}\n";done;_sfexamples="";for i in "${!_sfetails[@]}";do _sfetail="${_sfetails[$i]}";if [ "$((${#_sfetail} + _sfeoffset))" -gt "$_sfwidth" ];then _sftmpwidth="$((_sfwidth - _sfeoffset))";_sftmpwidth=$(echo -e "${_sftmpwidth}\n1"|sort -nr|head -n 1);_sfetail=$(echo "$_sfetail"|fold -s -w "$_sftmpwidth");_sfetail="${_sfetail//$' \n'/$'\n;'}";fi;_sfexamples="${_sfexamples} ${_sfeheads[$i]};${_sfetail}\n";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;fi;if [ "$_sfodesc" != "" ];then echo -e "\nOPTIONS";echo -e "$_sfodesc"|column -s ";" -t -W 2;fi;if [ "$_sfexamples" != "" ];then echo -e "\nEXAMPLES";echo -e "$_sfexamples"|column -s ";" -t -W 2;fi;if [ -n "${sfextra}" ];then echo -e "\n$sfextra";fi;exit 0;};for a in "$@";do [ "$a" == "-h" ]||[ "$a" == "--help" ]&&_sfusage;done;for d in "${sfdeps[@]}";do if ! command -v "$d"&>/dev/null;then sferr "Command ${sftbf}${d}${sftrs} not found" 0;_sfdeperr=true;fi;done;[ "$_sfdeperr" == true ]&&exit 1;while(("$#"));do if [ -n "${_sfflags["$1"]}" ];then declare "${_sfflags["$1"]//-/_}"=true;elif [ -n "${_sfargs["$1"]}" ];then if [ -n "$2" ]&&[ "${2:0:1}" != "-" ];then declare "${_sfargs["$1"]//-/_}"="$2";shift;else sferr "Argument for ${sftbf}${1}${sftrs} missing";fi;else if [ "${1:0:1}" == "-" ];then sferr "Unsupported argument/flag: ${sftbf}${1}${sftrs}";else if [ "${#_sfpargs[@]}" != 0 ];then declare "${_sfpargs[0]//-/_}"="$1";[ "$sfparr" == true ]&&_sfplast="${_sfpargs[0]//-/_}"&&_sfparr=("$1");_sfpargs=("${_sfpargs[@]:1}");elif [ "$sfparr" == true ];then _sfparr+=("$1");else sferr "Too many positional arguments";fi;fi;fi;shift;done;[ "$sfparr" == true ]&&[ "${#_sfparr[@]}" -gt 0 ]&&read -r -a "${_sfplast?}"<<<"${_sfparr[@]}";if [ "${#_sfpargs[@]}" -gt 0 ];then for p in "${_sfpargs[@]}";do sferr "Positional argument ${sftbf}${p}${sftrs} missing" 0;done;exit 1;fi;unset a d e i OLDIFS _sfargs _sfehead _sfeheads _sfeoffset _sferr _sfetails _sfexamples _sfflags _sfodesc _sfohead _sfoheads _sfooffset _sfotails _sfpargs _sfparr _sfpdesc _sfphead _sfpheads _sfplast _sfpoffset _sfptails _sfpusage _sftmpwidth _sfusage _sfwidth + sftrs=$'\e[0m';sftbf=$'\e[1m';sftdim=$'\e[2m';sftul=$'\e[4m';sftblk=$'\e[5m';sftinv=$'\e[7m';sfthdn=$'\e[8m';sftclr=$'\e[1A\e[K';sftk=$'\e[30m';sftr=$'\e[31m';sftg=$'\e[32m';sfty=$'\e[33m';sftb=$'\e[34m';sftm=$'\e[35m';sftc=$'\e[36m';sftw=$'\e[97m';function sferr { echo -e "${sftbf}${sftr}ERROR${sftrs} $1";[ -z "$2" ]&&exit 1;};function sfwarn { echo -e "${sftbf}${sfty}WARNING${sftrs} $1";};function sfask { if [ -n "$2" ];then echo -ne "$1? [${sftbf}y${sftrs}/${sftbf}N${sftrs}] ";read -r sfin;[[ "$sfin" =~ n|N|^$ ]]&&sfin=false||sfin=true;else echo -ne "$1? [${sftbf}Y${sftrs}/${sftbf}n${sftrs}] ";read -r sfin;[[ "$sfin" =~ y|Y|^$ ]]&&sfin=true||sfin=false;fi;};function sfget { if [ -n "$2" ];then read -r -p "$1 [${sftbf}$2${sftrs}]: " sfin;else read -r -p "$1: " sfin;fi;[ "$sfin" == "" ]&&[ "$2" != "" ]&&sfin="$2";};function _sferr { echo "${sftbf}${sftr}SF PARSE ERROR${sftrs} $1";exit 1;};OLDIFS=$IFS;IFS=";";_sfpargs=();_sfpheads=();_sfpoffset=0;_sfptails=();_sfpusage="";_sfoheads=();_sfooffset=0;_sfotails=();declare -A _sfflags;declare -A _sfargs;sfargs=("${sfargs[@]}" "help;h;Show this help message and exit");for a in "${sfargs[@]}";do _sfsubst=${a//";"};_sfcount="$((${#a} - ${#_sfsubst}))";if [ "$_sfcount" -eq 1 ];then read -r -a _sfparsearr<<<"${a}";[[ " ${_sfpargs[*]} " =~ " ${_sfparsearr[0]} " ]]&&_sferr "${sftbf}${_sfparsearr[0]}${sftrs} is already set: ${sftbf}${a}${sftrs}";_sfpargs+=("${_sfparsearr[0]}");_sfpusage="$_sfpusage ${_sfparsearr[0]}";_sfphead="${_sfparsearr[0]}";[ "${#_sfphead}" -gt "${_sfpoffset}" ]&&_sfpoffset="${#_sfphead}";_sfpheads+=("$_sfphead");_sfptails+=("${_sfparsearr[1]}");elif [ "$_sfcount" -eq 2 ];then read -r -a _sfparsearr<<<"${a}";[ -n "${_sfflags["--${_sfparsearr[0]}"]}" ]&&_sferr "${sftbf}${_sfparsearr[0]}${sftrs} is already set: ${sftbf}${a}${sftrs}";_sfflags["--${_sfparsearr[0]}"]="${_sfparsearr[0]}";[ -n "${_sfflags["-${_sfparsearr[1]}"]}" ]&&_sferr "${sftbf}${_sfparsearr[1]}${sftrs} is already set: ${sftbf}${a}${sftrs}";_sfflags["-${_sfparsearr[1]}"]="${_sfparsearr[0]}";declare "${_sfparsearr[0]//-/_}"=false;_sfohead="-${_sfparsearr[1]}, --${_sfparsearr[0]}";[ "${#_sfohead}" -gt "${_sfooffset}" ]&&_sfooffset="${#_sfohead}";_sfoheads+=("$_sfohead");_sfotails+=("${_sfparsearr[2]}");elif [ "$_sfcount" -eq 4 ];then read -r -a _sfparsearr<<<"${a}";[ -n "${_sfargs["--${_sfparsearr[0]}"]}" ]&&_sferr "${sftbf}${_sfparsearr[0]}${sftrs} is already set: ${sftbf}${a}${sftrs}";_sfargs["--${_sfparsearr[0]}"]="${_sfparsearr[0]}";[ -n "${_sfargs["-${_sfparsearr[1]}"]}" ]&&_sferr "${sftbf}${_sfparsearr[1]}${sftrs} is already set: ${sftbf}${a}${sftrs}";_sfargs["-${_sfparsearr[1]}"]="${_sfparsearr[0]}";declare "${_sfparsearr[0]//-/_}"="${_sfparsearr[3]}";_sfohead="-${_sfparsearr[1]}, --${_sfparsearr[0]} ${_sfparsearr[2]}";[ "${#_sfohead}" -gt "${_sfooffset}" ]&&_sfooffset="${#_sfohead}";_sfoheads+=("$_sfohead");[ "${_sfparsearr[3]}" != "" ]&&_sfotails+=("${_sfparsearr[4]} (default: ${_sfparsearr[3]})")||_sfotails+=("${_sfparsearr[4]}");else _sferr "Wrong argument declaration: ${sftbf}${a}${sftrs}";fi;done;_sfeheads=();_sfetails=();_sfeoffset=0;for e in "${sfexamples[@]}";do _sfsubst=${e//";"};_sfcount="$((${#e} - ${#_sfsubst}))";if [ "$_sfcount" -eq 1 ];then read -r -a _sfparsearr<<<"${e}";_sfehead="${_sfparsearr[0]}";[ "${#_sfehead}" -gt "${_sfeoffset}" ]&&_sfeoffset="${#_sfehead}";_sfeheads+=("$_sfehead");_sfetails+=("${_sfparsearr[1]}");else _sferr "Wrong example declaration: ${sftbf}${e}${sftrs}";fi;done;IFS=$OLDIFS;[ "$sfparr" == true ]&&[ "${#_sfpargs[@]}" == 0 ]&&_sferr "At least one positional argument must be used with ${sftbf}sfparr${sftrs}";[ "$sfparr" == true ]&&_sfpusage="${_sfpusage% *} [${_sfpusage##* } ...]";_sfpoffset=$(("_sfpoffset" + 3));_sfooffset=$(("_sfooffset" + 3));_sfeoffset=$(("_sfeoffset" + 3));_sfwidth=$(stty size|cut -d ' ' -f 2);_sfpdesc="";for i in "${!_sfptails[@]}";do _sfptail="${_sfptails[$i]}";if [ "$((${#_sfptail} + _sfpoffset))" -gt "$_sfwidth" ];then _sftmpwidth="$((_sfwidth - _sfpoffset))";_sftmpwidth=$(echo -e "${_sftmpwidth}\n1"|sort -nr|head -n 1);_sfptail=$(echo "$_sfptail"|fold -s -w "$_sftmpwidth");_sfptail="${_sfptail//$' \n'/$'\n;'}";fi;_sfpdesc="${_sfpdesc} ${_sfpheads[$i]};${_sfptail}\n";done;_sfodesc="";for i in "${!_sfotails[@]}";do _sfotail="${_sfotails[$i]}";if [ "$((${#_sfotail} + _sfooffset))" -gt "$_sfwidth" ];then _sftmpwidth="$((_sfwidth - _sfooffset))";_sftmpwidth=$(echo -e "${_sftmpwidth}\n1"|sort -nr|head -n 1);_sfotail=$(echo "$_sfotail"|fold -s -w "$_sftmpwidth");_sfotail="${_sfotail//$' \n'/$'\n;'}";fi;_sfodesc="${_sfodesc} ${_sfoheads[$i]};${_sfotail}\n";done;_sfexamples="";for i in "${!_sfetails[@]}";do _sfetail="${_sfetails[$i]}";if [ "$((${#_sfetail} + _sfeoffset))" -gt "$_sfwidth" ];then _sftmpwidth="$((_sfwidth - _sfeoffset))";_sftmpwidth=$(echo -e "${_sftmpwidth}\n1"|sort -nr|head -n 1);_sfetail=$(echo "$_sfetail"|fold -s -w "$_sftmpwidth");_sfetail="${_sfetail//$' \n'/$'\n;'}";fi;_sfexamples="${_sfexamples} ${_sfeheads[$i]};${_sfetail}\n";done;function _sfusage { echo -n "Usage: $(basename "$0") [OPTIONS]";echo -ne "$_sfpusage";echo;[ -n "${sfdesc}" ]&&echo -e "\n$sfdesc"|fold -s -w "$_sfwidth";if [ "$_sfpdesc" != "" ];then echo -e "\nPOSITIONAL ARGUMENTS";echo -e "$_sfpdesc"|column -s ";" -t;fi;if [ "$_sfodesc" != "" ];then echo -e "\nOPTIONS";echo -e "$_sfodesc"|column -s ";" -t;fi;if [ "$_sfexamples" != "" ];then echo -e "\nEXAMPLES";echo -e "$_sfexamples"|column -s ";" -t;fi;[ -n "${sfextra}" ]&&echo -e "\n$sfextra"|fold -s -w "$_sfwidth";exit 0;};for a in "$@";do [ "$a" == "-h" ]||[ "$a" == "--help" ]&&_sfusage;done;for d in "${sfdeps[@]}";do if ! command -v "$d"&>/dev/null;then sferr "Command ${sftbf}${d}${sftrs} not found" 0;_sfdeperr=true;fi;done;[ "$_sfdeperr" == true ]&&exit 1;while(("$#"));do if [ -n "${_sfflags["$1"]}" ];then declare "${_sfflags["$1"]//-/_}"=true;elif [ -n "${_sfargs["$1"]}" ];then if [ -n "$2" ]&&[ "${2:0:1}" != "-" ];then declare "${_sfargs["$1"]//-/_}"="$2";shift;else sferr "Argument for ${sftbf}${1}${sftrs} missing";fi;else if [ "${1:0:1}" == "-" ];then sferr "Unsupported argument/flag ${sftbf}${1}${sftrs}";else if [ "${#_sfpargs[@]}" != 0 ];then declare "${_sfpargs[0]//-/_}"="$1";[ "$sfparr" == true ]&&_sfplast="${_sfpargs[0]//-/_}"&&_sfparr=("$1");_sfpargs=("${_sfpargs[@]:1}");elif [ "$sfparr" == true ];then _sfparr+=("$1");else sferr "Too many positional arguments";fi;fi;fi;shift;done;[ "$sfparr" == true ]&&[ "${#_sfparr[@]}" -gt 0 ]&&read -r -a "${_sfplast?}"<<<"${_sfparr[@]}";[ "$sfparr" == true ]&&[ "${#_sfpargs[@]}" -gt 0 ]&&unset '_sfpargs[${#_sfpargs[@]}-1]';if [ "${#_sfpargs[@]}" -gt 0 ];then for p in "${_sfpargs[@]}";do sferr "Positional argument ${sftbf}${p}${sftrs} missing" 0;done;exit 1;fi;unset a d e i OLDIFS _sfargs _sfehead _sfeheads _sfeoffset _sferr _sfetails _sfexamples _sfflags _sfodesc _sfohead _sfoheads _sfooffset _sfotails _sfpargs _sfparr _sfpdesc _sfphead _sfpheads _sfplast _sfpoffset _sfptails _sfpusage _sftmpwidth _sfusage _sfwidth #+end_src ** Requirements @@ -43,14 +43,14 @@ This is the list of variables which can be set *before* including /sf/. Everything is optional. - | Name | Description | Example | - |--------------+------------------------------------------------------------------------------------------------------+--------------------------------------| - | =sfdesc= | Description of the script | ~sfdesc="This script does nothing."~ | - | =sfargs= | Array for declaration of arguments, positional arguments and flags. Look below for more information | See [[#sfargs][below]] | - | =sfparr= | Flag which indicates if the last declared positional argument should be treated as array | ~sfparr=true~ | - | =sfexamples= | Array for declaration of examples for the usage output. Look below for more information | See also [[#sfexamples][below]] | - | =sfextra= | Additional usage output | ~sfextra="No copyright."~ | - | =sfdeps= | Array for declaration of script dependencies. An error is thrown if a given command is not available | ~sfdeps=("ffmpeg" "opusinfo")~ | + | Name | Description | Example | + |--------------+-----------------------------------------------------------------------------------------------------------------------+-------------------------------------| + | =sfdesc= | Description of the script | ~sfdesc="This script does nothing"~ | + | =sfargs= | Array for declaration of arguments, positional arguments and flags. Look below for more information | See [[#sfargs][below]] | + | =sfparr= | Flag which indicates if the last declared positional argument should be treated as array | ~sfparr=true~ | + | =sfexamples= | Array for declaration of examples for the usage output. Look below for more information | See also [[#sfexamples][below]] | + | =sfextra= | Additional usage output | ~sfextra="No copyright"~ | + | =sfdeps= | Array for declaration of script dependencies. An error is thrown if one ore more of the set command are not available | ~sfdeps=("ffmpeg" "opusinfo")~ | Examples which show the usage of all variables can be found [[#examples][below]] and in the =examples= directory. @@ -60,7 +60,7 @@ :end: This is an array of strings. - Every string defines an argument, a flag or an positional argument of the script. + Every string defines an argument, a flag or a positional argument of the script. The type is defined by the amount of semicolons in the string. | Type | Declaration order | Example | @@ -145,10 +145,10 @@ | =sftrs= | Reset formatting | | =sftbf= | Bold | | =sftdim= | Dim | - | =sftul= | Underline | + | =sftul= | Underlined | | =sftblk= | Blinking | | =sftinv= | Invert foreground/background | - | =sfthd= | Hidden | + | =sfthdn= | Hidden | | =sftclr= | Clear the previous line | | =sftk= | Black | | =sftr= | Red | @@ -166,6 +166,8 @@ echo "${sftbf}${sftr}framework${sftrs}" #+end_src + Look at the [[#clear][clear]] example to see some of them in action. + ** Examples :properties: :custom_id: examples @@ -255,6 +257,66 @@ go #+end_example +*** Clear + :properties: + :custom_id: clear + :end: + + This script shows the usage of color formatting variables and =$sftclr=: + + #+begin_src sh + #!/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 + #+end_src + + The produced usage is: + + #+begin_example + Usage: clear [OPTIONS] + + Show the usage of color variables and $sftclr. + + OPTIONS + -h, --help Show this help message and exit + #+end_example + + The execution results in this: + + #+begin_example + $ ./clear + Tadaa! + #+end_example + *** Add This script adds numbers and shows the usage of =sfparr=: @@ -299,7 +361,7 @@ And here is the produced usage: #+begin_example - Usage: add [OPTIONS] NUMBERS ... + Usage: add [OPTIONS] [NUMBERS ...] Calculate the sum of multiple numbers. diff --git a/examples/clear b/examples/clear new file mode 100755 index 0000000..ca91b36 --- /dev/null +++ b/examples/clear @@ -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 diff --git a/sf b/sf index d3572aa..8066bc2 100644 --- a/sf +++ b/sf @@ -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