假设我的提示看起来像这样(_ 代表我的光标)
~ % _
Run Code Online (Sandbox Code Playgroud)
有什么办法可以让它看起来像这样
~ % _
[some status]
Run Code Online (Sandbox Code Playgroud)
这个问题最初是关于 zsh 的,但现在有了其他答案。
Gil*_*il' 17
以下设置似乎有效。如果命令行溢出第一行,则第二行上的文本将消失。该preexec函数在运行命令之前擦除第二行;如果您想保留它,请更改为preexec () { echo; }。
terminfo_down_sc=$terminfo[cud1]$terminfo[cuu1]$terminfo[sc]$terminfo[cud1]
PS1_2='[some status]'
PS1="%{$terminfo_down_sc$PS1_2$terminfo[rc]%}%~ %# "
preexec () { print -rn -- $terminfo[el]; }
Run Code Online (Sandbox Code Playgroud)
%转义记录在 zsh 手册 ( man zshmisc) 中。
Terminfo 是一个终端访问 API。Zsh 有一个terminfo模块可以访问终端描述数据库:$terminfo[$cap]是要发送的字符序列,以行使终端的能力$cap,即运行其$cap命令。有关man 5 terminfo更多信息,请参阅(在 Linux 上,节号在其他 unice 上可能会有所不同)。
动作顺序是:将光标向下移动一行(cud1),然后再向上移动(cuu1);保存光标位置 ( sc); 将光标向下移动一行;打印[some status];恢复光标位置。开头的向下和向上位仅在提示位于屏幕底部的情况下才需要。preexec 行会擦除第二行 ( el),这样它就不会与命令的输出混淆。
如果第二行的文字比终端宽,显示可能会出现乱码。在紧要关头使用Ctrl+L进行修复。
这是bashGilles 的 zsh 解决方案的等价物。Bash 没有本地 terminfo 模块,但tput命令(与 捆绑在一起terminfo)做了很多相同的事情。
PS1_line1='\w \$ '
PS1_line2='[some status]'
if (tput cuu1 && tput sc && tput rc && tput el) >/dev/null 2>&1
then
PS1="
\[$(tput cuu1; tput sc)\]
\[${PS1_line2}$(tput rc)\]${PS1_line1}"
PS2="\[$(tput el)\]> "
trap 'tput el' DEBUG
else
PS1="$PS1_line2 :: $PS1_line1"
fi
Run Code Online (Sandbox Code Playgroud)
如果终端不支持其中一项功能,它将回退到单行提示。
该trap行是一种模拟 zshpreexec功能的hacky方式。有关更多信息,请参阅https://superuser.com/questions/175799/。
编辑:基于 Gilles 的评论改进了脚本。
| 归档时间: |
|
| 查看次数: |
3879 次 |
| 最近记录: |