获取 termcap 功能的终端状态

ibu*_*fen 2 scripting terminal tput terminfo

如何检索像smam和这样的终端设置的状态rmam

原因是我设置rmam的:

tput rmam
Run Code Online (Sandbox Code Playgroud)

在脚本中,然后smam在退出时继续设置:

tput smam
Run Code Online (Sandbox Code Playgroud)

但是如果终端rmam在脚本启动时设置,我不想smam在退出时设置。


如何才能做到这一点?

use*_*777 5

在支持它的终端模拟器上,您可以使用\033[?7$p转义符(“请求 DEC 私有模式”)来查询该参数(7=> 自动包装模式):

decrqm()(
    exec </dev/tty
    t=$(stty -g)
    trap 'stty "$t"; return' EXIT QUIT INT TERM
    stty -icanon -echo time 1 min 0
    e=$(printf '\033')
    printf "${e}[$1\$p" >/dev/tty
    case $(dd count=1 2>/dev/null) in
    "${e}[$1;1\$y") echo on;;
    "${e}[$1;2\$y") echo off;;
    *) echo unknown;;
    esac
)

$ tput smam  # printf '\033[?7h'
$ decrqm '?7'
on
$ tput rmam  # printf '\033[?7l'
$ decrqm '?7'
off
Run Code Online (Sandbox Code Playgroud)

更好的方法是在启动脚本时保存该设置,\033[?7s并在退出时恢复\033[?7r

save_am(){ printf '\033[?7s'; }
restore_am(){ printf '\033[?7r'; }

save_am
tput rmam
..
restore_am
Run Code Online (Sandbox Code Playgroud)

但是许多终端模拟器(特别是screentmux不支持这些转义。至少默认情况下不是。所以所有这些都是纯粹的琐事——你不能把它用于任何实际的事情;-)