diff --git a/fluxion.sh b/fluxion.sh index 58f318f..d08f2c8 100755 --- a/fluxion.sh +++ b/fluxion.sh @@ -224,11 +224,11 @@ trap handle_exit SIGINT SIGHUP # Design function fluxion_header() { - format_autosize "[%*s]\n" - local verticalBorder=$FormatAutosize + format_apply_autosize "[%*s]\n" + local verticalBorder=$FormatApplyAutosize - format_autosize "[%*s${CRed}FLUXION $FLUXIONVersion ${CRed}< F${CYel}luxion ${CRed}I${CYel}s ${CRed}T${CYel}he ${CRed}F${CYel}uture >%*s$CBlu]\n"; - local headerTextFormat="$FormatAutosize" + format_apply_autosize "[%*s${CRed}FLUXION $FLUXIONVersion ${CRed}< F${CYel}luxion ${CRed}I${CYel}s ${CRed}T${CYel}he ${CRed}F${CYel}uture >%*s$CBlu]\n"; + local headerTextFormat="$FormatApplyAutosize" conditional_clear @@ -507,8 +507,9 @@ function set_interface() { if [ $WIAvailableDataCount -eq 1 -a ${WIAvailableState[0]} = '+' ]; then WISelected="${WIAvailable[0]}" else + format_apply_autosize "$CRed[$CYel%1d$CRed]%b %-8b [%1s] %*.*s\n" io_query_format_fields "$FLUXIONVLine $FLUXIONInterfaceQuery" \ - "$CRed[$CYel%d$CRed]%b %-8b [%1s] %s\n" \ + "$FormatApplyAutosize" \ WIAvailableColor[@] WIAvailable[@] WIAvailableState[@] WIAvailableInfo[@] echo @@ -744,11 +745,11 @@ function set_target_ap() { local headerTitle=$(format_center_static "WIFI LIST"; echo -n "$FormatCenterStatic\n\n") - format_autosize "$CRed[$CYel * $CRed]$CClr %-*s %4s %3s %3s %2s %8s %18s\n" - local headerFields=$(printf "$FormatAutosize" "ESSID" "QLTY" "PWR" "STA" "CH" "SECURITY" "BSSID") + format_apply_autosize "$CRed[$CYel ** $CRed]$CClr %-*.*s %4s %3s %3s %2s %8.8s %18s\n" + local headerFields=$(printf "$FormatApplyAutosize" "ESSID" "QLTY" "PWR" "STA" "CH" "SECURITY" "BSSID") - format_autosize "$CRed[$CYel%03d$CRed]%b %-*s %3s%% %3s %3d %2s %8s %18s\n" - io_query_format_fields "$headerTitle$headerFields" "$FormatAutosize" \ + format_apply_autosize "$CRed[$CYel%03d$CRed]%b %-*.*s %3s%% %3s %3d %2s %8.8s %18s\n" + io_query_format_fields "$headerTitle$headerFields" "$FormatApplyAutosize" \ TargetAPCandidatesColor[@] \ TargetAPCandidatesESSID[@] \ TargetAPCandidatesQuality[@] \ @@ -778,11 +779,11 @@ function set_target_ap() { } function view_target_ap_info() { - format_autosize "%*s$CBlu%7s$CClr: %-32b%*s\n" + format_apply_autosize "%*s$CBlu%7s$CClr: %-32b%*s\n" - printf "$FormatAutosize" "" "ESSID" "$APTargetSSID / $APTargetEncryption" "" - printf "$FormatAutosize" "" "Channel" "$APTargetChannel" "" - printf "$FormatAutosize" "" "BSSID" "$APTargetMAC ($CYel${APTargetMaker:-UNKNOWN}$CClr)" "" + printf "$FormatApplyAutosize" "" "ESSID" "$APTargetSSID / $APTargetEncryption" "" + printf "$FormatApplyAutosize" "" "Channel" "$APTargetChannel" "" + printf "$FormatApplyAutosize" "" "BSSID" "$APTargetMAC ($CYel${APTargetMaker:-UNKNOWN}$CClr)" "" echo } diff --git a/lib/FormatUtils.sh b/lib/FormatUtils.sh index b77fcf1..d5239cf 100644 --- a/lib/FormatUtils.sh +++ b/lib/FormatUtils.sh @@ -1,74 +1,136 @@ #!/bin/bash FormatTabLength=8 +FormatValidSpecifiers='%([+-]?([0-9]+|\*)?(\.([0-9]+|\*))?)?[bqdiouxXfeEgGcsnaA]' # This should be relocated (here temporarily) tabs -$FormatTabLength # Set tab width to var -format_strip_invisibles() { - FormatStripInvisibles=$1 +#format_strip_specifiers() { +# FormatStripSpecifiers=$(echo "$1" | sed -r "s/$FormatValidSpecifiers//g") +#}= - # 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') +function format_strip_invisibles() { + # This function currently only strips the following: + # Color escape sequences, & control characters + FormatStripInvisibles=$(echo "$1" | sed -r 's/\\(e\[([0-9]*;?[0-9]+)m|(t|n))//g') } -format_strip_specifiers() { - FormatStripSpecifiers=$(echo "$1" | sed -r 's/%[\+-]?(([0-9]+|\*)|[0-9]*\.([0-9]+|\*))?[bqdiouxXfeEgGcsnaA]//g') +function format_expand_invisibles() { + FormatExpandInvisibles=$(echo "$1" | sed -r 's/\\(e\[([0-9]*;?[0-9]+)m|n)/%0s/g; s/\\t/%'"$FormatTabLength"'s/g') } -format_list_specifiers() { +function format_list_specifiers() { # Special specifier also included (with length value as '*'). - FormatListSpecifiers=($(echo "$1" | grep -oP '%[\+-]?(([0-9]+|\*)|[0-9]*\.([0-9]+|\*))?[bqdiouxXfeEgGcsnaA]')) + FormatListSpecifiers=($(echo "$1" | grep -oP "$FormatValidSpecifiers")) } -format_emulate_expansion() { -echo +# Statics are all specifiers with a fixed size +function format_calculate_statics_length() { + local __format_calculate_statics_length__specifiers=("${!2}") + + if [ ! "$2" ]; then + echo "format_calculate_statics_length missing \$2" + format_list_specifiers "$1" + __format_calculate_statics_length__specifiers=("${FormatListSpecifiers[@]}") + fi + FormatCalculateStaticsLength=$(echo "${__format_calculate_statics_length__specifiers[@]}" | sed -r 's/\.[0-9]+s/s/g' | grep -oP '\d+' | awk 'BEGIN {s=0} {s+=$0} END {print s}') } -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 +function format_calculate_literals_length() { + local __format_calculate_literals_length__normalizedFormat="`echo "$2" | sed -r 's/%%|\*\*/%1s/g'`" + local __format_calculate_literals_length__specifiers=("${!3}") - # Calculate number of all printable characters in format before substitution. - format_strip_specifiers "$__format_calculate_length__visibles" - local __format_calculate_length__literalsLength=${#FormatStripSpecifiers} + if [ ! "$2" ]; then + echo "format_calculate_literals_length missing \$2" + format_strip_invisibles "$1" + __format_calculate_literals_length__normalizedFormat="`echo "$FormatStripInvisibles" | sed -r 's/%%|\*\*/%1s/g'`" + fi - format_list_specifiers "$__format_calculate_length__visibles" + if [ ! "$3" ]; then + echo "format_calculate_literals_length missing \$3" + format_list_specifiers "$1" + __format_calculate_literals_length__specifiers=("${FormatListSpecifiers[@]}") + fi - 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)) + FormatCalculateLiteralsLength=$((${#__format_calculate_literals_length__normalizedFormat} - ($(echo "${__format_calculate_literals_length__specifiers[@]}" | wc -m) - ${#__format_calculate_literals_length__specifiers[@]}))) } -# TODO: This function's performance could sure use improvement... -# This is a semi-permanent fix, unless someone wants to tackle it... -format_autosize() { # Note that this does not yet support multiple lines (multiple \n). - # 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=$(echo "$1" | sed -r 's/%\*s/%'"$__format_autosize__dynamicsLength"'s/g' | \ - sed -r 's/%\.\*s/%.'"$__format_autosize__dynamicsLength"'s/g' | \ - sed -r 's/%-\*s/%-'"$__format_autosize__dynamicsLength"'s/g' | \ - sed -r 's/%-\.\*s/%-.'"$__format_autosize__dynamicsLength"'s/g') +function format_calculate_dynamics_length() { + local __format_calculate_dynamics_length__staticsLength=$2 + local __format_calculate_dynamics_length__literalsLength=$3 + + if [ ! "$2" ]; then + echo "format_calculate_dynamics_length missing \$2" + format_expand_invisibles "$1" + format_list_specifiers "$FormatExpandInvisibles" + format_calculate_statics_length X FormatListSpecifiers[@] + __format_calculate_dynamics_length__staticsLength=$FormatCalculateStaticsLength + fi + + if [ ! "$3" ]; then + if [ "$2" ]; then + format_expand_invisibles "$1" + format_list_specifiers "$FormatExpandInvisibles" + fi + echo "format_calculate_dynamics_length missing \$3" + format_calculate_literals_length X "$FormatExpandInvisibles" FormatListSpecifiers[@] + __format_calculate_dynamics_length__literalsLength=$FormatCalculateLiteralsLength + fi + + FormatCalculateDynamicsLength=$(( $(tput cols) - (__format_calculate_dynamics_length__staticsLength + __format_calculate_dynamics_length__literalsLength) )) } -format_center_static() { +function format_calculate_autosize_length() { + local __format_calculate_autosize_length__dynamicsLength=$2 + local __format_calculate_autosize_length__dynamicsCount=$3 + + if [ ! "$2" ]; then + format_expand_invisibles "$1" + format_list_specifiers "$FormatExpandInvisibles" + format_calculate_statics_length X FormatListSpecifiers[@] + format_calculate_literals_length X "$FormatExpandInvisibles" FormatListSpecifiers[@] + format_calculate_dynamics_length X "$FormatCalculateStaticsLength" "$FormatCalculateLiteralsLength" + __format_calculate_autosize_length__dynamicsLength=$FormatCalculateDynamicsLength + fi + + if [ ! "$3" ]; then + if [ "$2" ] + then format_list_specifiers "$1" + fi + # local __format_calculate_autosize_length__dynamics=("${FormatListSpecifiers[@]}") + # ("`echo "${FormatListSpecifiers[@]}" | awk '{ for(i = 1; i <= NF; i++) { if ($i !~ /[0-9]+/) print $i; } }'`") + __format_calculate_autosize_length__dynamicsCount=0 + for __format_calculate_autosize_length__specifier in "${FormatListSpecifiers[@]}"; do + if echo "$__format_calculate_autosize_length__specifier" | grep '\*' >/dev/null 2>&1; then + ((__format_calculate_autosize_length__dynamicsCount++)) + fi + done + fi + + if [ $__format_calculate_autosize_length__dynamicsCount -ne 0 -a \ + $__format_calculate_autosize_length__dynamicsLength -ge 0 ] + then FormatCalculateAutosizeLength=$(( __format_calculate_autosize_length__dynamicsLength / __format_calculate_autosize_length__dynamicsCount )) + else FormatCalculateAutosizeLength=0 + fi +} + +# Note that this does not yet support multiple lines (multiple \n). +function format_apply_autosize() { + format_calculate_autosize_length "$1" + FormatApplyAutosize=$(echo "$1" | sed -r 's/\*\.\*/'"$FormatCalculateAutosizeLength"'.'"$FormatCalculateAutosizeLength"'/g; s/(^|[^*])\*([^*]|$)/\1'"$FormatCalculateAutosizeLength"'\2/g; s/\*\*/*/g') +} + +function format_center_static() { format_strip_invisibles "$1" local __format_center_static__text_length=${#FormatStripInvisibles} - format_autosize "%*s%${__format_center_static__text_length}s%*s" - FormatCenterStatic=$(printf "$FormatAutosize" "" "$1" "") + format_apply_autosize "%*s%${__format_center_static__text_length}s%*s" + FormatCenterStatic=$(printf "$FormatApplyAutosize" "" "$1" "") } -format_center_dynamic() { +function format_center_dynamic() { format_calculate_length "$1" - format_autosize "%*s%${FormatCalculateLength}s%*s" + format_apply_autosize "%*s%${FormatCalculateLength}s%*s" # Temporary, I'll find a better solution later (too tired). - FormatCenterDynamic=$(printf "`echo "$FormatAutosize" | sed -r 's/%[0-9]+s/%s/2'`" "" "$1" "") + FormatCenterDynamic=$(printf "`echo "$FormatApplyAutosize" | sed -r 's/%[0-9]+s/%s/2'`" "" "$1" "") }