Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 37 additions & 29 deletions modules/system-customize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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[@]}"
Expand Down
84 changes: 69 additions & 15 deletions tests/test-motd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -251,25 +251,37 @@ 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";
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)); }
# 进度条位于百分号后的一段空白之后;用字面量与 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";
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_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";
[[ "$disk_line" == *%*' '* ]] || fail "disk bar follows percent";
for plain_line in 4 5 6 7 10; do
Expand All @@ -278,29 +290,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
Expand Down Expand Up @@ -431,6 +444,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 格填充。
# 按精确字符断言填充段在前、轨道段在后,粗细与填充比例都不会被颜色或空白掩盖。
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_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"

dumb_out=$(env TERM=dumb COLUMNS=80 bash "$TEST_DIR/t");
Expand All @@ -445,6 +467,31 @@ 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(){ 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"
cat >"$TEST_DIR/bin-alert/df"<<'DF'
#!/usr/bin/env bash
Expand Down Expand Up @@ -1671,6 +1718,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"
)
(
Expand All @@ -1683,8 +1735,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 '
Expand Down
Loading