Hello Coders
Some time ago i was asking about python and bash performances, and i was told i could post the regarding code, and someone would kindly help to make it faster (if possible).
If you have noted, i'm on the way to finalize, finish, stable TUI - Text(ual) User Interface.
It is a framework, aiming to be of help when trying to achieve an Interface on Text level, so it is ment to be used by Scripts, to make it easier for the actual end-user.
This said, the complete package -> all required files are located at: GitHub - sri-arjuna/tui: A line based Text User Interface framework for scripts
Note that it is most optimized for redhad based systems, if you have issues on your system, please let me know about it - and its solution, so i can fix this for others/later.
However, i already have had rewrote it in the aim for performance gain, i'll only include THAT file, representing the core-key-functions for all the presentations done within that package.
The (my) 'logic' behind this is like: printf
Print the information as required, 1 to 3 strings, left, center or right aligned, depending upon arguments, it'll get colored, and depening upon string lengths, even become multilined (not wanted, but required).
Functions are cool, but files/applications are cooler, but took me quite a while to actualy get there -> 2 years...
And i' do like to play with self-supporting-solving-scripts.
So like in most temals, printf is available, and prints on the same line until the line is full.
For my package TUI, tui-printf is the core component for all displaying tasks.
Therefor, it must not only handle several calls, while staying on the very same console line (where possible -> string lenghts), it also must provide color and newlines upon other calls, furthhermore, if the string lenghts provided are too long to be displayed on the current terminal line (whether gui-window or virtual-terminal) it must 'split' them first by arguments-locations (left,cetner,right) then by cutting the actual lenght of a single/certain string, using itself to solve this task.
As a hobby-auto-didactic-learning-kind-of-lazy-guy, i've reached the limits of my brain at this stage for this task (performance).
After this much talking, i'm still not posting the script, as you need to know HOW the script is retrieving its data....
By ENV - so most of the variables required by this TUI-package, are loaded by /etc/profile.d/tui.sh, which not only sources the systemwide configuration in /etc/tui/files, but also in the custom files within $HOME/.config/tui/*.
That way, the ENV is 'filled' with variables(19) like (but not limited to): $DONE
(string: [ DONE ]
in text, OR [ ]
in GUI.
I'm aware that with the use of tui-status, i could also make another script, returning such similar strings, in which case, in turn, some RET_XY variables would need to be shared by env, which is penetrant within my mind, beeing aware of that, i'm open for suggestions, other than to rewrite it completly to (#)C(++), Python, or whatever.
Finaly, the script, with the most important question:
Do you see anything to increase performance?
File: tui-printf -- Actualy - please see https://github.com/sri-arjuna/tui/blob/master/bin/tui-printf for a readable structure....
#!/bin/bash
#
# Author: Simon Arjuna Erat (sea)
# Contact: erat.simon@gmail.com
# License: GNU Lesser General Public License (LGPL)
# Created: 2013.05.03
# Changed: 2014.06.10
script_version=0.9
# Description: Descriptional text goes here
#
# Variables
#
#source tui
[ -z "$BORDER_LEFT$HEAD_BG" ] && source /etc/profile.d/tui.sh
TITLE="tui-printf"
ME="${0##*/}"
ME_DIR="${0/${0##/*/}}"
help_text="
$ME ($script_version)
$TITLE
Usage: $ME [options] [arguments]
$ME Prints up to 3 arguments.
1: Left
2: Left, Right
3: Left, Center, Right
Arguments will tell $ME if and where to colorize, or if to do a linebreak.
Options are:
-h(elp) This screen
-E(cho) Simulates 'echo', prints up to 3 strings and a newline.
-T(itle) Prints 1st argument centred, if more - aligment like tui-echo
The inner background is colored white with blue font by default
-H(header) Full line has a blue background and white font as default color
"
FIRST=""
SECOND=""
THIRD=""
WIDTH=$( [ -z $COLUMNS ] && tput cols || printf $COLUMNS ) # Retrieve env or calc it new
TOTAL="$WIDTH"
WIDTH=$[ $WIDTH - ${#BORDER_LEFT} - ${#BORDER_RIGHT} - 2 ] # Subtract default 'borders'
WIDTH=$[ $WIDTH / 2 * 2 ] # Make it even
EMPTY="$(printf '%*s' $WIDTH)" # Get 'width' spaces
doEcho=false
doHeader=false
doTitle=false
optStyle=""
#
# Catching Arguments
#
#[[ -z $1 ]] && printf "$help_text" && exit $RET_HELP
while getopts "EHTh?" name
do case $name in
E|echo) doEcho=true
optStyle="-E" ;;
H|header) doHeader=true
optStyle="-H" ;;
T|title) doTitle=true
optStyle="-T" ;;
h|"?"|*) printf "$help_text"
exit $RET_HELP
;;
esac
done
shift $(($OPTIND - 1))
ARGS=(${*}) # Remaining arguments | These two fail on spaced-strings
ARGS_COUNT_org=${#ARGS[@]} # Amount of remaining
#FIRST="${ARGS[0]}"
#SECOND="${ARGS[1]}"
#THIRD="${ARGS[2]}"
ARGS_COUNT=0
[ ! -z "$1" ] && FIRST="$1" && ((ARGS_COUNT++))
[ ! -z "$2" ] && SECOND="$2" && ((ARGS_COUNT++))
[ ! -z "$3" ] && THIRD="$3" && ((ARGS_COUNT++))
[ ! -z "$4" ] && doMore=true || doMore=false
#
# Verify all fit on 1 line
#
len=$[ ${#FIRST} + ${#SECOND} + ${#THIRD} ]
if [[ $len -gt $WIDTH ]]
then len2=$[ ${#FIRST} + ${#SECOND} ]
#echo $len $len2 $WIDTH
if [[ $len2 -le $WIDTH ]]
then # first & second match on one line
tui-printf -E "$FIRST" "$SECOND"
tui-printf $optStyle "$THIRD"
else # it needs further spliting:
if [[ ${#FIRST} -le $WIDTH ]]
then tui-printf -E "$FIRST"
tui-printf $optStyle "$SECOND" "$THIRD"
else half=$[ ${#FIRST} / 2 ]
part1="${FIRST:0:$half}"
part2="${FIRST:$half}"
tui-printf -E "$part1"
[[ -z $SECOND ]] && \
tui-printf -E "$part2" || \
tui-printf -E "$part2" "$SECOND"
[[ -z $THIRD ]] || \
tui-printf $optStyle "$THIRD"
fi
fi
# Since this is special handling,
# calling itself with reduced string lengths
# so we can exit after this is done
exit
# else # just continue with the script below
fi
#
# Prepare Colors & Strings
#
if [ $doEcho = true ]
then COLOR_LINE_START="\r${TUI_RESET}"
COLOR_LINE_IDENT=""
COLOR_LINE_CLOSE=""
COLOR_LINE_END="\n"
elif [ $doHeader = true ]
then # Full line
COLOR_LINE_START="\r${HEAD_BG}${HEAD_FG}"
COLOR_LINE_IDENT=""
COLOR_LINE_CLOSE=""
COLOR_LINE_END="${TUI_RESET}\n"
elif [ $doTitle = true ]
then # 'Inlay'
COLOR_LINE_START="\r"
COLOR_LINE_IDENT="${TITLE_FG}${TITLE_BG}"
COLOR_LINE_CLOSE="${TUI_RESET}"
COLOR_LINE_END="\n"
else # The default
COLOR_LINE_START="\r${TUI_RESET}"
COLOR_LINE_IDENT=""
COLOR_LINE_CLOSE=""
COLOR_LINE_END=""
fi
case $ARGS_COUNT in
0) FIRST="$EMPTY"
;;
1) if [ $doTitle = true ]
then SECOND="$FIRST"
num_o="${#SECOND}"
num=$[ $num_o / 2 * 2 ]
[ $num_o -gt $num ] && adder=" "||adder=" "
EMPTY="${EMPTY:${num}+2}" # The +2 is a temp bugfix
half=$[ ${#EMPTY} / 2 ]
FIRST="${EMPTY:$half}"
THIRD="$FIRST$adder"
else # Usual arangement
SECOND=$"${EMPTY:${#FIRST}}"
fi
;;
2) THIRD="$SECOND"
SECOND="${EMPTY:${#FIRST}+${#SECOND}}"
;;
*) # 3 (BETA - and more - handler)
EMPTY="${EMPTY:${#FIRST}+${#SECOND}+${#THIRD}}"
half=$[ ${#EMPTY} / 2 ]
len_strings=$[ ${#FIRST} + ${#SECOND} + ${#THIRD} ]
len_compare=$[ $len_strings / 2 * 2 ]
[ $len_compare -lt $len_strings ] && \
adder=1 || \
adder=0
SECOND="${EMPTY:$half}$SECOND${EMPTY:$half+$adder}"
;;
esac
#
# Display & Action
#
printf "${COLOR_LINE_START}${BORDER_LEFT}${COLOR_LINE_IDENT} ${FIRST}${SECOND}${THIRD} ${COLOR_LINE_CLOSE}${BORDER_RIGHT}${COLOR_LINE_END}"
#
# BETA "if more"
#
#unset ARGS[0] ARGS[1] ARGS[2]
if [ $doMore = true ]
then # there are remains..
C=0
for A in "${ARGS[*]}";do
[ "$1" = "$A" ] && unset ARGS[$C] && echo $A
[ "$2" = "$A" ] && unset ARGS[$C] && echo $A
[ "$3" = "$A" ] && unset ARGS[$C] && echo $A
((C++))
done
tui-title "DEBUG"
tui-echo "Too many arguments:" "$ARGS_COUNT($ARGS_COUNT_org)=${ARGS[*]}"
# DEBUG
echo $ME $optStyle "${ARGS[*]}"
exit
fi
Please understand, i've had no understand about lincenses, and still its a jungle to me.. .it'll change, i'm just not sure to what - atm...
I'm aiming to be as FOSS as possible, so i was told GPLv3 was the way to go/change?
Thank you in advance for any constructive input.