What i wanted is CLI function to act something like separate program in a sense, for example like zenity behaves, but cli based in it’s interface and pure sh / bash…
If i’d describe it as human with words when i write that part:
local result=$(CLI "list" some_data)
I’d like to get:
usual output of the function
0 1.7.17 We
1 2.11 Are
2 3.1 Really
3 1.7.31 Doomed
->
When selected by user input i’d like to return value of:
Being simpleminded, and not too much of a basher, I’d just find a way to do both - once enclosed in backticks for variable assignment. No idea if that does what you want, though
Check this out, if you diff that with OP and run both side by side - you’ll get what i mean:
#!/bin/bash
title=$(basename "$0")
echo -n -e "\033]0;$title\007"
# Force output in english
export LC_ALL=en_US.UTF-8
readarray some_data << EOF
0 1.7.17 We
1 2.11 Are
2 3.1 Really
3 1.7.31 Doomed
EOF
main() {
echo
echo " -------------------- Select -------------------- "
echo
CLI "list" some_data
local result="${CLI_RESULT}"
[[ "${result}" != "" ]] && echo "${result}" || echo "Nothing selected"
}
CLI() {
unset CLI_RESULT
local mode=$1
local -n args="$2"
case "$mode" in
list)
local length=${#args[@]}
for (( i=0; i < ${length}; i++ ));
do
echo "${args[i]}"
done | column -t -s " " -R "1"
echo
local input
while true;
do
[ -n "$input" ] || read -r -p "-> " input
if [[ "${input}" =~ ^[0-9]+$ ]] && [ "${input}" -lt "${length}" ];
then
local choice="${args[${input}]}"
CLI_RESULT=$(echo "${choice}" | cut -d " " -f1)
break
else
local input=""
echo "Wrong option"
fi
done
;;
esac
}
main "$@"
This is pretty much exactly how i wanted it to work actually…But i thought maybe there is some more elegant method of doing so, than using global variable CLI_RESULT?
Would be cool to be able to get exactly same output as in that post, but with syntax of just assigning to variable:
local result=$(CLI "list" some_data)
Not sure if it’s possible at all
Very likely, i’m just not sure exactly how in that case