-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathinputs.sh
More file actions
216 lines (199 loc) · 6.64 KB
/
inputs.sh
File metadata and controls
216 lines (199 loc) · 6.64 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env bash
# Multi-UI Scripting - Input Functions
# https://github.com/lunarcloud/script-dialog
# LGPL-2.1 license
# Variables set in init.sh and used here
# shellcheck disable=SC2154
#######################################
# Display a text input box
# GLOBALS:
# GUI_ICON
# GUI_TITLE
# SYMBOL
# QUESTION_SYMBOL
# TEST_STRING
# INTERFACE
# RECMD_LINES
# RECMD_COLS
# APP_NAME
# ACTIVITY
# ZENITY_ICON_ARG
# ZENITY_HEIGHT (optional)
# ZENITY_WIDTH (optional)
# ARGUMENTS:
# The text to display
# The initial input value
# OUTPUTS:
# the entered text
# RETURN:
# 0 if success, non-zero otherwise.
#######################################
function inputbox() {
if [ -z ${GUI_ICON+x} ]; then
GUI_ICON=$XDG_ICO_QUESTION
fi
if [ -z ${SYMBOL+x} ]; then
local SYMBOL=$QUESTION_SYMBOL
fi
_calculate-gui-title
TEST_STRING="${QUESTION_SYMBOL} $1"
_calculate-tui-size
local exit_status=0
if [ "$INTERFACE" == "whiptail" ]; then
INPUT=$(whiptail --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --inputbox "${SYMBOL} $1" "$RECMD_LINES" "$RECMD_COLS" "$2" 3>&1 1>&2 2>&3)
exit_status=$?
elif [ "$INTERFACE" == "dialog" ]; then
INPUT=$(dialog --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --inputbox "${SYMBOL} $1" "$RECMD_LINES" "$RECMD_COLS" "$2" 3>&1 1>&2 2>&3)
exit_status=$?
elif [ "$INTERFACE" == "zenity" ]; then
INPUT="$(zenity --entry --title="$GUI_TITLE" "$ZENITY_ICON_ARG" "$GUI_ICON" ${ZENITY_HEIGHT+--height=$ZENITY_HEIGHT} ${ZENITY_WIDTH+--width=$ZENITY_WIDTH} --text="$1" --entry-text "$2")"
exit_status=$?
elif [ "$INTERFACE" == "kdialog" ]; then
INPUT="$(kdialog --title "$GUI_TITLE" --icon "$GUI_ICON" --inputbox "$1" "$2")"
exit_status=$?
else
read ${NO_READ_DEFAULT+-i "$2"} -rep "${SYMBOL}${bold}$1: ${normal}" INPUT
exit_status=$?
fi
# Exit script if dialog was cancelled
if [ $exit_status -ne 0 ]; then
exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE"
fi
echo "$INPUT"
}
#######################################
# Display a (single or series of) input box(es) for entering a username and a password
# GLOBALS:
# GUI_ICON
# GUI_TITLE
# XDG_ICO_PASSWORD
# PASSWORD_SYMBOL
# TEST_STRING
# INTERFACE
# RECMD_LINES
# RECMD_COLS
# APP_NAME
# ACTIVITY
# ZENITY_ICON_ARG
# ZENITY_HEIGHT (optional)
# ZENITY_WIDTH (optional)
# $1
# $2
# ARGUMENTS:
# The name of the username variable
# The name of the password variable
# The initial username input value
# The text to display for username entry
# The text to display for password entry
# OUTPUTS:
# n/a
# RETURN:
# 0 if success, non-zero otherwise.
#######################################
function userandpassword() {
if [ -z ${GUI_ICON+x} ]; then
GUI_ICON=$XDG_ICO_PASSWORD
fi
_calculate-gui-title
TEST_STRING="$4"
_calculate-tui-size
local __uservar="$1"
local __passvar="$2"
local SUGGESTED_USERNAME="$3"
local USER_TEXT="$4"
if [ "$USER_TEXT" == "" ]; then USER_TEXT="Username"; fi
local PASS_TEXT="$5"
if [ "$PASS_TEXT" == "" ]; then PASS_TEXT="Password"; fi
local CREDS=()
if [ "$INTERFACE" == "whiptail" ]; then
CREDS[0]=$(inputbox "$USER_TEXT" "$SUGGESTED_USERNAME")
CREDS[1]=$(whiptail --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --passwordbox "$PASS_TEXT" "$RECMD_LINES" "$RECMD_COLS" 3>&1 1>&2 2>&3)
elif [ "$INTERFACE" == "dialog" ]; then
mapfile -t CREDS < <( dialog --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --insecure --mixedform "Login:" "$RECMD_LINES" "$RECMD_COLS" 0 "Username: " 1 1 "$SUGGESTED_USERNAME" 1 11 22 0 0 "Password :" 2 1 "" 2 11 22 0 1 3>&1 1>&2 2>&3 )
elif [ "$INTERFACE" == "zenity" ]; then
ENTRY=$(zenity --title="$GUI_TITLE" "$ZENITY_ICON_ARG" "$GUI_ICON" ${ZENITY_HEIGHT+--height=$ZENITY_HEIGHT} ${ZENITY_WIDTH+--width=$ZENITY_WIDTH} --password --username "$SUGGESTED_USERNAME")
local exit_status=$?
CREDS[0]=$(echo "$ENTRY" | cut -d'|' -f1)
CREDS[1]=$(echo "$ENTRY" | cut -d'|' -f2)
# Exit script if dialog was cancelled
if [ $exit_status -ne 0 ]; then
exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE"
fi
elif [ "$INTERFACE" == "kdialog" ]; then
CREDS[0]=$(inputbox "$USER_TEXT" "$SUGGESTED_USERNAME")
CREDS[1]=$(kdialog --title="$GUI_TITLE" --icon "$GUI_ICON" --password "$PASS_TEXT")
else
read ${NO_READ_DEFAULT+-i "$SUGGESTED_USERNAME"} -rep "${QUESTION_SYMBOL}${bold}$USER_TEXT: ${normal}" "CREDS[0]"
local exit_status=$?
if [ $exit_status -eq 0 ]; then
read -srp "${bold}${PASSWORD_SYMBOL}$PASS_TEXT: ${normal}" "CREDS[1]"
exit_status=$?
fi
echo
# Exit script if dialog was cancelled
if [ $exit_status -ne 0 ]; then
exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE"
fi
fi
local exit_status=$?
# Exit script if dialog was cancelled
if [ $exit_status -ne 0 ]; then
exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE"
fi
eval "$__uservar"="'${CREDS[0]}'"
eval "$__passvar"="'${CREDS[1]}'"
}
#######################################
# Display an input box for entering a password
# GLOBALS:
# GUI_ICON
# GUI_TITLE
# XDG_ICO_PASSWORD
# PASSWORD_SYMBOL
# TEST_STRING
# INTERFACE
# RECMD_LINES
# RECMD_COLS
# APP_NAME
# ACTIVITY
# ZENITY_ICON_ARG
# ZENITY_HEIGHT (optional)
# ZENITY_WIDTH (optional)
# ARGUMENTS:
# The text to display for password entry
# OUTPUTS:
# the entered text
# RETURN:
# 0 if success, non-zero otherwise.
#######################################
function password() {
if [ -z ${GUI_ICON+x} ]; then
GUI_ICON=$XDG_ICO_PASSWORD
fi
_calculate-gui-title
# shellcheck disable=SC2034 # TEST_STRING is used by _calculate-tui-rows-cols
TEST_STRING="${PASSWORD_SYMBOL}$1"
_calculate-tui-size
local exit_status=0
if [ "$INTERFACE" == "whiptail" ]; then
PASSWORD=$(whiptail --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --passwordbox "$1" "$RECMD_LINES" "$RECMD_COLS" 3>&1 1>&2 2>&3)
exit_status=$?
elif [ "$INTERFACE" == "dialog" ]; then
PASSWORD=$(dialog --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --passwordbox "$1" "$RECMD_LINES" "$RECMD_COLS" 3>&1 1>&2 2>&3)
exit_status=$?
elif [ "$INTERFACE" == "zenity" ]; then
PASSWORD=$(zenity --title="$GUI_TITLE" "$ZENITY_ICON_ARG" "$GUI_ICON" ${ZENITY_HEIGHT+--height=$ZENITY_HEIGHT} ${ZENITY_WIDTH+--width=$ZENITY_WIDTH} --password)
exit_status=$?
elif [ "$INTERFACE" == "kdialog" ]; then
PASSWORD=$(kdialog --title="$GUI_TITLE" --icon "$GUI_ICON" --password "$1")
exit_status=$?
else
read -srp "${PASSWORD_SYMBOL}${bold}$ACTIVITY: ${normal}" PASSWORD
exit_status=$?
fi
# Exit script if dialog was cancelled
if [ $exit_status -ne 0 ]; then
exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE"
fi
echo "$PASSWORD"
}