Debian Codegraph @

scripts/render-readme-banner.sh

raw
1 #!/usr/bin/env bash
2 set -euo pipefail
3 export LANG=C.UTF-8
4 export LC_ALL=C.UTF-8
5
6 if ! command -v gum >/dev/null 2>&1; then
7 echo "render-readme-banner: gum is required" >&2
8 exit 1
9 fi
10
11 bg="#050301"
12 ink="#ff9e2c"
13 width=61
14 script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
15 mascot_path="$script_dir/assets/mascot.txt"
16 inside=59
17 frame=$'\e[38;2;255;158;44;48;2;5;3;1m'
18 reset=$'\e[0m'
19
20 strip_ansi() {
21 sed -E $'s/\x1B\\[[0-9;?]*[ -\\/]*[@-~]//g'
22 }
23
24 visible_width() {
25 local plain
26 plain="$(printf '%s' "$1" | strip_ansi)"
27 printf '%s' "${#plain}"
28 }
29
30 emit_frame_line() {
31 printf '%s%s%s\n' "$frame" "$1" "$reset"
32 }
33
34 emit_mascot_line() {
35 local line="$1"
36 local width left right
37
38 line="${line//$'\e[?25l'/}"
39 line="${line//$'\e[?25h'/}"
40 width="$(visible_width "$line")"
41 if (( width > inside )); then
42 printf 'render-readme-banner: %s is %d columns, max %d\n' "$mascot_path" "$width" "$inside" >&2
43 exit 1
44 fi
45
46 left=$(((inside - width) / 2))
47 right=$((inside - width - left))
48 printf '%s│%*s%s%s%*s│%s\n' "$frame" "$left" "" "$line" "$frame" "$right" "" "$reset"
49 }
50
51 top="$(
52 gum style \
53 --foreground "$bg" \
54 --background "$ink" \
55 --bold \
56 --padding "0 1" \
57 --width "$width" \
58 ""
59 )"
60
61 main="$(
62 if [[ ! -f "$mascot_path" ]]; then
63 printf 'render-readme-banner: %s not found\n' "$mascot_path" >&2
64 exit 1
65 fi
66
67 emit_frame_line "┌───────────────────────────────────────────────────────────┐"
68 emit_frame_line "│ NAME clnkr(1) │"
69 emit_frame_line "├───────────────────────────────────────────────────────────┤"
70 emit_frame_line "│ │"
71 while IFS= read -r line || [[ -n "$line" ]]; do
72 emit_mascot_line "$line"
73 done <"$mascot_path"
74 emit_frame_line "│ │"
75 emit_frame_line "└───────────────────────────────────────────────────────────┘"
76 )"
77
78 bottom="$(
79 gum style \
80 --foreground "$bg" \
81 --background "$ink" \
82 --padding "0 1" \
83 --width "$width" \
84 ""
85 )"
86
87 gum join --vertical "$top" "$main" "$bottom"