Unicode stuff.
[profile.git] / opt / bin / choice
1 #!/bin/bash
2 #
3 # choice: Display an X dialogue to allow a choice.
4 # Usage: choice [-t <timeout>] <description> <text>:<ret> [<text>:<ret> ...]
5 # Example: choice "File exists" "Replace:1" "Back up:2" "No action:0"
6 #
7
8 function usage() {
9   echo >&2 "Usage: choice [-t <timeout>] <description> <text>:<ret> [<text>:<ret> ...]"
10   exit 100
11 }
12
13 while getopts ":t:" arg; do
14   case $arg in
15     t) timeout="$OPTARG";;
16     *) usage;;
17   esac
18 done
19 shift $((OPTIND-1))
20
21 description="$1"; shift
22 [ -z "$description" ] && usage
23 for i in ${1+"$@"}; do
24   [ "${i/:/}" = "$i" ] && usage
25 done
26
27 args=
28 if which zenity 2>/dev/null | grep ^/ &>/dev/null; then
29   [ ! -z "$timeout" ] && timeout="--timeout=$timeout"
30   for i in ${1+"$@"}; do
31     args="$args \"${i/:/\" }"
32   done
33   ret=$(eval zenity --list --text="\"$description\"" --column=Choice --column=hidden --hide-column=2 --print-column=2 $args $timeout)
34   exit $ret
35 else
36   [ ! -z "$timeout" ] && timeout="-timeout $timeout"
37   for i in ${1+"$@"}; do
38     args="$args,\"$i\""
39   done
40   args=${args/,/}
41   eval exec xmessage -center $timeout -buttons $args "$description"
42 fi