shell-base/modules/scripting/smenu.lib

70 lines
1.4 KiB
Bash

#!/bin/bash
SMENUDIR=$(dirname $BASH_SOURCE)
export SMENUDIR
smenu_fetch () {
local repo=https://github.com/p-gen/smenu.git
if git clone $repo /tmp/smenu; then
pushd /tmp/smenu || return 1
if bash ./build.sh; then
mv smenu "$SMENUDIR"
chmod +x "$SMENUDIR/smenu"
else
echo Error attempting to build smenu from the repo
return 1
fi
rm -rf /tmp/smenu
popd || return 1
else
echo Error unable to fetch repo at https://github.com/p-gen/smenu.git
return 1
fi
}
smenu () {
[[ ! $SMENUDIR ]] && echo unable to establish Bash Template directory && return 2
[[ ! -f "$SMENUDIR/smenu" ]] && smenu_fetch
if [[ -f "$SMENUDIR/smenu" ]]; then
[[ ! $@ ]] && set -- --help
$SMENUDIR/smenu "$@"
else
echo unable to fetch/build smenu, see $BASH_SOURCE
return 1
fi
}
confirm () {
local res
prompt=${1:-Please confirm your choice:}
res=$(
smenu -2 ^Y -1 ^N -3 ^C -s /^N -x cur 10 \
-m "${prompt}:" \
<<< "YES NO CANCEL"
)
case $res in
YES)
res=0 ;;
NO)
res=1 ;;
CANCEL)
res=2 ;;
*)
res=3 ;;
esac
return $res
}
smenu_test () {
echo "testing smenu using a simple confirmation"
confirm "Choose or abort and see return value"
echo $?
if confirm "Choose or abort and an if statement is evaluated"; then
echo "confirm returned 0/true (i.e. YES), proceed"
else
echo "confirm returned other than 0, i.e. false, do nothing"
fi
}