跳到主要内容
ansicode

XTPUSHSGR / XTPOPSGR / XTREPORTSGR — SGR 栈(CSI Ps + p / + q / + r)

通过栈保存 / 恢复 / 查询当前 SGR 属性 —— XTPUSHCOLORS 在调色板栈外,对 SGR 属性的对应方案。

字节形式

涵盖所有常见的字符串字面量写法,方便正反查找。

\\x1b[\x1b[#{ (XTPUSHSGR, alt: CSI Pm + p) \x1b[#} (XTPOPSGR, alt: CSI + q) \x1b[+r (XTREPORTSGR)
\\033[\033[#{ / \033[#}
\\e[\e[#{ / \e[#}
ESC [ESC [ Pm + p / ESC [ + q / ESC [ + r
hex1b 5b ... 2b 70 / 2b 71 / 2b 72

说明

三个 xterm 扩展,用于堆叠 SGR(Select Graphic Rendition)状态 —— 加粗 / 斜体 / 下划线 / 前景色 / 背景色 / 闪烁 / 删除线 / 反相,即 `sgr-reset` 跟踪的全集。**XTPUSHSGR** 有两种等价形式:短形 `\x1b[#{` 保存当前 SGR 全量快照;长形 `\x1b[<Pm>+p` 仅保存 `Pm` 列出的属性(`1` 加粗、`3` 斜体、`4` 下划线、`5` 闪烁、`7` 反相、`9` 删除线、`30-37` / `40-47` / `38` / `48` 颜色)。**XTPOPSGR**(短 `\x1b[#}`,长 `\x1b[+q`)恢复栈顶 —— 实际发出的 SGR 属性为当前与保存状态之差,自动应用。**XTREPORTSGR**(`\x1b[+r`)以 `\x1b[<depth>+r` 形式回复栈深度。`#{` 与 `#}` 的双字节中间序列存在是因为它们比长形短且对解析器更友好(无参数)。适合在不丢失父上下文的前提下临时局部修改 SGR 的库 / TUI 原语(例如「焦点行外全部减淡」):压入 → 修改 → 渲染 → 弹出。注意 xterm 的 SGR 栈与调色板栈(`xtpushcolors-popcolors`)是**分离的** —— 压入一个不压入另一个。仅 xterm 支持;kitty / wezterm / ghostty 静默忽略(kitty 的 CSI u 键盘协议碰巧也用 `+` 作中间字节但语义不同)。

规范出处: xterm-ctlseqs (XTPUSHSGR / XTPOPSGR / XTREPORTSGR)

参数

Pm可选的 SGR 属性编码列表(仅长形压入)。省略 = 保存当前全部 SGR 状态。

示例

bash
printf '\033[1;31m'   # bold red\nprintf '\033[#{'      # push current SGR\nprintf '\033[0;34mlinkified word\033[#}'   # mutate, render, pop → bold red restored\necho ' continues bold red'
python
import sys\nsys.stdout.write('\x1b[#{')   # save all SGR\nsys.stdout.write('\x1b[2;33mdim yellow status')\nsys.stdout.write('\x1b[#}')   # restore parent SGR
go
// Save only foreground color (long form):\nfmt.Print("\x1b[38+p")   // push fg\nfmt.Print("\x1b[31malert\x1b[+q")   // pop restores fg
javascript
process.stdout.write('\x1b[+r')   // query stack depth — reply: \x1b[<n>+r
c
printf("\x1b[#{");   /* push */\nprintf("\x1b[7m local highlight \x1b[#}");  /* pop restores */

终端支持

xterm
支持
Linux console (fbcon)
不支持
macOS Terminal.app
不支持
iTerm2
不支持
Windows Terminal
不支持
cmd.exe / ConPTY
不支持
kitty
不支持
alacritty
不支持
WezTerm
不支持
Ghostty
不支持
GNOME Terminal
不支持
Konsole
不支持
tmux
不支持
GNU screen
不支持

相关序列