fix(cli): make status dividers span terminal width
Some checks failed
Test blackout notifier / test (push) Failing after 2s
Hourly blackout check / check (push) Successful in 3s

This commit is contained in:
2026-07-21 17:01:00 +03:30
parent b317726638
commit 301e57fe70
2 changed files with 53 additions and 16 deletions

50
main.sh
View File

@@ -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/tty || true)"
fi
if [[ -z "$terminal_size" && -t 2 ]]; then
terminal_size="$(stty size <&2 2>/dev/null || true)"
fi
if [[ -z "$terminal_size" && -t 0 ]]; then
terminal_size="$(stty size <&0 2>/dev/null || true)"
fi
read -r _ detected_columns <<<"$terminal_size"
if [[ "$detected_columns" =~ ^[0-9]+$ ]]; then
columns="$detected_columns"
fi
fi
if [[ -z "$columns" ]] && command -v tput >/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
if [[ -z "$columns" ]]; then
columns="$(terminal_columns)"
width=$((columns < 64 ? columns : 64))
rule="$(repeat_character '=' "$width")"
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
if [[ -z "$columns" ]]; then
columns="$(terminal_columns)"
content_width=$((columns < 150 ? columns : 150))
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"

View File

@@ -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')"