aboutsummaryrefslogtreecommitdiffstats
path: root/Tools/scripts/dialogwrapper/dialogwrapper.sh
blob: 2e8b4bbb363d846e8c2fcfbb647ae99356c855e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/sh

# $FreeBSD$

# dialog wrapper script

AWK="/usr/bin/awk"
DIALOG="/usr/bin/dialog"
ECHO="/bin/echo"
SED="/usr/bin/sed"
STTY="/bin/stty"

# get terminal size
size=$( ${STTY} size )
visheight="${size%%[$IFS]*}"
visheight=$(($visheight-3))
listheight=$(($visheight-6))
viswidth="${size#*[$IFS]}"
viswidth=$(($viswidth-10))
descwidth=$(($viswidth-22))

# test for a minimum version of dialog(1)
DIALOG_VER="0"
DIALOG_MIN="1.1-20100428"
dialogout=$( ${DIALOG} --print-version 2>&1 )
if [ $? -eq 0 ]; then
  DIALOG_VER=$( ${ECHO} "$dialogout" | ${SED} -e 's/^[^0-9]*//' )
  # only newer versions of dialog have --item-help
  HAS_ITEM_HELP="1"
fi

# escape the menu title
TITLE=$( ${AWK} -v title="$2" \
    'BEGIN { gsub(/'\''/, "'\''\\'\'\''", title); print title }' )

cmdstr=""
[ "${HAS_ITEM_HELP}" ] && cmdstr="--item-help"
cmdstr="$cmdstr $1 '$2' $visheight $viswidth $listheight"
shift 5

menulist=$(
  varlist=
  _maxvarlen=0
  while [ $# -gt 0 ]; do
    var="$1"
    [ ${#var} -gt $_maxvarlen ] && export _maxvarlen=${#var}
    varlist="$varlist${varlist:+
}$var"
    # build hashed environment variables
    export _${var}_desc="$2"
    export _${var}_val="$3"
    shift 3
  done
  ${ECHO} "$varlist" \
  | ${AWK} -v hasitemhelp="${HAS_ITEM_HELP}" -v viswid="$viswidth" '
  {
    var  = $1
    desc = ENVIRON["_" var "_desc"]
    val  = ENVIRON["_" var "_val"]
    descwid = viswid -(ENVIRON["_maxvarlen"] + 12)
    extdesc = ""
    if ( length(desc) > descwid ) {
      extdesc = substr(desc, descwid)
      gsub(/'\''/, "'\''\\'\'\''", extdesc)
      desc = substr(desc, 1, descwid - 1) "+"
    }
    gsub(/'\''/, "'\''\\'\'\''", desc)
    printf "'\''%s'\'' '\''%s'\'' '\''%s'\''", var, desc, val
    if ( hasitemhelp ) printf " '\''%s'\''", extdesc
    printf "\n"
  } '
)

eval ${DIALOG} $cmdstr $menulist
status="$?"
echo
exit $status