X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=opt%2Fbin%2Fchoice;fp=opt%2Fbin%2Fchoice;h=8f80781d67bc33846dbd28a6c0192052e4421f38;hb=819bb88d5a014a23150b8fd609d194e883958674;hp=0000000000000000000000000000000000000000;hpb=596624f4629703d95ef6c0d10efba2d2c36cb7c5;p=profile.git diff --git a/opt/bin/choice b/opt/bin/choice new file mode 100755 index 0000000..8f80781 --- /dev/null +++ b/opt/bin/choice @@ -0,0 +1,42 @@ +#!/bin/bash +# +# choice: Display an X dialogue to allow a choice. +# Usage: choice [-t ] : [: ...] +# Example: choice "File exists" "Replace:1" "Back up:2" "No action:0" +# + +function usage() { + echo >&2 "Usage: choice [-t ] : [: ...]" + exit 100 +} + +while getopts ":t:" arg; do + case $arg in + t) timeout="$OPTARG";; + *) usage;; + esac +done +shift $((OPTIND-1)) + +description="$1"; shift +[ -z "$description" ] && usage +for i in ${1+"$@"}; do + [ "${i/:/}" = "$i" ] && usage +done + +args= +if which zenity 2>/dev/null | grep ^/ &>/dev/null; then + [ ! -z "$timeout" ] && timeout="--timeout=$timeout" + for i in ${1+"$@"}; do + args="$args \"${i/:/\" }" + done + ret=$(eval zenity --list --text="\"$description\"" --column=Choice --column=hidden --hide-column=2 --print-column=2 $args $timeout) + exit $ret +else + [ ! -z "$timeout" ] && timeout="-timeout $timeout" + for i in ${1+"$@"}; do + args="$args,\"$i\"" + done + args=${args/,/} + eval exec xmessage -center $timeout -buttons $args "$description" +fi