Skip to content

fix(motd): detect terminal width outside command substitution - #110

Merged
LucaLin233 merged 2 commits into
mainfrom
forge/fix-motd-terminal-width-autodetect
Dec 2, 2026
Merged

LucaLin233 merged 2 commits into
mainfrom
forge/fix-motd-terminal-width-autodetect

Conversation

@LucaLin233

Copy link
Copy Markdown
Owner

Summary

MOTD 欢迎页在真实 32 列终端里仍按 40 列渲染,最宽行 38 列并折行。原因是 COLS=$(motd_columns) 在命令替换内执行,motd_columns 中的 [[ -t 1 ]] 看到的是捕获管道而不是终端,tput cols 从未运行。

本次在命令替换外先记录 stdout 是否为终端,再交给 motd_columns 使用;tools/setup-motd.sh 与 modules/system-customize.sh 的相同 heredoc 欢迎 payload 同步修改,哈希一致。

Scope

  • tools/setup-motd.sh:payload 内新增 motd_stdout_is_tty,motd_columns 改用它判断是否调用 tput;更新 payload 哈希。
  • modules/system-customize.sh:同一 payload 的等价修改与哈希更新。
  • tests/test-motd.sh:更新 payload 哈希;新增真实 32 列 PTY 回归,无 COLUMNS,验证宽度不超过 32 且欢迎行、CPU 行保留。payload 自身 stderr 在 script -c 子命令内单独重定向,wrapper stderr 另用文件,两处分别断言,改用 fd 2 的错误实现会回退 40 列并失败。

保持不变:显式 COLUMNS 优先、PAM/非 TTY 的 40 列兜底、不把 stderr 终端状态当作 stdout。

Validation

  • git diff --check:通过。
  • 改动脚本逐个 bash -n:tools/setup-motd.sh、modules/system-customize.sh、tests/test-motd.sh 均通过。
  • 两份 payload 逐字相同,SHA-256 为 1e1a2987751cbaa3fca0697e20f5bb9c16dad13fb28a61bf1540807fc67ac2b8。
  • 复现与修复对照(隔离工作树内只读渲染,非生产目标机):旧版在无 COLUMNS 的 32 列 PTY 输出最宽 38 列;修复后为 32 列,欢迎行与 CPU 行均在。
  • 本次未在本机运行完整测试套件,完整测试交 CI。

Risks

  • 宽度判定从函数内移到脚本启动时,语义不变:仅影响是否尝试 tput,显式 COLUMNS 与 40 列兜底仍优先/保留。
  • 真实 SSH/PAM 登录环境未实测;PTY 回归使用 script 提供的伪终端,不等同目标机登录链。

Rollback

还原本 PR 提交即可;payload 与哈希一并回到旧值,无数据或配置迁移。

LucaLin233 and others added 2 commits December 2, 2026 05:53
motd_columns ran inside $(...), where stdout is a pipe, so [[ -t 1 ]] was always false and a real 32-column terminal fell back to 40 columns. Capture the stdout TTY state before the substitution and pass it to the function, keeping explicit COLUMNS priority and the PAM 40-column fallback.

Cover the real PTY path with a regression test whose payload stderr is redirected inside the script child, so a fd 2 based implementation cannot pass.
@LucaLin233
LucaLin233 merged commit 6a20d44 into main Dec 2, 2026
16 checks passed
@LucaLin233
LucaLin233 deleted the forge/fix-motd-terminal-width-autodetect branch December 2, 2026 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant