add Standalone clock
Test blackout notifier / test (push) Successful in 5s

This commit is contained in:
MeghdadFadaee committed 2026-10-07 09:40:38 +03:30
1 parent d597602c8a
commit 22b0697734
2 files changed
+481

No files matched your search

+21
View File
@@ -247,3 +247,24 @@ NO_COLOR=1 COLUMNS=80 LINES=24 ./main.sh
When `pyfiglet` or `figlet` provides the `univers` font, the countdown uses it
automatically. Otherwise the bundled matching glyphs are used, keeping the same
appearance without making Figlet a dependency.
## Standalone clock
Run `./clock.sh` for a live clock using the dashboard's large digits and cyan
dividers. It shows Gregorian and Jalali dates, a greeting for the time of day,
and a progress bar counting toward midnight. On smaller terminals, the large
display shows hours and minutes with the precise time below it.
The clock digits turn red during the last ten minutes of each hour (`:50`–`:59`).
```sh
./clock.sh
./clock.sh --once # Print a single snapshot
./clock.sh --utc # Use UTC
TZ=Europe/London ./clock.sh # Choose another timezone
```
The clock defaults to Tehran time and needs only Bash 4.3+ and GNU `date`.
It works independently of `main.sh` and needs no network connection. Press `q`
or `Ctrl+C` to exit; the original terminal screen and cursor are restored.
Piped output prints one snapshot automatically. `NO_COLOR`, `COLUMNS`, and
`LINES` work as they do for the outage dashboard.
Executable
+460
View File
@@ -0,0 +1,460 @@
#!/usr/bin/env bash
set -euo pipefail
# macOS ships Bash 3.2 and BSD date. Prefer Homebrew's compatible runtime when
# it is installed, even when this script was started explicitly with /bin/bash.
for coreutils_path in \
/opt/homebrew/opt/coreutils/libexec/gnubin \
/usr/local/opt/coreutils/libexec/gnubin; do
if [[ -d "$coreutils_path" ]]; then
PATH="$coreutils_path:$PATH"
export PATH
break
fi
done
unset coreutils_path
if ((BASH_VERSINFO[0] < 4 || (BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] < 3))); then
for modern_bash in /opt/homebrew/bin/bash /usr/local/bin/bash; do
if [[ -x "$modern_bash" ]]; then
exec "$modern_bash" "$0" "$@"
fi
done
printf 'Error: Bash 4.3 or newer is required (found %s).\n' "$BASH_VERSION" >&2
printf 'On macOS, install it with: brew install bash coreutils\n' >&2
exit 2
fi
unset modern_bash
# The standalone font and banner renderer match main.sh.
center_line() {
local text="$1"
local columns="$2"
local padding=0
if ((${#text} < columns)); then
padding=$(((columns - ${#text}) / 2))
fi
printf '%*s%s\n' "$padding" '' "$text"
}
repeat_character() {
local character="$1"
local count="$2"
local output=""
printf -v output '%*s' "$count" ''
printf '%s' "${output// /$character}"
}
large_glyph() {
case "$1" in
0)
cat <<'EOF'
,a8888a,
,8P"' `"Y8,
,8P Y8,
88 88
88 88
`8b d8'
`8ba, ,ad8'
"Y8888P"
EOF
;;
1)
cat <<'EOF'
88
,d88
888888
88
88
88
88
88
EOF
;;
2)
cat <<'EOF'
ad888888b,
d8" "88
a8P
,d8P"
a8P"
a8P'
d8"
88888888888
EOF
;;
3)
cat <<'EOF'
ad888888b,
d8" "88
a8P
aad8"
""Y8,
"8b
Y8, a88
"Y888888P'
EOF
;;
4)
cat <<'EOF'
,d8
,d888
,d8" 88
,d8" 88
,d8" 88
8888888888888
88
88
EOF
;;
5)
cat <<'EOF'
8888888888
88
88 ____
88a8PPPP8b,
PP" `8b
d8
Y8a a8P
"Y88888P"
EOF
;;
6)
cat <<'EOF'
ad8888ba,
8P' "Y8
d8
88,dd888bb,
88P' `8b
88 d8
88a a8P
"Y88888P"
EOF
;;
7)
cat <<'EOF'
888888888888
,8P'
d8"
,8P'
d8"
,8P'
d8"
8P'
EOF
;;
8)
cat <<'EOF'
ad88888ba
d8" "8b
Y8a a8P
"Y8aaa8P"
,d8"""8b,
d8" "8b
Y8a a8P
"Y88888P"
EOF
;;
9)
cat <<'EOF'
ad88888ba
d8" "88
8P 88
Y8, ,d88
"PPPPPP"88
8P
8b, a8P
`"Y8888P'
EOF
;;
D)
cat <<'EOF'
88888888ba
88 "8b
88 8b
88 88
88 88
88 8P
88 a8P
88888888P"
EOF
;;
H)
cat <<'EOF'
88 88
88 88
88 88
8888888888
88 88
88 88
88 88
88 88
EOF
;;
M)
cat <<'EOF'
88b d88
888b d888
88`8b d8'88
88 `8b d8' 88
88 `8b d8' 88
88 `8b d8' 88
88 `888' 88
88 `8' 88
EOF
;;
:)
printf '%s\n' ' ' '888' '888' ' ' ' ' '888' '888' ' '
;;
' ')
printf '%s\n' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
;;
*)
printf '%s\n' '???' '???' '???' '???' '???' '???' '???' '???'
;;
esac
}
print_large_countdown() {
local text="$1"
local columns="$2"
local character glyph_line glyph_width index row rendered_width=0 block_padding=0
local -a glyph_lines rows=('' '' '' '' '' '' '' '')
for ((index = 0; index < ${#text}; index++)); do
character="${text:index:1}"
mapfile -t glyph_lines < <(large_glyph "$character")
glyph_width=0
for row in {0..7}; do
if ((${#glyph_lines[row]} > glyph_width)); then
glyph_width="${#glyph_lines[row]}"
fi
done
for row in {0..7}; do
printf -v glyph_line '%-*s' "$glyph_width" "${glyph_lines[row]:-}"
rows[row]+="$glyph_line "
done
done
for row in {0..7}; do
while [[ "${rows[row]}" == *' ' ]]; do
rows[row]="${rows[row]% }"
done
if ((${#rows[row]} > rendered_width)); then
rendered_width="${#rows[row]}"
fi
done
if ((rendered_width > columns)); then
center_line "$text" "$columns"
return
fi
block_padding=$(((columns - rendered_width) / 2))
for row in {0..7}; do
printf '%*s%s\n' "$block_padding" '' "${rows[row]}"
done
}
TERMINAL_ACTIVE=0
clock_cleanup() {
if ((TERMINAL_ACTIVE)); then
printf '\033[0m\033[?25h\033[?1049l'
fi
}
clock_usage() {
cat <<'EOF'
Usage: ./clock.sh [--once] [--utc]
A live clock with Gregorian and Jalali dates and a daily progress bar.
Press q or Ctrl+C to quit. Piped output prints one snapshot.
--once Print one snapshot without taking over the terminal
--utc Display UTC instead of the default Tehran time
-h, --help Show this help
Environment: TZ overrides Tehran time; NO_COLOR disables colors;
COLUMNS and LINES override terminal dimensions.
Requires Bash 4.3+ and GNU date (macOS: brew install bash coreutils).
EOF
}
clock_size() {
local rows=24 columns=80 detected_rows detected_columns
if [[ -t 1 ]]; then
read -r detected_rows detected_columns < <(stty size <&1 2>/dev/null || true)
rows="${detected_rows:-24}"
columns="${detected_columns:-80}"
fi
rows="${LINES:-$rows}"
columns="${COLUMNS:-$columns}"
[[ "$rows" =~ ^[1-9][0-9]{0,3}$ ]] || rows=24
[[ "$columns" =~ ^[1-9][0-9]{0,3}$ ]] || columns=80
printf '%s %s\n' "$rows" "$columns"
}
# Arithmetic inverse of the modern Jalali conversion used by main.sh.
clock_jalali() {
local gy="$1" gm="$2" gd="$3" gy2 days jy jm jd
local -a offsets=(0 31 59 90 120 151 181 212 243 273 304 334)
gy2=$gy
((gm > 2)) && gy2=$((gy + 1))
days=$((355666 + 365 * gy + (gy2 + 3) / 4 - (gy2 + 99) / 100
+ (gy2 + 399) / 400 + gd + offsets[gm - 1]))
jy=$((-1595 + 33 * (days / 12053)))
days=$((days % 12053))
jy=$((jy + 4 * (days / 1461)))
days=$((days % 1461))
if ((days > 365)); then
jy=$((jy + (days - 1) / 365))
days=$(((days - 1) % 365))
fi
if ((days < 186)); then
jm=$((1 + days / 31))
jd=$((1 + days % 31))
else
jm=$((7 + (days - 186) / 30))
jd=$((1 + (days - 186) % 30))
fi
printf '%04d/%02d/%02d\n' "$jy" "$jm" "$jd"
}
clock_center() {
local text="$1" columns="$2" color="${3:-}"
if ((${#text} > columns)); then
text="${text:0:columns}"
fi
printf '%s' "$color"
center_line "$text" "$columns"
printf '%s' "$RESET"
}
clock_render() {
local epoch="$1" rows="$2" columns="$3"
local year month day hour minute second weekday month_name offset day_number
local time date_text jalali period rule banner banner_rows seconds_today clock_color detail_color
local bar_width filled percent remaining progress
read -r year month day hour minute second weekday month_name offset day_number < <(
LC_ALL=C date -d "@$epoch" '+%Y %m %d %H %M %S %A %B %z %j'
)
time="$hour : $minute : $second"
clock_color="$YELLOW"
detail_color="$DIM"
if ((10#$minute >= 50)); then
clock_color="$RED"
detail_color="$RED"
fi
jalali="$(clock_jalali "$((10#$year))" "$((10#$month))" "$((10#$day))")"
date_text="$weekday, $month_name $((10#$day)), $year"
case "$hour" in
0[0-4]|2[1-3]) period="QUIET HOURS" ;;
0[5-9]|1[01]) period="GOOD MORNING" ;;
1[2-6]) period="GOOD AFTERNOON" ;;
*) period="GOOD EVENING" ;;
esac
rule="$(repeat_character '=' "$columns")"
clock_center "$rule" "$columns" "$CYAN"
clock_center "$period / ${TZ} (UTC${offset:0:3}:${offset:3:2})" "$columns" "$DIM"
banner="$(print_large_countdown "$time" "$columns")"
# Keep the big hours and minutes when all six digits would not fit.
if [[ "$banner" != *$'\n'* ]]; then
banner="$(print_large_countdown "$hour:$minute" "$columns")"
fi
banner_rows=1
[[ "$banner" == *$'\n'* ]] && banner_rows=8
if ((banner_rows == 1 || rows < banner_rows + 9)); then
banner="$(center_line "$time" "$columns")"
fi
printf '%s%s%s\n' "$clock_color" "$banner" "$RESET"
clock_center "$hour:$minute:$second" "$columns" "$detail_color"
clock_center "$rule" "$columns" "$CYAN"
clock_center "$date_text" "$columns" "$BOLD"
clock_center "JALALI $jalali" "$columns" "$CYAN"
if ((rows >= 10 && columns >= 24)); then
seconds_today=$((10#$hour * 3600 + 10#$minute * 60 + 10#$second))
percent=$((seconds_today * 100 / 86400))
bar_width=$((columns - 20))
((bar_width > 40)) && bar_width=40
filled=$((seconds_today * bar_width / 86400))
progress="[$(repeat_character '#' "$filled")$(repeat_character '.' "$((bar_width - filled))")]"
clock_center "DAY $progress $percent%" "$columns" "$GREEN"
remaining=$((1440 - seconds_today / 60))
clock_center "DAY $((10#$day_number)) / $((remaining / 60))h $((remaining % 60))m to midnight" "$columns" "$DIM"
fi
if ((rows >= 11)); then
clock_center "ONE SECOND AT A TIME / q to quit" "$columns" "$DIM"
fi
clock_center "$rule" "$columns" "$CYAN"
}
clock_main() {
local once=0 input_fd=0 epoch last_epoch=-1 rows columns frame key
export TZ="${TZ:-Asia/Tehran}"
while (($#)); do
case "$1" in
--once) once=1 ;;
--utc) export TZ=UTC ;;
-h|--help) clock_usage; return 0 ;;
*) printf 'Error: unknown option: %s\n' "$1" >&2; return 2 ;;
esac
shift
done
if ! date -d '@0' +%s >/dev/null 2>&1; then
printf 'Error: GNU date is required (macOS: brew install coreutils).\n' >&2
return 1
fi
RESET="" BOLD="" DIM="" CYAN="" YELLOW="" GREEN="" RED=""
trap clock_cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
if [[ ! -t 1 ]]; then
once=1
fi
if ((!once)); then
if [[ -t 0 ]]; then
input_fd=0
elif [[ -r /dev/tty ]]; then
exec {input_fd}</dev/tty
fi
TERMINAL_ACTIVE=1
printf '\033[?1049h\033[?25l\033[H'
fi
if [[ -t 1 && -z "${NO_COLOR+x}" ]]; then
RESET=$'\033[0m' BOLD=$'\033[1m' DIM=$'\033[2m'
CYAN=$'\033[36m' YELLOW=$'\033[33m' GREEN=$'\033[32m'
RED=$'\033[31m'
fi
while true; do
epoch="$(date +%s)"
if [[ "$epoch" != "$last_epoch" ]]; then
read -r rows columns < <(clock_size)
frame="$(clock_render "$epoch" "$rows" "$columns")"
if ((TERMINAL_ACTIVE)); then
printf '\033[H%s\033[K\033[J' "${frame//$'\n'/$'\033[K\n'}"
else
printf '%s\n' "$frame"
fi
last_epoch="$epoch"
fi
((once)) && break
key=""
if [[ -t "$input_fd" ]]; then
if read -u "$input_fd" -rsn 1 -t 0.1 key && [[ "$key" == [qQ] ]]; then
break
fi
else
sleep 0.1
fi
done
}
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
clock_main "$@"
fi