cli-games/game_functions.sh

96 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
# Get the coluns and lines of the "screen"
cols=$(tput cols)
lines=$(tput lines)
# Settings to improve accessibility of dialog.
export DIALOGOPTS='--insecure --no-lines --visit-items'
inputbox() {
# Returns: text entered by the user
# Args 1, Instructions for box.
# args: 2 initial text (optional)
dialog --backtitle "$(gettext "Enter text and press enter.")" \
--clear \
--inputbox "$1" 0 0 "$2" --stdout
}
msgbox() {
# Returns: None
# Shows the provided message on the screen with an ok button.
dialog --clear --msgbox "$*" 10 72
}
infobox() {
# Returns: None
# Shows the provided message on the screen with no buttons.
local timeout=3
dialog --infobox "$*" 0 0
read -n1 -t $timeout continue
# Clear any keypresses from the buffer
clear_buffer
}
yesno() {
# Returns: Yes or No
# Args: Question to user.
# Called in if $(yesno) == "Yes"
# Or variable=$(yesno)
if dialog --clear --backtitle "$(gettext "Press 'Enter' for \"yes\" or 'Escape' for \"no\".")" --yesno "$*" 0 0 ; then
echo "Yes"
else
echo "No"
fi
}
get_keypress() {
# Returnes the pressed key.
# There arre two ways to use this function.
# first way, get_keypress variableName
# Second way variableName="$(get_keypress)"
# This variable name is long to absolutely minimize possibility of collision.
local getKeypressFunctionReturnVariable=$1
local returnedKeypress
# Unset IFS to capture any key that is pressed.
local ifs="$IFS"
unset IFS
read -sn1 returnedKeypress
# Restore IFS
IFS="$ifs"
if [[ $getKeypressFunctionReturnVariable ]]; then
eval $getKeypressFunctionReturnVariable="'$returnedKeypress'"
else
echo "$returnedKeypress"
fi
}
menulist() {
# Args: menu options
# returns: selected option
# set gameMenu to control the message.
declare -a menuList
for i in $@ ; do
menuList+=("$i" "$i")
done
dialog --backtitle "${menuMessage:-Game menu...}" \
--clear \
--no-tags \
--menu "Please make your selection" 0 0 0 "${menuList[@]}" --stdout
}
numpicker() {
# Args: max number, Min numberr optional.
# returns: selected number
# set gameMenu to control the message.
declare -a menuList
local max=$1
local min=${2:-10}
for i in $(seq $min $max) ; do
menuList+=("$i" "$i")
done
dialog --backtitle "${menuMessage:-Numeric menu...}" \
--clear \
--no-tags \
--menu "Please select a number between $min and $max." 0 0 0 "${menuList[@]}" --stdout
}