From 301e57fe70fbfce19c2f0eb9c5185c77827b2e38 Mon Sep 17 00:00:00 2001 From: MeghdadFadaee Date: Tue, 21 Jul 2026 17:01:00 +0330 Subject: [PATCH] fix(cli): make status dividers span terminal width --- main.sh | 54 ++++++++++++++++++++++++++++++++-------------- tests/test_main.sh | 15 +++++++++++++ 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/main.sh b/main.sh index b664745..3e1d443 100755 --- a/main.sh +++ b/main.sh @@ -354,8 +354,25 @@ build_schedule() { terminal_columns() { local columns="${COLUMNS:-}" + local terminal_size="" detected_columns="" - if [[ -z "$columns" && -t 1 ]] && command -v tput >/dev/null 2>&1; then + if [[ -z "$columns" ]] && command -v stty >/dev/null 2>&1; then + if [[ -r /dev/tty ]]; then + terminal_size="$(stty size 2>/dev/null /dev/null 2>&1; then columns="$(tput cols 2>/dev/null || true)" fi @@ -691,28 +708,29 @@ render_event() { render_empty() { local columns="$1" - local width=$((columns < 64 ? columns : 64)) local rule - rule="$(repeat_character '=' "$width")" + rule="$(repeat_character '=' "$columns")" printf '%s%s%s\n' "$GREEN" "$rule" "$RESET" printf '\n' - center_line "ALL CLEAR" "$width" - center_line "No current or upcoming outages." "$width" + center_line "ALL CLEAR" "$columns" + center_line "No current or upcoming outages." "$columns" printf '\n' printf '%s%s%s\n' "$GREEN" "$rule" "$RESET" } render_unavailable() { - local columns width rule + local columns="${1:-}" + local rule color_setup - columns="$(terminal_columns)" - width=$((columns < 64 ? columns : 64)) - rule="$(repeat_character '=' "$width")" + if [[ -z "$columns" ]]; then + columns="$(terminal_columns)" + fi + rule="$(repeat_character '=' "$columns")" printf '%s%s%s\n\n' "$RED" "$rule" "$RESET" - center_line "STATE UNAVAILABLE" "$width" - center_line "Waiting for the next refresh." "$width" + center_line "STATE UNAVAILABLE" "$columns" + center_line "Waiting for the next refresh." "$columns" printf '\n%s%s%s\n' "$RED" "$rule" "$RESET" } @@ -753,12 +771,14 @@ schedule_next_transition() { render_dashboard() { local now_epoch="$1" - local columns content_width rule phase start_epoch stop_epoch record remaining banner + local columns="${2:-}" + local rule phase start_epoch stop_epoch record remaining banner local schedule_line ignored color_setup - columns="$(terminal_columns)" - content_width=$((columns < 150 ? columns : 150)) + if [[ -z "$columns" ]]; then + columns="$(terminal_columns)" + fi if ((${#SCHEDULE[@]} == 0)); then render_empty "$columns" @@ -792,6 +812,7 @@ render_dashboard() { main() { local now_epoch next_refresh=0 refresh_minutes=10 refresh_seconds state_ready=0 local last_rendered=-1 next_transition=0 rebuild_schedule=0 frame key input_fd=0 + local display_columns while (($#)); do case "$1" in @@ -879,11 +900,12 @@ main() { fi fi + display_columns="$(terminal_columns)" frame="$( if ((state_ready)); then - render_dashboard "$now_epoch" + render_dashboard "$now_epoch" "$display_columns" else - render_unavailable + render_unavailable "$display_columns" fi if [[ -n "$REFRESH_ERROR" ]]; then printf '\n%s\n' "$REFRESH_ERROR" diff --git a/tests/test_main.sh b/tests/test_main.sh index f038687..3d076e7 100755 --- a/tests/test_main.sh +++ b/tests/test_main.sh @@ -182,6 +182,21 @@ assert_equal "0" "${#SCHEDULE[@]}" \ EMPTY_OUTPUT="$(NO_COLOR=1 COLUMNS=60 render_dashboard "$EMPTY_NOW")" assert_contains "$EMPTY_OUTPUT" "ALL CLEAR" \ "an empty schedule should render the all-clear screen" +WIDE_EMPTY_OUTPUT="$(NO_COLOR=1 COLUMNS=180 render_dashboard "$EMPTY_NOW")" +WIDE_EMPTY_TOP_LINE="${WIDE_EMPTY_OUTPUT%%$'\n'*}" +WIDE_EMPTY_BOTTOM_LINE="${WIDE_EMPTY_OUTPUT##*$'\n'}" +assert_equal "180" "${#WIDE_EMPTY_TOP_LINE}" \ + "the all-clear top divider should span the full terminal width" +assert_equal "180" "${#WIDE_EMPTY_BOTTOM_LINE}" \ + "the all-clear bottom divider should span the full terminal width" + +WIDE_UNAVAILABLE_OUTPUT="$(NO_COLOR=1 COLUMNS=80 render_unavailable 180)" +WIDE_UNAVAILABLE_TOP_LINE="${WIDE_UNAVAILABLE_OUTPUT%%$'\n'*}" +WIDE_UNAVAILABLE_BOTTOM_LINE="${WIDE_UNAVAILABLE_OUTPUT##*$'\n'}" +assert_equal "180" "${#WIDE_UNAVAILABLE_TOP_LINE}" \ + "the unavailable top divider should span the full terminal width" +assert_equal "180" "${#WIDE_UNAVAILABLE_BOTTOM_LINE}" \ + "the unavailable bottom divider should use the detected terminal width" TERMINAL_ACTIVE=1 DRAW_OUTPUT="$(draw_frame $'short\nlonger')"