Implemented FormatUtils & used for startup banner.

The new horizontal utility utilizes the * token for centering.
* Every field with the token %*s will autofill the remaining space.
* Field tokens may be any valid format specifier, plus the asterisk.
* > Example: printf "%*s %.*s %*d" "col1" "col2" "3.0" (notice asterisk)
* > Example: printf "%*b %s" "\e[1;33mThis is red text\e[0m" "this is not"

Use the new utilities to center the startup banner and other messages.
This commit is contained in:
Matias Barcenas 2017-08-12 15:43:39 -05:00
parent 15494058aa
commit ad89264442
2 changed files with 104 additions and 24 deletions

View File

@ -25,14 +25,14 @@ export FLUXIONOutputDevice=$([ $FLUXIONDebug ] && echo "/dev/stdout" || echo "/d
FLUXIONHoldXterm=$([ $FLUXIONDebug ] && echo "-hold" || echo "")
################################# < Shell Color Codes > ################################
CRed="\033[1;31m"
CGrn="\033[1;32m"
CYel="\033[1;33m"
CBlu="\033[1;34m"
CPrp="\033[5;35m"
CCyn="\033[5;36m"
CGry="\033[0;37m"
CWht="\033[1;37m"
CRed="\e[1;31m"
CGrn="\e[1;32m"
CYel="\e[1;33m"
CBlu="\e[1;34m"
CPrp="\e[5;35m"
CCyn="\e[5;36m"
CGry="\e[0;37m"
CWht="\e[1;37m"
CClr="\e[0m"
################################ < FLUXION Parameters > ################################
@ -41,6 +41,7 @@ FLUXIONVLine="$CRed[$CYel*$CRed]$CClr"
################################# < Library Includes > #################################
source lib/SandboxUtils.sh
source lib/FormatUtils.sh
source lib/IOUtils.sh
source lib/HashUtils.sh
@ -244,19 +245,23 @@ function check_dependencies() {
for CLITool in ${CLITools[*]}; do
# Could use parameter replacement, but requires extra variable.
echo -ne "$FLUXIONVLine `printf "%-64s" "$CLITool" | sed 's/ /./g'`"
local line=$(printf "%-44s" "$CLITool" | sed 's/ /./g')
#line+=$([ ! hash $CLITool 2>/dev/null ] && echo "$CRed Missing!$CClr" || echo ".....$CGrn OK.$CClr")
if ! hash $CLITool 2>/dev/null; then
echo -e "$CRed Missing!$CClr"
line+="$CRed Missing!$CClr"
CLIToolsMissing=1
else
echo -e ".....$CGrn OK.$CClr"
line+=".....$CGrn OK.$CClr"
fi
format_center "$FLUXIONVLine $line"
echo -e "$FormatCenter"
sleep 0.025
done
if [ $CLIToolsMissing ]; then
if [ "$CLIToolsMissing" ]; then
exit 1
fi
@ -269,23 +274,31 @@ if [ ! -d "$FLUXIONWorkspacePath" ]; then
fi
if [ ! $FLUXIONDebug ]; then
clear; echo
sleep 0.01 && echo -e "$CRed "
sleep 0.01 && echo -e " ⌠▓▒▓▒ ⌠▓╗ ⌠█┐ ┌█ ┌▓\ /▓┐ ⌠▓╖ ⌠◙▒▓▒◙ ⌠█\ ☒┐ "
sleep 0.01 && echo -e " ║▒_ │▒║ │▒║ ║▒ \▒\/▒/ │☢╫ │▒┌╤┐▒ ║▓▒\ ▓║ "
sleep 0.01 && echo -e " ≡◙◙ ║◙║ ║◙║ ║◙ ◙◙ ║¤▒ ║▓║☯║▓ ♜◙\✪\◙♜ "
sleep 0.01 && echo -e " ║▒ │▒║__ │▒└_┘▒ /▒/\▒\ │☢╫ │▒└╧┘▒ ║█ \▒█║ "
sleep 0.01 && echo -e " ⌡▓ ⌡◘▒▓▒ ⌡◘▒▓▒◘ └▓/ \▓┘ ⌡▓╝ ⌡◙▒▓▒◙ ⌡▓ \▓┘ "
sleep 0.01 && echo -e " ¯¯¯ ¯¯¯¯¯¯ ¯¯¯¯¯¯¯ ¯¯¯ ¯¯¯ ¯¯¯¯ ¯¯¯¯¯¯¯ ¯¯¯¯¯¯¯¯ "
clear; echo -e "$CRed"
FLUXIONBanner=()
format_center " ⌠▓▒▓▒ ⌠▓╗ ⌠█┐ ┌█ ┌▓\ /▓┐ ⌠▓╖ ⌠◙▒▓▒◙ ⌠█\ ☒┐"; FLUXIONBanner[${#FLUXIONBanner[@]}]="$FormatCenter";
format_center " ║▒_ │▒║ │▒║ ║▒ \▒\/▒/ │☢╫ │▒┌╤┐▒ ║▓▒\ ▓║"; FLUXIONBanner[${#FLUXIONBanner[@]}]="$FormatCenter";
format_center " ≡◙◙ ║◙║ ║◙║ ║◙ ◙◙ ║¤▒ ║▓║☯║▓ ♜◙\✪\◙♜"; FLUXIONBanner[${#FLUXIONBanner[@]}]="$FormatCenter";
format_center " ║▒ │▒║__ │▒└_┘▒ /▒/\▒\ │☢╫ │▒└╧┘▒ ║█ \▒█║"; FLUXIONBanner[${#FLUXIONBanner[@]}]="$FormatCenter";
format_center " ⌡▓ ⌡◘▒▓▒ ⌡◘▒▓▒◘ └▓/ \▓┘ ⌡▓╝ ⌡◙▒▓▒◙ ⌡▓ \▓┘"; FLUXIONBanner[${#FLUXIONBanner[@]}]="$FormatCenter";
format_center "¯¯¯ ¯¯¯¯¯¯ ¯¯¯¯¯¯¯ ¯¯¯ ¯¯¯ ¯¯¯¯ ¯¯¯¯¯¯¯ ¯¯¯¯¯¯¯¯"; FLUXIONBanner[${#FLUXIONBanner[@]}]="$FormatCenter";
for line in "${FLUXIONBanner[@]}"; do
echo "$line"; sleep 0.1
done
echo
sleep 0.1
echo -e "$CRed FLUXION $CWht$FLUXIONVersion (rev. $CGrn$FLUXIONRevision$CWht)$CYel by$CWht ghost"
format_center "${CGrn}Site: ${CRed}https://github.com/FluxionNetwork/fluxion$CClr"; echo -e "$FormatCenter"
sleep 0.1
echo -e "$CGrn Site: ${CRed}https://github.com/FluxionNetwork/fluxion$CClr"
format_center "${CRed}FLUXION $CWht$FLUXIONVersion (rev. $CGrn$FLUXIONRevision$CWht)$CYel by$CWht ghost"; echo -e "$FormatCenter"
sleep 0.1
echo -n " Online Version"
FLUXIONVNotice="Online Version"
FLUXIONVNoticeOffset=$(($(tput cols) / 2 + ((${#FLUXIONVNotice} / 2) - 4)))
printf "%${FLUXIONVNoticeOffset}s" "Online Version"
check_updates &
spinner "$!"

67
lib/FormatUtils.sh Normal file
View File

@ -0,0 +1,67 @@
#!/bin/bash
FormatTabLength=8
# This should be relocated (here temporarily)
tabs -$FormatTabLength # Set tab width to var
format_strip_invisibles() {
FormatStripInvisibles=$1
# Strip color escape sequences
FormatStripInvisibles=$(echo "$FormatStripInvisibles" | sed -r 's/\\e\[([0-9]*;?[0-9]+)m//g')
# Strip control characters
FormatStripInvisibles=$(echo "$FormatStripInvisibles" | sed -r 's/\\t//g' | sed -r 's/\\n//g')
}
format_strip_specifiers() {
FormatStripSpecifiers=$(echo "$1" | sed -r 's/%[\+-]?(([0-9]+|\*)|[0-9]*\.([0-9]+|\*))?[bqdiouxXfeEgGcsnaA]//g')
}
format_list_specifiers() {
# Special specifier also included (with length value as '*').
FormatListSpecifiers=($(echo "$1" | grep -oP '%[\+-]?(([0-9]+|\*)|[0-9]*\.([0-9]+|\*))?[bqdiouxXfeEgGcsnaA]'))
}
format_emulate_expansion() {
echo
}
format_calculate_length() {
# Retrieve string of printable characters only in format before substitution.
format_strip_invisibles "`echo "$1" | sed -r 's/%%/%1s/g'`"
local __format_calculate_length__visibles=$FormatStripInvisibles
# Calculate number of all printable characters in format before substitution.
format_strip_specifiers "$__format_calculate_length__visibles"
local __format_calculate_length__literalsLength=${#FormatStripSpecifiers}
format_list_specifiers "$__format_calculate_length__visibles"
local __format_calculate_length__staticsLength=$(echo "${FormatListSpecifiers[@]}" | grep -oP '\d+' | awk '{s+=$0} END {print s}')
FormatCalculateLength=$((__format_calculate_length__literalsLength + __format_calculate_length__staticsLength))
}
format_autosize() {
# Treat horizontal tab as a specifier with a length of tab-length.
format_calculate_length "`echo "$1" | sed -r 's/\\\\t/%'"$FormatTabLength"'s/g'`"
# Exploit the fact the previous function just calculated FormatStripSpecifiers.
local __format_autosize__dynamics_count=$(echo "${FormatListSpecifiers[@]}" | grep -oP '%[\+-]?\.?\*[bqdiouxXfeEgGcsnaA]' | wc -l)
local __format_autosize__availableLength=$(( $(tput cols) - $FormatCalculateLength ))
local __format_autosize__dynamicsLength=$(( $__format_autosize__availableLength / $__format_autosize__dynamics_count ))
FormatAutosize="$1"
FormatAutosize=$(echo "$FormatAutosize" | sed -r 's/%\*s/%'"$__format_autosize__dynamicsLength"'s/g')
FormatAutosize=$(echo "$FormatAutosize" | sed -r 's/%\.\*s/%.'"$__format_autosize__dynamicsLength"'s/g')
FormatAutosize=$(echo "$FormatAutosize" | sed -r 's/%-\*s/%-'"$__format_autosize__dynamicsLength"'s/g')
FormatAutosize=$(echo "$FormatAutosize" | sed -r 's/%-\.\*s/%-.'"$__format_autosize__dynamicsLength"'s/g')
}
format_center() {
format_strip_invisibles "$1"
local __format_center__text_length=${#FormatStripInvisibles}
format_autosize "%*s%${__format_center__text_length}s%*s"
FormatCenter=$(printf "$FormatAutosize" "" "$1" "")
#FormatCenter=$(echo "$FormatCenter" | cut -d X -f 1)"$1"$(echo "$FormatCenter" | cut -d X -f 2)
}