2 # Copyright (C) 2006 Andre Noll
3 # Licensed under the LGPL, version 3. See COPYING and COPYING.LESSER.
5 if [[ "$(type -t _gsu_setup)" != "function" ]]; then
6 gsu_dir=${gsu_dir:-${BASH_SOURCE[0]%/*}}
7 . "$gsu_dir/common" || exit 1
11 _gsu_node_name_pattern='[a-zA-Z_]'
18 gsu_msg "fatal: could not get terminal geometry"
33 1|255) ret=1;; # cancelled
35 result="dialog exit code $ec"
40 # Open a dialog box which asks the user to input a text
42 # Usage: gsu_input_box <text> <init>
44 # <text> is displayed above of the input field, which is is preset to <init>.
45 # The entered text is returned in $result. On success (user pressed OK)
46 # the function returns zero. If the user selected Cancel, the return value is
47 # one. On dialog errors, a negative error code is returned.
50 local g text="$1" init="$2"
54 result="$(dialog --inputbox "$text" $g "$init" 3>&1 1>&2 2>&3 3>&-)"
58 # Show the given file in a text box
60 # Usage: gsu_textbox <path>
62 # The box has an OK button which closes the box when activated.
72 dialog --textbox "$file" $g
76 # Show a message in a text box
78 # Usage: gsu_msgbox <text>
80 # This is like gsu_textbox() but the text is passed as a string.
85 # Some versions of dialog segfault if the text is too long. Hence we
86 # always use a temporary file.
87 gsu_make_tempfile 'gsu_msgbox.XXXXXXXXXX'
90 trap "rm -f $tmp" EXIT
93 rm -f "$tmp" # ignore errors
98 local header=$1 dflt_item=$2
104 result="$(dialog --no-lines --no-items \
105 --default-item "$dflt_item" \
106 --menu "$gsu_banner_txt"$'\n'"Current location: $header" \
107 $geom 16 "$@" 3>&1 1>&2 2>&3 3>&-)"
113 local tmp="${1%%$_gsu_node_name_pattern*}"
119 local tree="$1" root="${2%/}"
122 ret=-$E_GSU_MENU_TREE
123 result="subtree grep failed"
124 first="$(grep -n "$TAB\{1,\}$root/" <<< "$tree")" || return
125 [[ -z "$first" ]] && return
127 line_num="${first%%:*}"
128 _get_level "${first#*:}"
131 #echo "line: $line_num, root: $root, indent level: $level"
132 result="$(sed -e "1,${line_num}d;" <<< "$tree" \
133 | sed -e "/^$TAB\{1,$level\}$_gsu_node_name_pattern/,\$d" \
134 | sed -e "/^$TAB\{$((level + 2))\}/d")"
136 ret=-$E_GSU_MENU_TREE
137 result="sed command for subtree $root failed"
145 local tree="$1" TAB=' '
147 result="$(grep "^${TAB}${_gsu_node_name_pattern}" <<< "$tree")"
149 ret=-$E_GSU_MENU_TREE
150 result="root node grep failed"
158 local root_item=$1 tree=$2 subtree=$3
161 local old_root dflt_item
165 while read -a arr; do
166 ((${#arr[@]} == 0)) && continue
168 if ((${#arr[@]} > 1)); then
171 if [[ "${id:$((${#id} - 1)):1}" == '/' ]]; then
183 dflt_item=${items[0]}
185 _gsu_menu "$root_item" "$dflt_item" "${items[@]}"
186 ((ret < 0)) && return
187 [[ -z "$result" ]] && return # menu was cancelled
190 if [[ "${id:$((${#id} - 1)):1}" == '/' ]]; then
191 _get_subtree "$tree" "$id"
192 ((ret < 0)) && return
193 _browse "${dflt_item%/}" "$tree" "$result"
194 ((ret < 0)) && return
197 eval ${gsu_name}_${id}
203 local tree="$1" subtree
205 type -t dialog &> /dev/null
207 gsu_msg "dialog executable not found"
210 _get_root_nodes "$tree"
212 _browse 'Main menu' "$tree" "$subtree"