xterm/vt102序列填充终端窗口,然后退出按键,清除屏幕上的任何东西?

时间:2022-08-29 00:19:52

I'm trying to wrap my head around VT102 sequences and how such libraries as ncurses work.

我试着用VT102序列和ncurses这样的库是如何工作的。

To give an example, when I open Emacs, it fills the terminal window. When I hit C-x C-c, the program exits and my terminal window looks exactly as it looked before I launched emacs (i.e. the line above my cursor shows the emacs command in my zsh history, not whatever emacs had rendered).

举个例子,当我打开Emacs时,它会填充终端窗口。当我点击C-x C-c时,程序退出,我的终端窗口看上去与我启动emacs之前的样子一模一样(例如,我的光标上方的一行显示了我zsh历史中的emacs命令,而不是emacs所显示的)。

As a sort of "hello world", I've written a little ruby script that opens /dev/tty, then writes \x1B[48;597m\n (blank lines, with a background color) for as many rows as there are in my terminal. The program then sleeps for 5 seconds, leaving me happily staring at a bright orange screen momentarily. Then it exits, leaving me at my zsh prompt, with 53 lines of bright orange up above it.

作为一种“hello world”,我编写了一个小ruby脚本,打开/dev/tty,然后编写\x1B[487;597 \n(带背景色的空白行),以满足终端中的行数。然后这个程序休眠了5秒,让我高兴地盯着一个亮橙色的屏幕。然后它就消失了,只剩下我的zsh提示符,上面有53行亮橙色。

Two questions:

两个问题:

  1. Is writing blank lines with a background color the correct way to fill the terminal window with color?
  2. 用背景色书写的空白行是用颜色填充终端窗口的正确方法吗?
  3. How do I completely clear the screen by sending some VT102 sequence to /dev/tty?
  4. 如何通过将VT102序列发送到/dev/tty来完全清除屏幕?

I am able to send \x1B[2J, but this does the same thing as when I execute the clear command, and actual just shifts all the content up the terminal window, until my cursor is at the very top of a blank window, rather than leaving my cursor wherever it would naturally be if my program had not written a load of blank lines to the screen.

我能发送\ x1B[2 j,但这并等于当我执行明确的命令,和实际变化的所有内容的终端窗口,直到我的光标是一个空白窗口的顶端,而不是让我的光标无论它自然会如果我的程序没有写一堆空行到屏幕上。

PS: Not sure what tags to use here that anybody is actually likely to be subscribed to.

PS:不确定在这里使用什么标签,任何人都有可能订阅。

1 个解决方案

#1


3  

Is writing blank lines with a background color the correct way to fill the terminal window with color?

用背景色书写的空白行是用颜色填充终端窗口的正确方法吗?

No. The correct way to do this (on a terminal that supports BCE*) is to set a background color, then erase the screen using an ED sequence (e.g, ^[ [ 2 J).

不。正确的方法(在支持BCE*的终端上)是设置背景颜色,然后使用ED序列(e)擦除屏幕。g ^[[2 J)。

*: most modern terminals do

*:大多数现代终端都是这样

How do I completely clear the screen by sending some VT102 sequence to /dev/tty?

如何通过将VT102序列发送到/dev/tty来完全清除屏幕?

Using ED. See above.

使用ED,见上图。

If what you actually want to do is switch to the alternate screen (like emacs is doing in your example), the alternate screen is controlled by private mode 1049. You can switch to it using ^[ [ ? 1 0 4 9 h, and switch back using ^[ [ ? 1 0 4 9 l. Note, however, that this is an xterm extension; it's not entirely standard, and wasn't supported by any DEC hardware terminals (e.g, the VT102).

如果您真正想要做的是切换到备用屏幕(就像您示例中的emacs那样),备用屏幕由private mode 1049控制。你可以切换到使用^[[?1 0 4 9 h,切换使用^[[?注意,这是一个xterm的扩展;它并不完全是标准的,也不支持任何DEC硬件终端(e)。g,VT102)。

#1


3  

Is writing blank lines with a background color the correct way to fill the terminal window with color?

用背景色书写的空白行是用颜色填充终端窗口的正确方法吗?

No. The correct way to do this (on a terminal that supports BCE*) is to set a background color, then erase the screen using an ED sequence (e.g, ^[ [ 2 J).

不。正确的方法(在支持BCE*的终端上)是设置背景颜色,然后使用ED序列(e)擦除屏幕。g ^[[2 J)。

*: most modern terminals do

*:大多数现代终端都是这样

How do I completely clear the screen by sending some VT102 sequence to /dev/tty?

如何通过将VT102序列发送到/dev/tty来完全清除屏幕?

Using ED. See above.

使用ED,见上图。

If what you actually want to do is switch to the alternate screen (like emacs is doing in your example), the alternate screen is controlled by private mode 1049. You can switch to it using ^[ [ ? 1 0 4 9 h, and switch back using ^[ [ ? 1 0 4 9 l. Note, however, that this is an xterm extension; it's not entirely standard, and wasn't supported by any DEC hardware terminals (e.g, the VT102).

如果您真正想要做的是切换到备用屏幕(就像您示例中的emacs那样),备用屏幕由private mode 1049控制。你可以切换到使用^[[?1 0 4 9 h,切换使用^[[?注意,这是一个xterm的扩展;它并不完全是标准的,也不支持任何DEC硬件终端(e)。g,VT102)。