-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhelpers.sh
More file actions
238 lines (211 loc) · 6.09 KB
/
Copy pathhelpers.sh
File metadata and controls
238 lines (211 loc) · 6.09 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env bash
# Multi-UI Scripting - Helper Functions
# https://github.com/lunarcloud/script-dialog
# LGPL-2.1 license
# Variables set in init.sh and used here
# shellcheck disable=SC2154
#######################################
# Attempts to run a privileged command (sudo or equivalent)
# GLOBALS:
# NO_SUDO
# SUDO
# ACTIVITY
# ARGUMENTS:
# Command to run with elevated privilege
# OUTPUTS:
# n/a
# RETURN:
# 0 if success, non-zero otherwise.
#######################################
function superuser() {
if [ "$NO_SUDO" == true ]; then
(>&2 echo "${red}No sudo available!${normal}")
return 201
fi
if [ "$SUDO_USE_INTERFACE" == true ]; then
ACTIVITY="Enter password to run \"$*\""
password "$@" | sudo -p "" -S -- "$@"
elif [[ "$SUDO" == *"pkexec"* ]]; then
$SUDO "$@"
elif sudo -n true 2>/dev/null; then # if credentials cached
sudo -- "$@"
else
$SUDO -- "$@"
fi
}
#######################################
# Set the GUI_TITLE based on the ACTIVITY and APP_NAME
# GLOBALS:
# GUI_TITLE
# ACTIVITY
# APP_NAME
# ARGUMENTS:
# n/a
# OUTPUTS:
# n/a
# RETURN:
# n/a
#######################################
function _calculate-gui-title() {
if [ -n "$ACTIVITY" ]; then
# shellcheck disable=SC2034 # GUI_TITLE is used by other functions
GUI_TITLE="$ACTIVITY - $APP_NAME"
else
# shellcheck disable=SC2034
GUI_TITLE="$APP_NAME"
fi
}
#######################################
# Update the max columns & lines
# GLOBALS:
# GUI
# MAX_COLS
# MAX_LINES
# ARGUMENTS:
# n/a
# OUTPUTS:
# n/a
# RETURN:
# n/a
#######################################
function _calculate-tui-max() {
if ! command -v tput >/dev/null 2>&1; then
return;
fi
if [ "$GUI" == "false" ] ; then
MAX_LINES=$(tput lines 2>/dev/null)
MAX_COLS=$(tput cols 2>/dev/null)
fi
# Never really fill the whole screen space
MAX_LINES=$(( MAX_LINES - 5 ))
MAX_COLS=$(( MAX_COLS - 4 ))
}
#######################################
# Update the recommended columns, lines, scroll
# GLOBALS:
# TEST_STRING
# RECMD_SCROLL
# RECMD_COLS
# RECMD_LINES
# MAX_COLS
# MAX_LINES
# MIN_COLS
# MIN_LINES
# ARGUMENTS:
# n/a
# OUTPUTS:
# n/a
# RETURN:
# n/a
#######################################
function _calculate-tui-size() {
_calculate-tui-max
RECMD_SCROLL=false
# Handle empty string case
if [ -z "$TEST_STRING" ]; then
# shellcheck disable=SC2153 # MIN_COLS and MIN_LINES are defined in init.sh
RECMD_COLS=$MIN_COLS
# shellcheck disable=SC2153
RECMD_LINES=$MIN_LINES
return
fi
# Count actual lines and find longest line
local line_count=0
local max_line_length=0
while IFS= read -r line; do
line_count=$((line_count + 1))
local line_len=${#line}
if [ "$line_len" -gt "$max_line_length" ]; then
max_line_length=$line_len
fi
done <<< "$TEST_STRING"
# Calculate recommended columns based on longest line
# Add padding for borders, margins, and UI elements (typically 4-6 chars)
RECMD_COLS=$((max_line_length + 6))
# For single-line text, consider wrapping and use a reasonable width
if [ "$line_count" -eq 1 ]; then
local total_chars=${#TEST_STRING}
# Target width: Start with content + padding, but cap at a reasonable maximum
# Use 50% of available space as the maximum for single-line text
local target_width=$((MAX_COLS / 2))
if [ "$target_width" -lt "$MIN_COLS" ]; then
target_width=$MIN_COLS
fi
# Only expand to target width if text is long enough to benefit from it
# For short text, stay closer to content size
if [ "$RECMD_COLS" -lt "$target_width" ] && [ "$total_chars" -gt 40 ]; then
RECMD_COLS=$target_width
fi
# Calculate wrapped line count at this width
# Subtract padding to get actual text width
local text_width=$((RECMD_COLS - 6))
# Ensure text_width is at least 1 to avoid division by zero
if [ "$text_width" -le 0 ]; then
text_width=1
fi
# Calculate ceiling division: (total_chars + text_width - 1) / text_width
line_count=$(((total_chars + text_width - 1) / text_width))
fi
# Calculate recommended lines with padding for UI elements
# Add 4 lines for title, borders, and buttons
RECMD_LINES=$((line_count + 4))
# Enforce maximum constraints
if [ "$RECMD_LINES" -gt "$MAX_LINES" ] ; then
RECMD_LINES=$MAX_LINES
# shellcheck disable=SC2034 # RECMD_SCROLL is used by dialog functions
RECMD_SCROLL=true
fi
if [ "$RECMD_COLS" -gt "$MAX_COLS" ]; then
RECMD_COLS=$MAX_COLS
# shellcheck disable=SC2034
RECMD_SCROLL=true
fi
# Enforce minimum constraints
if [ "$RECMD_LINES" -lt "$MIN_LINES" ] ; then
RECMD_LINES=$MIN_LINES
fi
if [ "$RECMD_COLS" -lt "$MIN_COLS" ]; then
RECMD_COLS=$MIN_COLS
fi
TEST_STRING="" #blank out for memory's sake
}
#######################################
# if neither GUI nor terminal interfaces can be used, relaunch the script in a terminal emulator
# GLOBALS:
# GUI
# terminal
# ARGUMENTS:
# n/a
# OUTPUTS:
# n/a
# RETURN:
# 0 if success, non-zero otherwise.
#######################################
function relaunch-if-not-visible() {
local parentScript
parentScript=$(basename "$0")
if [ "$GUI" == "false" ] && [ "$terminal" == "false" ]; then
if [ -e "/tmp/relaunching" ] && [ "$(cat /tmp/relaunching)" == "$parentScript" ]; then
echo "Won't relaunch $parentScript more than once"
else
echo "$parentScript" > /tmp/relaunching
echo "Relaunching $parentScript ..."
local TERM_APP=xterm
# Launch in whatever terminal is available
if command -v >/dev/null x-terminal-emulator; then
TERM_APP=x-terminal-emulator
elif [ "$desktop" != "gnome" ] && command -v >/dev/null kdialog; then
TERM_APP=kdialog
elif [ "$desktop" != "kde" ] && command -v >/dev/null gnome-terminal; then
TERM_APP=gnome-terminal
elif command -v >/dev/null xfce4-terminal; then
TERM_APP=xfce4-terminal
elif command -v >/dev/null qterminal; then
TERM_APP=qterminal
fi
$TERM_APP -e "./$parentScript"
rm /tmp/relaunching
exit $?;
fi
fi
}