-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialogBox
95 lines (87 loc) · 3.38 KB
/
dialogBox
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
# Creates a Dialog Box
dialogBox() {
local title=$1
local message=$2
local width=${3:-50}
local padding=$(( (width - ${#title}) / 2 ))
clear
# Calculate dialog box dimensions
getTermSize
local dialogWidth=$((termColumns - 4))
local dialogHeight=$((termRows - 4))
# Calculate message position
local messageLines=$(echo "$message" | fold -s -w $((dialogWidth - 2)))
local messageHeight=$(echo "$messageLines" | wc -l)
local messageStartLine=$(( (dialogHeight - messageHeight) / 2 ))
# Print dialog box frame
printAtLineCol $messageStartLine $padding "╔$(printf '═%.0s' $(seq 1 $((width - 2))))╗"
for ((line = 1; line <= dialogHeight; line++)); do
printAtLineCol $((messageStartLine + line)) $padding "║$(printf ' %.0s' $(seq 1 $((width - 2))))║"
done
printAtLineCol $((messageStartLine + dialogHeight + 1)) $padding "╚$(printf '═%.0s' $(seq 1 $((width - 2))))╝"
# Print title and message
printAtLineCol $((messageStartLine + 1)) $((padding + 2)) "$title"
local messageLineNum=1
while IFS= read -r line; do
printAtLineCol $((messageStartLine + 3 + messageLineNum)) $((padding + 2)) "$line"
((messageLineNum++))
done <<< "$messageLines"
# Print buttons
local buttonWidth=8
local buttonPadding=$(( (width - (buttonWidth * 2 + 4)) / 2 ))
local selectedButton=0
local buttons=("OK" "Cancel")
printAtLineCol $((messageStartLine + dialogHeight + 4)) $((padding + buttonPadding)) " [${buttons[0]}] "
printAtLineCol $((messageStartLine + dialogHeight + 4)) $((padding + buttonPadding + buttonWidth + 4)) " [${buttons[1]}] "
# Hide cursor and wait for user input
hideCursor
local key
while true; do
IFS= read -rsn1 key
case "$key" in
$'\x1b') # Escape sequence
IFS= read -rsn2 -t 0.1 key # Read the remaining escape sequence
case "$key" in
'[A') # Up arrow
selectedButton=$(( (selectedButton - 1 + ${#buttons[@]}) % ${#buttons[@]} ))
;;
'[B') # Down arrow
selectedButton=$(( (selectedButton + 1) % ${#buttons[@]} ))
;;
'[D') # Left arrow
selectedButton=0
;;
'[C') # Right arrow
selectedButton=1
;;
'[Z') # Shift+Tab
selectedButton=$(( (selectedButton - 1 + ${#buttons[@]}) % ${#buttons[@]} ))
;;
esac
;;
$'\x09') # Tab
selectedButton=$(( (selectedButton + 1) % ${#buttons[@]} ))
;;
'') # Enter
case $selectedButton in
0)
exit 0
;;
1)
exit 1
;;
esac
;;
esac
# Print buttons with correct highlighting
local buttonColor='\e[0m' # Reset color
local selectedColor='\e[7m' # Background color for selected button
printAtLineCol $((messageStartLine + dialogHeight + 4)) $((padding + buttonPadding)) " [${buttons[0]}] "
printAtLineCol $((messageStartLine + dialogHeight + 4)) $((padding + buttonPadding + buttonWidth + 4)) " [${buttons[1]}] "
if [[ $selectedButton -eq 0 ]]; then
printAtLineCol $((messageStartLine + dialogHeight + 4)) $((padding + buttonPadding)) "${selectedColor}[${buttons[0]}]${buttonColor}"
else
printAtLineCol $((messageStartLine + dialogHeight + 4)) $((padding + buttonPadding + buttonWidth + 4)) "${selectedColor}[${buttons[1]}]${buttonColor}"
fi
done
}