2017-08-12 14:43:39 -06:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
FormatTabLength=8
|
2017-08-14 16:13:47 -06:00
|
|
|
FormatValidSpecifiers='%([+-]?([0-9]+|\*)?(\.([0-9]+|\*))?)?[bqdiouxXfeEgGcsnaA]'
|
2017-08-12 14:43:39 -06:00
|
|
|
|
|
|
|
# This should be relocated (here temporarily)
|
|
|
|
tabs -$FormatTabLength # Set tab width to var
|
|
|
|
|
2017-08-14 19:28:37 -06:00
|
|
|
# This function strips (some) invisible characters.
|
|
|
|
# It only strips those needed by fluxion, currently.
|
|
|
|
# Parameters: $1 - format
|
2017-08-14 16:13:47 -06:00
|
|
|
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')
|
2017-08-12 14:43:39 -06:00
|
|
|
}
|
|
|
|
|
2017-08-14 19:28:37 -06:00
|
|
|
# This function replaces all invisible characters
|
|
|
|
# with a specifier of their corresponding length.
|
|
|
|
# Parameters: $1 - format
|
2017-08-14 16:13:47 -06:00
|
|
|
function format_expand_invisibles() {
|
|
|
|
FormatExpandInvisibles=$(echo "$1" | sed -r 's/\\(e\[([0-9]*;?[0-9]+)m|n)/%0s/g; s/\\t/%'"$FormatTabLength"'s/g')
|
2017-08-12 14:43:39 -06:00
|
|
|
}
|
|
|
|
|
2017-08-14 19:28:37 -06:00
|
|
|
# This function lists all operators in format.
|
|
|
|
# Parameters: $1 - format
|
2017-08-14 16:13:47 -06:00
|
|
|
function format_list_specifiers() {
|
2017-08-12 14:43:39 -06:00
|
|
|
# Special specifier also included (with length value as '*').
|
2017-08-14 16:13:47 -06:00
|
|
|
FormatListSpecifiers=($(echo "$1" | grep -oP "$FormatValidSpecifiers"))
|
2017-08-12 14:43:39 -06:00
|
|
|
}
|
|
|
|
|
2017-08-14 19:28:37 -06:00
|
|
|
|
|
|
|
# This function calculates total length of statics in format.
|
|
|
|
# Statics are all specifiers in format with a fixed size.
|
|
|
|
# Parameters: $1 - format [$2 - specifier array]
|
2017-08-14 16:13:47 -06:00
|
|
|
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}')
|
2017-08-12 14:43:39 -06:00
|
|
|
}
|
|
|
|
|
2017-08-14 19:28:37 -06:00
|
|
|
# This function calculates total length of literals in format.
|
|
|
|
# Literals are all characters in format printed literally.
|
|
|
|
# Parameters: $1 - format [$2 - processed format [$3 - specifier array]]
|
2017-08-14 16:13:47 -06:00
|
|
|
function format_calculate_literals_length() {
|
2017-08-14 17:23:50 -06:00
|
|
|
local __format_calculate_literals_length__normalizedFormat="`echo "$2" | sed -r 's/%%|\*\*/X/g'`"
|
2017-08-14 16:13:47 -06:00
|
|
|
local __format_calculate_literals_length__specifiers=("${!3}")
|
2017-08-12 14:43:39 -06:00
|
|
|
|
2017-08-14 16:13:47 -06:00
|
|
|
if [ ! "$2" ]; then
|
|
|
|
echo "format_calculate_literals_length missing \$2"
|
|
|
|
format_strip_invisibles "$1"
|
2017-08-14 17:23:50 -06:00
|
|
|
__format_calculate_literals_length__normalizedFormat="`echo "$FormatStripInvisibles" | sed -r 's/%%|\*\*/X/g'`"
|
2017-08-14 16:13:47 -06:00
|
|
|
fi
|
2017-08-12 14:43:39 -06:00
|
|
|
|
2017-08-14 16:13:47 -06:00
|
|
|
if [ ! "$3" ]; then
|
|
|
|
echo "format_calculate_literals_length missing \$3"
|
|
|
|
format_list_specifiers "$1"
|
|
|
|
__format_calculate_literals_length__specifiers=("${FormatListSpecifiers[@]}")
|
|
|
|
fi
|
2017-08-12 14:43:39 -06:00
|
|
|
|
2017-08-14 16:13:47 -06:00
|
|
|
FormatCalculateLiteralsLength=$((${#__format_calculate_literals_length__normalizedFormat} - ($(echo "${__format_calculate_literals_length__specifiers[@]}" | wc -m) - ${#__format_calculate_literals_length__specifiers[@]})))
|
|
|
|
}
|
|
|
|
|
2017-08-14 19:28:37 -06:00
|
|
|
# This function calculates total length of dynamics in format.
|
|
|
|
# Dynamics are all asterisk-containing specifiers in format.
|
|
|
|
# Parameters: $1 - format [$2 - statics length [$3 - literals length]]
|
2017-08-14 16:13:47 -06:00
|
|
|
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) ))
|
|
|
|
}
|
2017-08-12 14:43:39 -06:00
|
|
|
|
2017-08-14 19:28:37 -06:00
|
|
|
function format_calculate_length() {
|
|
|
|
local __format_calculate_length__staticsLength=$2
|
|
|
|
local __format_calculate_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_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_length__literalsLength=$FormatCalculateLiteralsLength
|
|
|
|
fi
|
|
|
|
|
|
|
|
FormatCalculateLength=$(( __format_calculate_length__staticsLength + __format_calculate_length__literalsLength ))
|
|
|
|
}
|
|
|
|
|
|
|
|
# This function calculates the size of individual dynamics in format.
|
|
|
|
# Parameters: $1 - format [$2 - dynamics length [$3 - dynamics count]]
|
2017-08-14 16:13:47 -06:00
|
|
|
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
|
2017-08-12 14:43:39 -06:00
|
|
|
}
|
|
|
|
|
2017-08-14 19:28:37 -06:00
|
|
|
|
|
|
|
# This function replaces dynamics' asterisks with their length, in format.
|
|
|
|
# Parameters: $1 - format
|
2017-08-14 16:13:47 -06:00
|
|
|
# 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')
|
2017-08-12 14:43:39 -06:00
|
|
|
}
|
|
|
|
|
2017-08-14 19:28:37 -06:00
|
|
|
# This function centers literal text.
|
|
|
|
# Parameters: $1 - literals
|
|
|
|
function format_center_literals() {
|
2017-08-12 14:43:39 -06:00
|
|
|
format_strip_invisibles "$1"
|
2017-08-14 19:28:37 -06:00
|
|
|
local __format_center_literals__text_length=${#FormatStripInvisibles}
|
|
|
|
format_apply_autosize "%*s%${__format_center_literals__text_length}s%*s"
|
|
|
|
FormatCenterLiterals=$(printf "$FormatApplyAutosize" "" "$1" "")
|
2017-08-13 03:24:18 -06:00
|
|
|
}
|
|
|
|
|
2017-08-14 19:28:37 -06:00
|
|
|
# This function centers statics in format.
|
|
|
|
# Parameters: $1 - format
|
2017-08-14 16:13:47 -06:00
|
|
|
function format_center_dynamic() {
|
2017-08-13 03:24:18 -06:00
|
|
|
format_calculate_length "$1"
|
2017-08-14 16:13:47 -06:00
|
|
|
format_apply_autosize "%*s%${FormatCalculateLength}s%*s"
|
2017-08-13 03:24:18 -06:00
|
|
|
# Temporary, I'll find a better solution later (too tired).
|
2017-08-14 16:13:47 -06:00
|
|
|
FormatCenterDynamic=$(printf "`echo "$FormatApplyAutosize" | sed -r 's/%[0-9]+s/%s/2'`" "" "$1" "")
|
2017-08-12 14:43:39 -06:00
|
|
|
}
|