-
Notifications
You must be signed in to change notification settings - Fork 7
/
yad-includes
117 lines (110 loc) · 3.92 KB
/
yad-includes
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# yad-includes - Variables and functions for yad in scripts
# Copyright (C) 2015-2020 John Crawley <[email protected]>
# Copyright (C) 2016 xaos52 <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# These functions can be used in user scripts too, by first sourcing this file, eg:
# BL_COMMON_LIBDIR='/usr/lib/bunsen/common'
# if ! . "$BL_COMMON_LIBDIR/yad-includes" 2> /dev/null; then
# echo $"Error: Failed to locate yad-includes in $BL_COMMON_LIBDIR" >&2
# exit 1
# fi
# NOTE: Do not source this file in non-Bash shell scripts.
# It uses some "bashisms".
# These are global options for all the yad functions defined below.
# You can add to them for the duration of your script with something like:
# yad_common_args+=(--image=debian)
# Override options already set with, eg:
# yad_common_args+=(--borders=30)
# Unset options with 0, eg:
# yad_common_args+=(--center=0)
yad_common_args=('--window-icon=distributor-logo-bunsenlabs' '--center' '--borders=20')
yad_popup_args=('--undecorated' '--fixed' '--on-top')
# Call these functions as shorthand for simple popups etc.
# Append any options you want to add, change or unset.
# Plain strings will be regarded as --text eg:
# yad_info "It's not as bad as you think."
# Buttons are identified by their return values
# so their labels can be changed, eg:
# yad_error 'Something went wrong.' --button='Resigned acceptance':0
# New buttons will be added on the right, eg:
# yad_question 'Continue?' --button='Tell me more':2
yad_question(){
local yadargs=()
_yad_setargs '--button=Yes:0' '--button=No:1' "$@"
yad "${yadargs[@]}"
}
yad_info(){
local yadargs=()
_yad_setargs '--button=OK:0' "$@"
yad "${yadargs[@]}"
}
yad_error(){
local yadargs=()
_yad_setargs '--button=OK:0' '--image=dialog-error' "$@"
yad "${yadargs[@]}"
}
############################################################################
# DO NOT call this function directly.
# It is used by the yad functions above.
_yad_setargs(){
# Catch direct calls
case "${FUNCNAME[1]}" in
yad_question|yad_info|yad_error)
:;;
*)
echo "$0:${FUNCNAME[0]}: this function is only to be called by yad_question, yad_info or yad_error."
return 1
;;
esac
local i key value
local button_with_id_pattern='^(.*):(.+)$'
declare -A argsetter
local argsetter=()
local button_ids=()
for i in "${yad_common_args[@]}" "${yad_popup_args[@]}" "$@"
do
[[ $i = --* ]] || {
argsetter[--text]="$i"
continue
}
IFS='=' read -r key value <<<"$i"
[[ "$key" = --button && "$value" =~ $button_with_id_pattern ]] && {
key=--button"${BASH_REMATCH[2]}"
button_ids+=("$key")
}
[[ $value = 0 ]] && {
[[ ${argsetter["$key"]+X} ]] && unset "argsetter[$key]"
continue
}
argsetter[$key]="$value"
done
i=
for i in "${!argsetter[@]}"
do
[[ "$i" = --button[0-9]* ]] && continue
if [[ ${argsetter[$i]} ]]
then
yadargs+=("$i=${argsetter[$i]}")
else
yadargs+=("$i")
fi
done
i=
for i in "${button_ids[@]}"
do
[[ ${argsetter[$i]} ]] && yadargs+=("--button=${argsetter[$i]}")
unset "argsetter[$i]"
done
}