Skip to content

Commit 452e362

Browse files
Update ui.sh
1 parent c8d955c commit 452e362

1 file changed

Lines changed: 60 additions & 69 deletions

File tree

lib/ui.sh

Lines changed: 60 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,122 @@
11
#!/data/data/com.termux/files/usr/bin/bash
22

33
# UI Component Library for CompressKit
4-
# Handles all visual elements and user interaction
5-
6-
declare -A UI_COLORS=(
7-
["primary"]=$(tput setaf 51) # Bright Cyan
8-
["secondary"]=$(tput setaf 99) # Purple
9-
["success"]=$(tput setaf 46) # Green
10-
["warning"]=$(tput setaf 214) # Orange
11-
["error"]=$(tput setaf 196) # Red
12-
["info"]=$(tput setaf 39) # Blue
13-
["muted"]=$(tput setaf 245) # Gray
14-
["reset"]=$(tput sgr0)
15-
)
16-
17-
declare -A UI_SYMBOLS=(
18-
["success"]=""
19-
["error"]=""
20-
["warning"]=""
21-
["info"]=""
22-
["arrow"]=""
23-
["bullet"]=""
24-
["check"]=""
25-
["checked"]=""
26-
)
27-
28-
# Matrix rain effect implementation
4+
# Handles all visual elements and user interaction: Recursive systems of presentation.
5+
6+
# Source UI theme constants (modularized for adaptability)
7+
source "$(dirname "$0")/ui_theme.sh"
8+
9+
# --- Matrix Rain: Echoing the collapse of data streams ---
2910
matrix_rain() {
30-
local duration=$1
11+
local duration=${1:-10}
12+
local charset=("@", "#", "$", "%", "&", "*", "+", "-") # Customizable glyphs
13+
local density=$((COLUMNS / 10)) # Characters per row recalibrated for recursive density
14+
3115
local end_time=$((SECONDS + duration))
32-
33-
# Save cursor position and hide it
34-
tput sc
35-
tput civis
36-
16+
tput sc; tput civis
17+
3718
while [ $SECONDS -lt $end_time ]; do
38-
local x=$((RANDOM % COLUMNS))
39-
local y=$((RANDOM % LINES))
40-
tput cup $y $x
41-
echo -en "${UI_COLORS[primary]}$(printf \\$(printf '%03o' $((RANDOM % 93 + 33))))${UI_COLORS[reset]}"
42-
sleep 0.01
19+
for ((i = 0; i < density; i++)); do
20+
local x=$((RANDOM % COLUMNS))
21+
local char="${charset[RANDOM % ${#charset[@]}]}"
22+
tput cup $((RANDOM % LINES)) $x
23+
echo -en "${UI_COLORS[primary]}$char${UI_COLORS[reset]}"
24+
done
25+
sleep 0.05
4326
done
44-
45-
# Restore cursor position and show it
46-
tput rc
47-
tput cnorm
27+
28+
tput rc; tput cnorm
4829
}
4930

50-
# Interactive progress bar
31+
# --- Interactive Progress Bar: Time's arrow moving forward ---
5132
progress_bar() {
5233
local current=$1
5334
local total=$2
54-
local width=50
35+
local label=${3:-"Progress"}
36+
local width=$((COLUMNS - 20)) # Dynamic recalibration for terminal size
5537
local percentage=$((current * 100 / total))
5638
local filled=$((width * current / total))
5739
local empty=$((width - filled))
58-
59-
printf "\r${UI_COLORS[muted]}["
40+
41+
printf "\r${UI_COLORS[info]}%s:${UI_COLORS[reset]} [" "$label"
6042
printf "%${filled}s" | tr " " ""
6143
printf "%${empty}s" | tr " " ""
62-
printf "] %3d%%${UI_COLORS[reset]}" $percentage
44+
printf "] %3d%%" $percentage
6345
}
6446

65-
# Animated spinner
47+
# --- Spinner: Recursive cycles of waiting ---
6648
spinner() {
6749
local pid=$1
68-
local message=$2
69-
local spin='⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'
70-
local charwidth=3
71-
72-
while kill -0 $pid 2>/dev/null; do
73-
local temp=${spin#?}
74-
printf "\r${UI_COLORS[primary]}%c${UI_COLORS[reset]} %s" "$spin" "$message"
75-
spin=$temp${spin%"$temp"}
76-
sleep 0.1
50+
local message=${2:-"Loading"}
51+
local timeout=${3:-30} # Recursive timer (fail-safe)
52+
local spinner_set=('' '' '' '' '' '' '' '' '' '')
53+
local end_time=$((SECONDS + timeout))
54+
55+
while kill -0 $pid 2>/dev/null && [ $SECONDS -lt $end_time ]; do
56+
for frame in "${spinner_set[@]}"; do
57+
printf "\r${UI_COLORS[primary]}%s${UI_COLORS[reset]} %s" "$frame" "$message"
58+
sleep 0.1
59+
done
7760
done
78-
printf "\r"
61+
62+
printf "\r${UI_COLORS[success]}Done!${UI_COLORS[reset]}\n"
7963
}
8064

81-
# Status message functions
65+
# --- Status Messages: Echoing validation ---
8266
ui_success() { echo -e "${UI_COLORS[success]}${UI_SYMBOLS[success]} $1${UI_COLORS[reset]}"; }
8367
ui_error() { echo -e "${UI_COLORS[error]}${UI_SYMBOLS[error]} $1${UI_COLORS[reset]}"; }
8468
ui_warning() { echo -e "${UI_COLORS[warning]}${UI_SYMBOLS[warning]} $1${UI_COLORS[reset]}"; }
8569
ui_info() { echo -e "${UI_COLORS[info]}${UI_SYMBOLS[info]} $1${UI_COLORS[reset]}"; }
8670

87-
# Interactive menu
71+
# --- Interactive Menu: Recursive branching of options ---
8872
ui_menu() {
8973
local title=$1
9074
shift
9175
local options=("$@")
9276
local selected=0
9377
local key
94-
78+
9579
while true; do
9680
clear
9781
echo -e "${UI_COLORS[primary]}$title${UI_COLORS[reset]}\n"
98-
82+
9983
for i in "${!options[@]}"; do
10084
if [ $i -eq $selected ]; then
10185
echo -e "${UI_COLORS[primary]}${UI_SYMBOLS[arrow]} ${options[i]}${UI_COLORS[reset]}"
10286
else
10387
echo -e "${UI_COLORS[muted]} ${options[i]}${UI_COLORS[reset]}"
10488
fi
10589
done
106-
90+
10791
read -rsn1 key
10892
case "$key" in
10993
$'\x1B')
11094
read -rsn2 key
11195
case "$key" in
112-
'[A') ((selected--)) ;; # Up
113-
'[B') ((selected++)) ;; # Down
96+
'[A') ((selected--)) ;; # Navigate up the recursive tree
97+
'[B') ((selected++)) ;; # Navigate down the recursive tree
11498
esac
11599
;;
116-
'') break ;;
100+
'') break ;; # Select current node
117101
esac
118-
102+
119103
selected=$((selected % ${#options[@]}))
120104
[ $selected -lt 0 ] && selected=$((${#options[@]} - 1))
121105
done
122-
106+
123107
return $selected
124108
}
125109

126-
# Confirmation dialog
110+
# --- Confirmation Dialog: Recursive yes/no decision-making ---
127111
ui_confirm() {
128112
local message=$1
129113
local default=${2:-y}
130-
114+
131115
while true; do
132116
printf "${UI_COLORS[primary]}${UI_SYMBOLS[bullet]} %s [y/n] ${UI_COLORS[reset]}" "$message"
133117
read -r -n 1 response
134118
echo
135-
119+
136120
case "$response" in
137121
[yY]) return 0 ;;
138122
[nN]) return 1 ;;
@@ -141,3 +125,10 @@ ui_confirm() {
141125
esac
142126
done
143127
}
128+
129+
# --- Example Usage ---
130+
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
131+
ui_menu "Choose an option:" "Option 1" "Option 2" "Quit"
132+
choice=$?
133+
echo "You selected option $choice"
134+
fi

0 commit comments

Comments
 (0)