From 458d7c4a14a34b99748e0f32aeebdfdba6488f45 Mon Sep 17 00:00:00 2001 From: FORGE Date: Tue, 1 Dec 2026 08:20:00 +0800 Subject: [PATCH 1/3] fix(motd): apply the shared dark/light palette and heavier rules Hostname, OS version and kernel version now use the three RGB values fixed by the approved dark/light design, while every other field keeps the terminal theme colours. The welcome bar, both dividers and the memory/root-disk bars switch to heavy/block glyphs, and the PAM login path prints one bottom rule after the login record (also when no record exists). --- modules/system-customize.sh | 66 +++++++++++++++++++++---------------- tests/test-motd.sh | 47 +++++++++++++++++--------- tools/setup-motd.sh | 66 +++++++++++++++++++++---------------- 3 files changed, 106 insertions(+), 73 deletions(-) diff --git a/modules/system-customize.sh b/modules/system-customize.sh index c1ecd91..d70db19 100644 --- a/modules/system-customize.sh +++ b/modules/system-customize.sh @@ -38,7 +38,7 @@ fi readonly MOTD_ETC_ROOT MOTD_STATE_DIR MOTD_LOCK_FILE MOTD_TRANSACTION_PARENT MOTD_TRUSTED_UID MOTD_TRUSTED_GID readonly MOTD_SCRIPT="$MOTD_ETC_ROOT/update-motd.d/00-custom-welcome" readonly MOTD_SNAPSHOT_VERSION=1 -readonly MOTD_PAYLOAD_SHA256="0537c91cb69d1c3360f3302ee515528abf4ba014f018735a1dbebf6ecbcce836" +readonly MOTD_PAYLOAD_SHA256="04152b5c08fd321c7d64046d82c3d1e8a90a287ace7a2ca505d0245b4de07ce1" readonly -a MOTD_TARGET_IDS=(motd issue issue_net custom_welcome uname motd_news) readonly -a MOTD_TARGET_PATHS=( "$MOTD_ETC_ROOT/motd" "$MOTD_ETC_ROOT/issue" "$MOTD_ETC_ROOT/issue.net" @@ -2170,24 +2170,33 @@ login_query() { } render_last_login() { - local user="${PAM_USER:-}" raw time host kind - raw=$(login_query "$user") || return 0 - time=$(login_normalize_time "${raw%%[[:space:]]*}") || return 0 - host="${raw#"${raw%%[[:space:]]*}"}" - host="${host#"${host%%[![:space:]]*}"}" - host="${host%"${host##*[![:space:]]}"}" - print_row 10 " ${LABEL}上次登录${FG_RESET} " "${TEXT}|${time}" - if is_ipv4_address "$host" || is_ipv6_address "$host"; then - kind=ip - elif is_hostname "$host"; then - kind=host - else - kind="" + local user="${PAM_USER:-}" raw time host kind rule width index + width=$(( COLS - 2 )) + (( width <= 38 )) || width=38 + rule="" + for (( index = 0; index < width; index++ )); do rule+="━"; done + if raw=$(login_query "$user"); then + time=$(login_normalize_time "${raw%%[[:space:]]*}") || time="" + if [[ -n "$time" ]]; then + host="${raw#"${raw%%[[:space:]]*}"}" + host="${host#"${host%%[![:space:]]*}"}" + host="${host%"${host##*[![:space:]]}"}" + print_row 10 " ${LABEL}上次登录${FG_RESET} " "${TEXT}|${time}" + if is_ipv4_address "$host" || is_ipv6_address "$host"; then + kind=ip + elif is_hostname "$host"; then + kind=host + else + kind="" + fi + case "$kind" in + ip) print_row 10 " ${LABEL}来源 IP${FG_RESET} " "${LOGIN_SOURCE}|${host}" ;; + host) print_row 10 " ${LABEL}来源${FG_RESET} " "${LOGIN_SOURCE}|${host}" ;; + esac + fi fi - case "$kind" in - ip) print_row 10 " ${LABEL}来源 IP${FG_RESET} " "${LOGIN_SOURCE}|${host}" ;; - host) print_row 10 " ${LABEL}来源${FG_RESET} " "${LOGIN_SOURCE}|${host}" ;; - esac + # 仅 PAM 登录渲染路径:登录记录之后、shell 提示符之前的一条加粗分隔线。 + printf '%s%s%s\n' "$TRACK" "$rule" "$RESET" } [[ -n "$hostname_value" ]] || hostname_value=$(uname -n) kernel=$(uname -r) @@ -2307,23 +2316,22 @@ if [[ -n "${NO_COLOR+x}" || ( "${TERM:-}" == dumb && "${TERM@a}" == *x* ) ]]; th KERNEL=""; LOGIN_SOURCE="" SECTION=""; BOLD=""; UNBOLD=""; GREEN=""; YELLOW=""; RED="" else - # PAM 输出时拿不到终端主题提示,固定真彩色会在浅色主题上失去对比。 - # 正文、标签和普通说明用默认前景(39),由终端主题决定明暗; - # 强调项只用基本 ANSI 主题索引(30-37),让客户端映射到自己的调色板。 + # 主机名、系统版本和内核版本按定稿使用深浅终端共用的固定前景; + # 正文、标签和其他字段继续用默认前景(39)或基本 ANSI 主题索引,由客户端映射。 RESET="${ESC}[0m" FG_RESET="${ESC}[39m" TEXT="${ESC}[39m" LABEL="${ESC}[39m" DIM="${ESC}[39m" - HOST="${ESC}[34m" + HOST="${ESC}[38;2;8;96;196m" RULE="${ESC}[39m" - SYSTEM="${ESC}[35m" + SYSTEM="${ESC}[38;2;179;38;117m" CPU="${ESC}[33m" ARCH="${ESC}[39m" UPTIME="${ESC}[39m" UPTIME_ACCENT="${ESC}[32m" LOAD="${ESC}[36m" - KERNEL="${ESC}[33m" + KERNEL="${ESC}[38;2;150;80;0m" LOGIN_SOURCE="${ESC}[35m" TRACK="${ESC}[39m" SECTION="${ESC}[39m" @@ -2385,9 +2393,9 @@ make_bar() { for (( i = 0; i < BAR_CELLS; i++ )); do if (( i < filled )); then - bar+="${color}━" + bar+="${color}█" else - bar+="${TRACK}─" + bar+="${TRACK}━" fi done printf '%s' "$bar" @@ -2559,11 +2567,11 @@ fi hero_width=$(( COLS - 2 )) (( hero_width <= 38 )) || hero_width=38 hero_rule="" -for (( hero_index = 0; hero_index < hero_width; hero_index++ )); do hero_rule+="─"; done +for (( hero_index = 0; hero_index < hero_width; hero_index++ )); do hero_rule+="━"; done printf '\n' -print_row 11 "${RULE}│${RESET} ${BOLD}${TEXT}欢迎登录 " "${HOST}|${hostname_value}" -print_row 2 "${RULE}│${RESET} " "${DIM}|今天想做点什么?" +print_row 11 "${RULE}┃${RESET} ${BOLD}${TEXT}欢迎登录 " "${HOST}|${hostname_value}" +print_row 2 "${RULE}┃${RESET} " "${DIM}|今天想做点什么?" printf '%s%s%s\n' "$SECTION" "$hero_rule" "$RESET" print_row 7 " ${LABEL}系统${FG_RESET} " "${SYSTEM}${BOLD}|${system_value}" print_row 7 " ${LABEL}运行${FG_RESET} " "${uptime_segments[@]}" diff --git a/tests/test-motd.sh b/tests/test-motd.sh index c8f9f30..c7f1689 100644 --- a/tests/test-motd.sh +++ b/tests/test-motd.sh @@ -251,13 +251,13 @@ extract_payload "$ROOT_DIR/tools/setup-motd.sh">"$TEST_DIR/t"; extract_payload "$ROOT_DIR/modules/system-customize.sh">"$TEST_DIR/m"; cmp -s "$TEST_DIR/t" "$TEST_DIR/m"||fail payload; pass "MOTD payloads stay identical"; -assert_eq 0537c91cb69d1c3360f3302ee515528abf4ba014f018735a1dbebf6ecbcce836 "$(sha256sum "$TEST_DIR/t"|awk '{print $1}')" "payload hash" +assert_eq 04152b5c08fd321c7d64046d82c3d1e8a90a287ace7a2ca505d0245b4de07ce1 "$(sha256sum "$TEST_DIR/t"|awk '{print $1}')" "payload hash" # 固定版式断言按显式 80 列跑,避免宿主长主机名/云内核在 40 列兜底里自然折行后误失败。 payload_output=$(COLUMNS=80 bash "$TEST_DIR/t" | sed -E $'s/\033\\[[0-9;]*m//g' | sed 's/[[:space:]]*$//'); payload_lines=$(printf '%s\n' "$payload_output" | sed '/^$/d'); -assert_eq "│ 欢迎登录 $(hostname)" "$(printf '%s\n' "$payload_lines" | sed -n '1p')" "payload welcome line"; -assert_eq '│ 今天想做点什么?' "$(printf '%s\n' "$payload_lines" | sed -n '2p')" "payload prompt line"; -assert_eq "$(printf '─%.0s' {1..38})" "$(printf '%s\n' "$payload_lines" | sed -n '3p')" "payload hero divider"; +assert_eq "┃ 欢迎登录 $(hostname)" "$(printf '%s\n' "$payload_lines" | sed -n '1p')" "payload welcome line"; +assert_eq '┃ 今天想做点什么?' "$(printf '%s\n' "$payload_lines" | sed -n '2p')" "payload prompt line"; +assert_eq "$(printf '━%.0s' {1..38})" "$(printf '%s\n' "$payload_lines" | sed -n '3p')" "payload hero divider"; assert_eq '系统,运行,CPU,负载,内存,根盘,内核' "$(printf '%s\n' "$payload_lines" | sed -n '4,10p' | awk '{printf "%s%s", sep, $1; sep=","}')" "payload info order"; assert_eq 10 "$(printf '%s\n' "$payload_lines" | wc -l)" "payload panel line count"; ! COLUMNS=80 bash "$TEST_DIR/t" | grep -qF $'\033[48;' || fail "payload must not paint a background"; @@ -265,11 +265,16 @@ raw_title=$(COLUMNS=80 bash "$TEST_DIR/t" | sed '/^$/d' | sed -n '1p'); [[ "$raw_title" == *$'\033[0m' ]] || fail "payload title must end with a full reset"; [[ "$raw_title" != *$'\033[39m' ]] || fail "payload title must not end with foreground-only reset"; pass "payload uses no background fill"; -bar_cells(){ local cells; cells=$(printf '%s' "$1" | sed 's/[^━─]//g' | LC_ALL=C wc -c); printf '%s' $((cells / 3)); } +bar_cells(){ local cells; cells=$(printf '%s' "$1" | sed 's/[^█━]//g' | LC_ALL=C wc -c); printf '%s' $((cells / 3)); } +bar_filled(){ local cells; cells=$(printf '%s' "$1" | sed 's/[^█]//g' | LC_ALL=C wc -c); printf '%s' $((cells / 3)); } mem_line=$(printf '%s\n' "$payload_lines" | sed -n '8p'); disk_line=$(printf '%s\n' "$payload_lines" | sed -n '9p'); assert_eq 6 "$(bar_cells "$mem_line")" "memory bar has 6 cells"; assert_eq 6 "$(bar_cells "$disk_line")" "disk bar has 6 cells"; +# 填充用块字符、轨道用重线,两者粗细可区分;每次运行都必须同时包含两种字符。 +[[ "$mem_line" == *'█'*'━'* ]] || fail "memory bar must mix block fill with heavy track"; +[[ "$disk_line" == *'█'*'━'* ]] || fail "disk bar must mix block fill with heavy track"; +[[ "$mem_line" != *'─'* && "$disk_line" != *'─'* ]] || fail "bars must not keep the thin track"; [[ "$mem_line" == *%*' '* ]] || fail "memory bar follows percent"; [[ "$disk_line" == *%*' '* ]] || fail "disk bar follows percent"; for plain_line in 4 5 6 7 10; do @@ -278,29 +283,30 @@ done ! printf '%s\n' "$payload_lines" | sed -n '4p' | grep -q '●' || fail "system line must not keep the green dot"; assert_eq " 内核 $(uname -r)($(uname -m))" "$(printf '%s\n' "$payload_lines" | sed -n '10p')" "payload kernel arch"; pass "payload bars limited to memory and root disk"; -# 主题自适应配色:PAM 输出没有终端主题提示,正文/标签/普通说明继承默认前景, -# 强调项只用基本 ANSI 索引,由客户端主题决定明暗。固定的 256 色或真彩色会在浅色背景上消失。 +# 定稿配色:主机名/系统版本/内核版本用深浅终端共用的三条固定 RGB,其余字段 +# 继续用默认前景(39)或基本 ANSI 主题索引,由客户端主题决定明暗。 theme_raw=$(COLUMNS=80 bash "$TEST_DIR/t" | sed '/^$/d'); # 用计数断言而不是 `grep -q` 管道:`-q` 提前关闭管道可能触发 SIGPIPE,让禁用项静默通过。 -assert_eq 0 "$(printf '%s\n' "$theme_raw" | grep -cF $'\033[38;' || true)" "payload must not use fixed truecolour or 256-colour foregrounds"; +assert_eq 3 "$(printf '%s\n' "$theme_raw" | grep -oF $'\033[38;2;' | wc -l || true)" "only the three pinned fields may use fixed RGB"; assert_eq 0 "$(printf '%s\n' "$theme_raw" | grep -cE $'\033\[(9[0-7]|10[0-7])m' || true)" "payload must not use bright colour indices"; +assert_eq 0 "$(printf '%s\n' "$theme_raw" | grep -cE $'\033\[38;5;' || true)" "payload must not use 256-colour foregrounds"; hero_raw=$(printf '%s\n' "$theme_raw" | sed -n '1p'); -[[ "$hero_raw" == *$'\033[34m'*"$(hostname)"* ]] || fail "hostname must use theme blue"; +[[ "$hero_raw" == *$'\033[38;2;8;96;196m'*"$(hostname)"* ]] || fail "hostname must use the shared fixed #0860C4"; [[ "$hero_raw" == *$'\033[39m欢迎登录 '* ]] || fail "welcome text must inherit the default foreground"; system_raw=$(printf '%s\n' "$theme_raw" | sed -n '4p'); -[[ "$system_raw" == *$'\033[35m'*$'\033[1m'* ]] || fail "system version must be bold theme magenta"; +[[ "$system_raw" == *$'\033[38;2;179;38;117m'*$'\033[1m'* ]] || fail "system version must be bold fixed #B32675"; [[ "$system_raw" != *'●'* ]] || fail "system line must not carry the status dot"; [[ "$system_raw" == *$'\033[39m系统'* ]] || fail "labels must inherit the default foreground"; load_raw=$(printf '%s\n' "$theme_raw" | sed -n '7p'); -[[ "$load_raw" == *$'\033[36m'* ]] || fail "load must use theme cyan"; +[[ "$load_raw" == *$'\033[36m'* ]] || fail "load must keep theme cyan"; kernel_raw_check=$(printf '%s\n' "$theme_raw" | sed -n '10p'); -[[ "$kernel_raw_check" == *$'\033[33m'*$'\033[1m'*"$(uname -r)"* ]] || fail "kernel must be bold theme yellow"; +[[ "$kernel_raw_check" == *$'\033[38;2;150;80;0m'*$'\033[1m'*"$(uname -r)"* ]] || fail "kernel must be bold fixed #965000"; [[ "$kernel_raw_check" == *$'\033[22m'*$'\033[39m('* ]] || fail "architecture must inherit the default foreground and leave the bold run"; hero_rule_raw=$(printf '%s\n' "$theme_raw" | sed -n '3p'); [[ "$hero_rule_raw" == $'\033[39m'* ]] || fail "hero divider must inherit the default foreground"; grep -qF 'GREEN="${ESC}[32m"' "$TEST_DIR/t" || fail "healthy memory/disk colour must use theme green"; grep -qF 'TRACK="${ESC}[39m"' "$TEST_DIR/t" || fail "progress track must inherit the default foreground"; -pass "payload follows the terminal theme instead of fixed colours" +pass "payload pins only hostname, system and kernel to the shared fixed colours" mkdir "$TEST_DIR/bin-accent" cat >"$TEST_DIR/bin-accent/motd-fixture"<<'FIXTURE' #!/usr/bin/env bash @@ -431,6 +437,10 @@ colored_text=$(printf '%s' "$colored_out"|sed -E $'s/\033\[[0-9;]*m//g'); # 只钉一个动态字段,确认两次运行确实读到了夹具而不是宿主实时值。 [[ "$colored_text" == *'0.13 / 0.27 / 0.41'* ]] || fail "stable fixture inputs were not applied"; [[ "$plain_out" == "$colored_text" ]] || fail "NO_COLOR text differs from colored text"; +# 稳定夹具:内存恰 50% → 3 格填充,根盘 75% → 5 格填充,验证粗细与填充比例。 +stable_lines=$(printf '%s\n' "$plain_out" | sed '/^$/d'); +assert_eq 3 "$(bar_filled "$(printf '%s\n' "$stable_lines" | sed -n '8p')")" "stable fixture memory bar fills 3 of 6"; +assert_eq 5 "$(bar_filled "$(printf '%s\n' "$stable_lines" | sed -n '9p')")" "stable fixture disk bar fills 5 of 6"; pass "payload honours NO_COLOR without changing text" dumb_out=$(env TERM=dumb COLUMNS=80 bash "$TEST_DIR/t"); @@ -1671,6 +1681,11 @@ login_payload() { COLUMNS=80 PAM_USER="$1" PATH="$TEST_DIR/login-bin:$PATH" bash [[ "$out" != *$'\033[38;'* ]] || fail "last-login must not carry a fixed RGB foreground" [[ "$out" == *$'\033[35m2001:db8::42'* ]] || fail "source must use theme magenta" ! printf '%s' "$plain" | grep -q '欢迎登录\|系统 \|内核' || fail "--last-login must not repeat the welcome panel" + # 底部横线只在 PAM 登录渲染路径出现:位于登录记录之后、比两条记录都靠下。 + rule_line=$(printf '%s\n' "$plain" | grep -n '^━' | tail -1 | cut -d: -f1) + [[ -n "$rule_line" ]] || fail "login path must end with a heavy rule" + assert_eq 3 "$rule_line" "bottom rule follows the login record" + assert_eq "$(printf '━%.0s' {1..38})" "$(printf '%s\n' "$plain" | sed -n "${rule_line}p")" "bottom rule is 38 heavy cells" pass "IPv4/IPv6 time and source render in the approved colours" ) ( @@ -1683,8 +1698,10 @@ login_payload() { COLUMNS=80 PAM_USER="$1" PATH="$TEST_DIR/login-bin:$PATH" bash ( root="$TEST_DIR/login-nohistory"; login_fixture "$root" '' load_motd; out=$(login_payload root) - [[ -z "$out" ]] || fail "missing history must print nothing, got: $out" - pass "missing history stays silent" + plain=$(printf '%s' "$out" | sed -E $'s/\033\\[[0-9;]*m//g') + assert_eq "$(printf '━%.0s' {1..38})" "$plain" "missing history prints only the bottom rule" + [[ "$out" == *$'\033[39m'* ]] || fail "bottom rule must inherit the default foreground" + pass "missing history keeps the bottom rule only" ) ( root="$TEST_DIR/login-ipnohost"; login_fixture "$root" '2026-12-01T03:25:52+08:00 ' diff --git a/tools/setup-motd.sh b/tools/setup-motd.sh index 5cc849a..ad64210 100755 --- a/tools/setup-motd.sh +++ b/tools/setup-motd.sh @@ -24,7 +24,7 @@ fi readonly MOTD_ETC_ROOT MOTD_STATE_DIR MOTD_LOCK_FILE MOTD_TRANSACTION_PARENT MOTD_TRUSTED_UID MOTD_TRUSTED_GID readonly MOTD_SCRIPT="$MOTD_ETC_ROOT/update-motd.d/00-custom-welcome" readonly MOTD_SNAPSHOT_VERSION=1 -readonly MOTD_PAYLOAD_SHA256="0537c91cb69d1c3360f3302ee515528abf4ba014f018735a1dbebf6ecbcce836" +readonly MOTD_PAYLOAD_SHA256="04152b5c08fd321c7d64046d82c3d1e8a90a287ace7a2ca505d0245b4de07ce1" readonly -a MOTD_TARGET_IDS=(motd issue issue_net custom_welcome uname motd_news) readonly -a MOTD_TARGET_PATHS=( "$MOTD_ETC_ROOT/motd" "$MOTD_ETC_ROOT/issue" "$MOTD_ETC_ROOT/issue.net" @@ -2156,24 +2156,33 @@ login_query() { } render_last_login() { - local user="${PAM_USER:-}" raw time host kind - raw=$(login_query "$user") || return 0 - time=$(login_normalize_time "${raw%%[[:space:]]*}") || return 0 - host="${raw#"${raw%%[[:space:]]*}"}" - host="${host#"${host%%[![:space:]]*}"}" - host="${host%"${host##*[![:space:]]}"}" - print_row 10 " ${LABEL}上次登录${FG_RESET} " "${TEXT}|${time}" - if is_ipv4_address "$host" || is_ipv6_address "$host"; then - kind=ip - elif is_hostname "$host"; then - kind=host - else - kind="" + local user="${PAM_USER:-}" raw time host kind rule width index + width=$(( COLS - 2 )) + (( width <= 38 )) || width=38 + rule="" + for (( index = 0; index < width; index++ )); do rule+="━"; done + if raw=$(login_query "$user"); then + time=$(login_normalize_time "${raw%%[[:space:]]*}") || time="" + if [[ -n "$time" ]]; then + host="${raw#"${raw%%[[:space:]]*}"}" + host="${host#"${host%%[![:space:]]*}"}" + host="${host%"${host##*[![:space:]]}"}" + print_row 10 " ${LABEL}上次登录${FG_RESET} " "${TEXT}|${time}" + if is_ipv4_address "$host" || is_ipv6_address "$host"; then + kind=ip + elif is_hostname "$host"; then + kind=host + else + kind="" + fi + case "$kind" in + ip) print_row 10 " ${LABEL}来源 IP${FG_RESET} " "${LOGIN_SOURCE}|${host}" ;; + host) print_row 10 " ${LABEL}来源${FG_RESET} " "${LOGIN_SOURCE}|${host}" ;; + esac + fi fi - case "$kind" in - ip) print_row 10 " ${LABEL}来源 IP${FG_RESET} " "${LOGIN_SOURCE}|${host}" ;; - host) print_row 10 " ${LABEL}来源${FG_RESET} " "${LOGIN_SOURCE}|${host}" ;; - esac + # 仅 PAM 登录渲染路径:登录记录之后、shell 提示符之前的一条加粗分隔线。 + printf '%s%s%s\n' "$TRACK" "$rule" "$RESET" } [[ -n "$hostname_value" ]] || hostname_value=$(uname -n) kernel=$(uname -r) @@ -2293,23 +2302,22 @@ if [[ -n "${NO_COLOR+x}" || ( "${TERM:-}" == dumb && "${TERM@a}" == *x* ) ]]; th KERNEL=""; LOGIN_SOURCE="" SECTION=""; BOLD=""; UNBOLD=""; GREEN=""; YELLOW=""; RED="" else - # PAM 输出时拿不到终端主题提示,固定真彩色会在浅色主题上失去对比。 - # 正文、标签和普通说明用默认前景(39),由终端主题决定明暗; - # 强调项只用基本 ANSI 主题索引(30-37),让客户端映射到自己的调色板。 + # 主机名、系统版本和内核版本按定稿使用深浅终端共用的固定前景; + # 正文、标签和其他字段继续用默认前景(39)或基本 ANSI 主题索引,由客户端映射。 RESET="${ESC}[0m" FG_RESET="${ESC}[39m" TEXT="${ESC}[39m" LABEL="${ESC}[39m" DIM="${ESC}[39m" - HOST="${ESC}[34m" + HOST="${ESC}[38;2;8;96;196m" RULE="${ESC}[39m" - SYSTEM="${ESC}[35m" + SYSTEM="${ESC}[38;2;179;38;117m" CPU="${ESC}[33m" ARCH="${ESC}[39m" UPTIME="${ESC}[39m" UPTIME_ACCENT="${ESC}[32m" LOAD="${ESC}[36m" - KERNEL="${ESC}[33m" + KERNEL="${ESC}[38;2;150;80;0m" LOGIN_SOURCE="${ESC}[35m" TRACK="${ESC}[39m" SECTION="${ESC}[39m" @@ -2371,9 +2379,9 @@ make_bar() { for (( i = 0; i < BAR_CELLS; i++ )); do if (( i < filled )); then - bar+="${color}━" + bar+="${color}█" else - bar+="${TRACK}─" + bar+="${TRACK}━" fi done printf '%s' "$bar" @@ -2545,11 +2553,11 @@ fi hero_width=$(( COLS - 2 )) (( hero_width <= 38 )) || hero_width=38 hero_rule="" -for (( hero_index = 0; hero_index < hero_width; hero_index++ )); do hero_rule+="─"; done +for (( hero_index = 0; hero_index < hero_width; hero_index++ )); do hero_rule+="━"; done printf '\n' -print_row 11 "${RULE}│${RESET} ${BOLD}${TEXT}欢迎登录 " "${HOST}|${hostname_value}" -print_row 2 "${RULE}│${RESET} " "${DIM}|今天想做点什么?" +print_row 11 "${RULE}┃${RESET} ${BOLD}${TEXT}欢迎登录 " "${HOST}|${hostname_value}" +print_row 2 "${RULE}┃${RESET} " "${DIM}|今天想做点什么?" printf '%s%s%s\n' "$SECTION" "$hero_rule" "$RESET" print_row 7 " ${LABEL}系统${FG_RESET} " "${SYSTEM}${BOLD}|${system_value}" print_row 7 " ${LABEL}运行${FG_RESET} " "${uptime_segments[@]}" From 7f6b3078d65d3ccc2b68bbe37484c44391ee9af2 Mon Sep 17 00:00:00 2001 From: FORGE Date: Tue, 1 Dec 2026 08:26:13 +0800 Subject: [PATCH 2/3] test(motd): stop requiring both bar glyphs on live host usage A real host can legitimately sit at 0/6 or 6/6, so the live-output check now only asserts six cells drawn from the block/heavy pair and no thin track. Exact fill order and counts stay on the stable fixtures, plus new 0/4/100 percent cases for both endpoints. --- tests/test-motd.sh | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/tests/test-motd.sh b/tests/test-motd.sh index c7f1689..4bc5b37 100644 --- a/tests/test-motd.sh +++ b/tests/test-motd.sh @@ -265,16 +265,19 @@ raw_title=$(COLUMNS=80 bash "$TEST_DIR/t" | sed '/^$/d' | sed -n '1p'); [[ "$raw_title" == *$'\033[0m' ]] || fail "payload title must end with a full reset"; [[ "$raw_title" != *$'\033[39m' ]] || fail "payload title must not end with foreground-only reset"; pass "payload uses no background fill"; -bar_cells(){ local cells; cells=$(printf '%s' "$1" | sed 's/[^█━]//g' | LC_ALL=C wc -c); printf '%s' $((cells / 3)); } +bar_chars(){ printf '%s' "$1" | sed 's/[^█━]//g'; } +bar_cells(){ local cells; cells=$(bar_chars "$1" | LC_ALL=C wc -c); printf '%s' $((cells / 3)); } bar_filled(){ local cells; cells=$(printf '%s' "$1" | sed 's/[^█]//g' | LC_ALL=C wc -c); printf '%s' $((cells / 3)); } mem_line=$(printf '%s\n' "$payload_lines" | sed -n '8p'); disk_line=$(printf '%s\n' "$payload_lines" | sed -n '9p'); assert_eq 6 "$(bar_cells "$mem_line")" "memory bar has 6 cells"; assert_eq 6 "$(bar_cells "$disk_line")" "disk bar has 6 cells"; -# 填充用块字符、轨道用重线,两者粗细可区分;每次运行都必须同时包含两种字符。 -[[ "$mem_line" == *'█'*'━'* ]] || fail "memory bar must mix block fill with heavy track"; -[[ "$disk_line" == *'█'*'━'* ]] || fail "disk bar must mix block fill with heavy track"; -[[ "$mem_line" != *'─'* && "$disk_line" != *'─'* ]] || fail "bars must not keep the thin track"; +# 实时宿主占用率不确定:0/6 与 6/6 都是合法端点,这里只验格数与字符集; +# 精确填充与起止顺序放在下方稳定夹具(内存 50%、根盘 75%)及其他固定占用夹具里断言。 +for bar_line in "$mem_line" "$disk_line"; do + [[ "$(bar_chars "$bar_line")" =~ ^[█━]{6}$ ]] || fail "bar must be 6 cells from the heavy glyph pair"; + [[ "$bar_line" != *'─'* ]] || fail "bar must not keep the thin track"; +done [[ "$mem_line" == *%*' '* ]] || fail "memory bar follows percent"; [[ "$disk_line" == *%*' '* ]] || fail "disk bar follows percent"; for plain_line in 4 5 6 7 10; do @@ -437,10 +440,15 @@ colored_text=$(printf '%s' "$colored_out"|sed -E $'s/\033\[[0-9;]*m//g'); # 只钉一个动态字段,确认两次运行确实读到了夹具而不是宿主实时值。 [[ "$colored_text" == *'0.13 / 0.27 / 0.41'* ]] || fail "stable fixture inputs were not applied"; [[ "$plain_out" == "$colored_text" ]] || fail "NO_COLOR text differs from colored text"; -# 稳定夹具:内存恰 50% → 3 格填充,根盘 75% → 5 格填充,验证粗细与填充比例。 +# 稳定夹具:内存恰 50% → 3 格填充,根盘 75% → 5 格填充。 +# 按精确字符断言填充段在前、轨道段在后,粗细与填充比例都不会被颜色或空白掩盖。 stable_lines=$(printf '%s\n' "$plain_out" | sed '/^$/d'); -assert_eq 3 "$(bar_filled "$(printf '%s\n' "$stable_lines" | sed -n '8p')")" "stable fixture memory bar fills 3 of 6"; -assert_eq 5 "$(bar_filled "$(printf '%s\n' "$stable_lines" | sed -n '9p')")" "stable fixture disk bar fills 5 of 6"; +stable_mem=$(printf '%s\n' "$stable_lines" | sed -n '8p'); +stable_disk=$(printf '%s\n' "$stable_lines" | sed -n '9p'); +assert_eq '███━━━' "$(bar_chars "$stable_mem")" "stable fixture memory bar is 3 filled then 3 track"; +assert_eq '█████━' "$(bar_chars "$stable_disk")" "stable fixture disk bar is 5 filled then 1 track"; +assert_eq 3 "$(bar_filled "$stable_mem")" "stable fixture memory bar fills 3 of 6"; +assert_eq 5 "$(bar_filled "$stable_disk")" "stable fixture disk bar fills 5 of 6"; pass "payload honours NO_COLOR without changing text" dumb_out=$(env TERM=dumb COLUMNS=80 bash "$TEST_DIR/t"); @@ -455,6 +463,23 @@ unset_term_text=$(printf '%s' "$unset_term_out"|sed -E $'s/\033\[[0-9;]*m//g'); [[ "$unset_term_text" == "$colored_text" ]] || fail "missing TERM text differs from coloured text"; pass "payload keeps colour when TERM is absent" +mkdir "$TEST_DIR/bin-df" +cat >"$TEST_DIR/bin-df/df"<<'DF' +#!/usr/bin/env bash +case "$1" in + -Ph) printf '%s\n' 'Filesystem Size Used Avail Use% Mounted on' "/dev/vda1 100G ${DF_USED:-1G} 1G ${DF_PERCENT:-4}% /" ;; + *) printf '%s\n' 'Filesystem 1024-blocks Used Available Capacity Mounted on' "/dev/vda1 104857600 1048576 103809024 ${DF_PERCENT:-4}% /" ;; +esac +DF +chmod 0700 "$TEST_DIR/bin-df/df"; +stable_disk_chars(){ COLUMNS=80 PATH="$TEST_DIR/bin-df:$PATH" bash "$TEST_DIR/t" | sed -E $'s/\033\[[0-9;]*m//g' | sed -n '/根盘/p' | sed 's/[^█━]//g'; } +# 4% → 0.24 格四舍五入为 0:合法空条,不得要求出现填充字符。 +assert_eq '━━━━━━' "$(DF_PERCENT=4 stable_disk_chars)" "disk bar at 4% is six empty heavy cells"; +# 0% 与 100% 是两端边界,填充格数与字符顺序都必须精确。 +assert_eq '━━━━━━' "$(DF_PERCENT=0 stable_disk_chars)" "disk bar at 0% is six empty heavy cells"; +assert_eq '██████' "$(DF_PERCENT=100 stable_disk_chars)" "disk bar at 100% is six filled block cells"; +pass "disk bar handles 0/4/100 percent endpoints with exact glyph order" + mkdir "$TEST_DIR/bin-alert" cat >"$TEST_DIR/bin-alert/df"<<'DF' #!/usr/bin/env bash From 4a77d32a8fad802f48aeb0eec76da9c5f719914b Mon Sep 17 00:00:00 2001 From: FORGE Date: Tue, 1 Dec 2026 08:30:59 +0800 Subject: [PATCH 3/3] test(motd): read bar glyphs without locale-dependent character classes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Debian's C locale made [[ =~ [█━]{6} ]] and the sed bracket classes treat the multi-byte glyphs as bytes, so a valid bar was rejected. The bar is now cut after the percent sign with parameter expansion and validated by stripping the two literal glyphs, which behaves the same in C and C.UTF-8. --- tests/test-motd.sh | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/tests/test-motd.sh b/tests/test-motd.sh index 4bc5b37..b516a2a 100644 --- a/tests/test-motd.sh +++ b/tests/test-motd.sh @@ -265,9 +265,13 @@ raw_title=$(COLUMNS=80 bash "$TEST_DIR/t" | sed '/^$/d' | sed -n '1p'); [[ "$raw_title" == *$'\033[0m' ]] || fail "payload title must end with a full reset"; [[ "$raw_title" != *$'\033[39m' ]] || fail "payload title must not end with foreground-only reset"; pass "payload uses no background fill"; -bar_chars(){ printf '%s' "$1" | sed 's/[^█━]//g'; } -bar_cells(){ local cells; cells=$(bar_chars "$1" | LC_ALL=C wc -c); printf '%s' $((cells / 3)); } -bar_filled(){ local cells; cells=$(printf '%s' "$1" | sed 's/[^█]//g' | LC_ALL=C wc -c); printf '%s' $((cells / 3)); } +# 进度条位于百分号后的一段空白之后;用字面量与 Bash 参数扩展取条, +# 不用 sed/[[ =~ ]] 的 Unicode 字符类——C locale 下它们按字节处理会错判。 +bar_text(){ local rest; [[ "$1" == *%* ]] || return 0; rest=${1##*%}; rest=${rest#"${rest%%[! ]*}"}; printf '%s' "$rest"; } +bar_bytes(){ printf '%s' "$1" | LC_ALL=C wc -c; } +bar_cells(){ local bar n; bar=$(bar_text "$1"); n=$(bar_bytes "$bar"); printf '%s' $((n / 3)); } +bar_filled(){ local bar n track; bar=$(bar_text "$1"); n=$(bar_bytes "$bar"); track=$(bar_bytes "${bar//█/}"); printf '%s' $(((n - track) / 3)); } +bar_valid(){ local bar rest; bar=$(bar_text "$1"); rest=${bar//█/}; rest=${rest//━/}; [[ -z "$rest" ]] && (( $(bar_bytes "$bar") % 3 == 0 )); } mem_line=$(printf '%s\n' "$payload_lines" | sed -n '8p'); disk_line=$(printf '%s\n' "$payload_lines" | sed -n '9p'); assert_eq 6 "$(bar_cells "$mem_line")" "memory bar has 6 cells"; @@ -275,7 +279,7 @@ assert_eq 6 "$(bar_cells "$disk_line")" "disk bar has 6 cells"; # 实时宿主占用率不确定:0/6 与 6/6 都是合法端点,这里只验格数与字符集; # 精确填充与起止顺序放在下方稳定夹具(内存 50%、根盘 75%)及其他固定占用夹具里断言。 for bar_line in "$mem_line" "$disk_line"; do - [[ "$(bar_chars "$bar_line")" =~ ^[█━]{6}$ ]] || fail "bar must be 6 cells from the heavy glyph pair"; + bar_valid "$bar_line" || fail "bar must contain only the block/heavy glyphs"; [[ "$bar_line" != *'─'* ]] || fail "bar must not keep the thin track"; done [[ "$mem_line" == *%*' '* ]] || fail "memory bar follows percent"; @@ -445,8 +449,8 @@ colored_text=$(printf '%s' "$colored_out"|sed -E $'s/\033\[[0-9;]*m//g'); stable_lines=$(printf '%s\n' "$plain_out" | sed '/^$/d'); stable_mem=$(printf '%s\n' "$stable_lines" | sed -n '8p'); stable_disk=$(printf '%s\n' "$stable_lines" | sed -n '9p'); -assert_eq '███━━━' "$(bar_chars "$stable_mem")" "stable fixture memory bar is 3 filled then 3 track"; -assert_eq '█████━' "$(bar_chars "$stable_disk")" "stable fixture disk bar is 5 filled then 1 track"; +assert_eq '███━━━' "$(bar_text "$stable_mem")" "stable fixture memory bar is 3 filled then 3 track"; +assert_eq '█████━' "$(bar_text "$stable_disk")" "stable fixture disk bar is 5 filled then 1 track"; assert_eq 3 "$(bar_filled "$stable_mem")" "stable fixture memory bar fills 3 of 6"; assert_eq 5 "$(bar_filled "$stable_disk")" "stable fixture disk bar fills 5 of 6"; pass "payload honours NO_COLOR without changing text" @@ -472,12 +476,20 @@ case "$1" in esac DF chmod 0700 "$TEST_DIR/bin-df/df"; -stable_disk_chars(){ COLUMNS=80 PATH="$TEST_DIR/bin-df:$PATH" bash "$TEST_DIR/t" | sed -E $'s/\033\[[0-9;]*m//g' | sed -n '/根盘/p' | sed 's/[^█━]//g'; } +stable_disk_chars(){ local line; line=$(COLUMNS=80 PATH="$TEST_DIR/bin-df:$PATH" bash "$TEST_DIR/t" | sed -E $'s/\033\[[0-9;]*m//g' | sed -n '/根盘/p'); bar_text "$line"; } +stable_disk_line(){ COLUMNS=80 PATH="$TEST_DIR/bin-df:$PATH" bash "$TEST_DIR/t" | sed -E $'s/\033\[[0-9;]*m//g' | sed -n '/根盘/p'; } # 4% → 0.24 格四舍五入为 0:合法空条,不得要求出现填充字符。 assert_eq '━━━━━━' "$(DF_PERCENT=4 stable_disk_chars)" "disk bar at 4% is six empty heavy cells"; # 0% 与 100% 是两端边界,填充格数与字符顺序都必须精确。 assert_eq '━━━━━━' "$(DF_PERCENT=0 stable_disk_chars)" "disk bar at 0% is six empty heavy cells"; assert_eq '██████' "$(DF_PERCENT=100 stable_disk_chars)" "disk bar at 100% is six filled block cells"; +for df_percent in 0 4 100; do + df_line=$(DF_PERCENT="$df_percent" stable_disk_line) + bar_valid "$df_line" || fail "disk bar at $df_percent% must use only the block/heavy glyphs"; + assert_eq 6 "$(bar_cells "$df_line")" "disk bar at $df_percent% has 6 cells"; +done +assert_eq 0 "$(DF_PERCENT=4 bar_filled "$(DF_PERCENT=4 stable_disk_line)")" "disk bar at 4% rounds down to 0 filled"; +assert_eq 6 "$(DF_PERCENT=100 bar_filled "$(DF_PERCENT=100 stable_disk_line)")" "disk bar at 100% is fully filled"; pass "disk bar handles 0/4/100 percent endpoints with exact glyph order" mkdir "$TEST_DIR/bin-alert"