Skip to content

Instantly share code, notes, and snippets.

@dkebler
Last active August 6, 2024 18:13
Show Gist options
  • Save dkebler/fe40cd5ad14f54807c6a975be1d0a1da to your computer and use it in GitHub Desktop.
Save dkebler/fe40cd5ad14f54807c6a975be1d0a1da to your computer and use it in GitHub Desktop.
A library script to help get and run smenu https://github.com/p-gen/smenu/discussions/44
#!/bin/bash
# A library script to help get and run smenu
# https://gist.github.com/dkebler/fe40cd5ad14f54807c6a975be1d0a1da
# https://github.com/p-gen/smenu
# https://github.com/p-gen/smenu/discussions/44
# source it and then have fun.
# I put in a function `smenu_test`.
# When you run it the first time it will pull,build and put smenu in same directory as this script
# assuming you are on linux/mac that has a /tmp directory and have build-essential or base-devel or the like installed.
# smenu the function will call smenu the binary so no need to put it your PATH.
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
}
@p-gen
Copy link

p-gen commented Aug 6, 2024

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment