Skip to content

Commit 5d1c9f2

Browse files
committed
bash-ui-lib: testing claude v2
Signed-off-by: Maciej Pijanowski <[email protected]>
1 parent 6f8e992 commit 5d1c9f2

File tree

5 files changed

+44
-11
lines changed

5 files changed

+44
-11
lines changed

bash-ui-lib/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ A reusable Bash library for creating interactive text-based user interfaces with
1111
- **Submenus**: Support for nested menu structures
1212
- **Customizable**: Override default rendering with custom callbacks
1313
- **Well-Tested**: Comprehensive test suite using BATS
14+
- **Safe**: Gracefully handles non-interactive terminals
1415

1516
## Installation
1617

@@ -24,6 +25,14 @@ git clone <repository-url>
2425
cp -r bash-ui-lib/lib /path/to/your/project/
2526
```
2627

28+
## Requirements
29+
30+
- **Interactive Terminal**: The library requires a TTY (interactive terminal) to function properly
31+
- **Bash 4.0+**: Uses associative arrays and other modern bash features
32+
- **Standard utilities**: `tput`, `grep`, `sed` (usually pre-installed)
33+
34+
**Note**: The library will detect non-interactive terminals and exit gracefully with appropriate warnings.
35+
2736
## Quick Start
2837

2938
Here's a minimal example:

bash-ui-lib/lib/dts-ui-adapter.sh

100644100755
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,8 @@ _setup_default_dts_menu() {
137137
# show_footer
138138
# Show footer with actions using UI library
139139
show_footer() {
140-
# Check if footer is set up
141-
if [[ "${#UI_FOOTER_ACTIONS_KEYS[@]}" -eq 0 ]]; then
142-
_setup_default_dts_footer
143-
fi
144-
145-
# Render footer using UI library
146-
ui_render_footer
140+
# Render DTS-style footer directly (don't call ui_render_footer to avoid recursion)
141+
_setup_default_dts_footer
147142
}
148143

149144
# _setup_default_dts_footer
@@ -155,7 +150,13 @@ _setup_default_dts_footer() {
155150
echo -ne "${RED}${POWEROFF_OPT_UP:-P}${NORMAL} to poweroff ${NORMAL}"
156151
echo -e "${RED}${SHELL_OPT_UP:-S}${NORMAL} to enter shell ${NORMAL}"
157152

158-
if systemctl is-active sshd.service >/dev/null 2>&1; then
153+
# Safely check SSH status - avoid segfault from systemctl issues
154+
local ssh_status="inactive"
155+
if command -v systemctl &>/dev/null; then
156+
ssh_status=$(systemctl is-active sshd.service 2>/dev/null || echo "inactive")
157+
fi
158+
159+
if [[ "$ssh_status" == "active" ]]; then
159160
echo -ne "${RED}${SSH_OPT_UP:-K}${NORMAL} to stop SSH server ${NORMAL}"
160161
else
161162
echo -ne "${RED}${SSH_OPT_UP:-K}${NORMAL} to launch SSH server ${NORMAL}"

bash-ui-lib/lib/ui-core.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,18 @@ ui_set_post_render_callback() {
8888
ui_read_input() {
8989
UI_INPUT=""
9090

91+
# Check if terminal is interactive
92+
if ! [[ -t 0 ]]; then
93+
# Not interactive, exit the loop
94+
ui_exit 1
95+
return 1
96+
fi
97+
9198
if [[ "$UI_INPUT_MODE" == "single" ]]; then
92-
read -r -n 1 UI_INPUT
99+
read -r -n 1 UI_INPUT || { ui_exit 1; return 1; }
93100
echo
94101
else
95-
read -r UI_INPUT
102+
read -r UI_INPUT || { ui_exit 1; return 1; }
96103
fi
97104
}
98105

bash-ui-lib/lib/ui-render.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,11 @@ ui_render_screen() {
296296
ui_handle_menu_selection() {
297297
local key="$1"
298298

299+
# Return early if key is empty or invalid
300+
if [[ -z "$key" ]]; then
301+
return 1
302+
fi
303+
299304
# Check if key exists in menu items
300305
if [[ -n "${UI_MENU_ITEMS_HANDLER[$key]+isset}" ]]; then
301306
local condition="${UI_MENU_ITEMS_CONDITION[$key]}"
@@ -327,6 +332,11 @@ ui_handle_menu_selection() {
327332
ui_handle_footer_selection() {
328333
local key="$1"
329334

335+
# Return early if key is empty or invalid
336+
if [[ -z "$key" ]]; then
337+
return 1
338+
fi
339+
330340
# Check if key exists in footer actions
331341
if [[ -n "${UI_FOOTER_ACTIONS_HANDLER[$key]+isset}" ]]; then
332342
local condition="${UI_FOOTER_ACTIONS_CONDITION[$key]}"

bash-ui-lib/lib/ui-utils.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,13 @@ ui_confirm() {
141141
# Pause and wait for user to press Enter
142142
ui_pause() {
143143
local message="${1:-Press Enter to continue.}"
144-
read -r -p "$message"
144+
# Check if stdin is available and terminal is interactive
145+
if [[ -t 0 ]]; then
146+
read -r -p "$message" || true
147+
else
148+
# Non-interactive, just print message and return
149+
echo "$message"
150+
fi
145151
}
146152

147153
# ui_wait_seconds <seconds> [message]

0 commit comments

Comments
 (0)