在 bash 脚本中使用“备用屏幕”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11023929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using the "alternate screen" in a bash script
提问by leemes
The alternate screenis used by many "user-interactive" terminal applications like vim, htop, screen, alsamixer, less, ... It is like a different buffer of the terminal content, which disappears when the application exits, so the whole terminal gets restored and it looks like the application hasn't output anything.
的备用屏幕用于通过像vim,HTOP,屏幕,alsamixer中,以下,...它像一个不同缓冲液中的终端的内容的,其消失的应用程序退出时,所以整个终端许多“用户交互式”应用终端得到恢复,看起来应用程序没有输出任何内容。
I'd like to achieve exactly the same thing in my own shell (bash) script, except that it doesn't have to be that portable. I'd stick to linux only and xterm-based terminal emulators; but the solution should use something like tputif it's possible. However, I don't want to use some external scripting language (or even something like C).
我想在我自己的 shell (bash) 脚本中实现完全相同的功能,只是它不必那么便携。我会坚持只使用 linux 和基于 xterm 的终端模拟器;但tput如果可能的话,解决方案应该使用类似的东西。但是,我不想使用一些外部脚本语言(甚至 C 之类的东西)。
Although I don't want to use C (as it should be a bash-script with as few dependencies as possible), I had a look into the source code of less. It seems to use terminfo as the database and looks up the "ti" terminal capability in its initialisation. When removing the line, it doesn't use the alternate sceen, so I assumed that I found the responsible code line.
虽然我不想使用 C(因为它应该是一个依赖尽可能少的 bash 脚本),但我查看了 less 的源代码。它似乎使用 terminfo 作为数据库并在其初始化中查找“ti”终端功能。删除该行时,它不使用备用屏幕,所以我假设我找到了负责的代码行。
However, I can't find such a capability in man terminfo. But maybe I'm on the wrong path finding a solution for this. Maybe terminfo / tput isn't my friend.
但是,我在man terminfo. 但也许我在寻找解决方案的错误道路上。也许 terminfo / tput 不是我的朋友。
So (how) can I use the alternate screen in a bash script? Does somebody know a simple application in which source code I may find a hint? (C application or bash script or whatever...)
那么(如何)我可以在 bash 脚本中使用备用屏幕吗?有人知道一个简单的应用程序,我可以在其中找到提示的源代码吗?(C 应用程序或 bash 脚本或其他任何...)
回答by rodrigo
You can switch to the alternate screen using this command:
您可以使用以下命令切换到备用屏幕:
$ tput smcup
And back with:
并返回:
$ tput rmcup
These commands just output the appropriate escape sequences for your terminal. If it is an XTERM they will be equivalent to the (more known but less elegant or portable):
这些命令只是为您的终端输出适当的转义序列。如果它是 XTERM,它们将等同于(更知名但不太优雅或便携):
$ echo -e "\e[?1049h"
And:
和:
$ echo -e "\e[?1049l"
For more terminal control commands see man 5 terminfo.
有关更多终端控制命令,请参阅man 5 terminfo。
回答by Thomas Dickey
smcup/rmcup are used, but only for the side effect: the escape sequence which switches between normal/alternate screens is usually embedded in those terminfo capabilities -- not always.
smcup/rmcup 被使用,但仅用于副作用:在正常/备用屏幕之间切换的转义序列通常嵌入在这些 terminfo 功能中——并非总是如此。
Some background is in the xterm faq Why doesn't the screen clear when running vi?
一些背景在 xterm 常见问题解答中为什么在运行 vi 时屏幕不清晰?
回答by golopot
For C console application:
对于 C 控制台应用程序:
ncurses
诅咒
Wikipedia:
维基百科:
ncurses (new curses) is a programming library that provides an API which allows the programmer to write text-based user interfaces in a terminal-independent manner.
ncurses(新的curses)是一个编程库,它提供了一个API,允许程序员以独立于终端的方式编写基于文本的用户界面。
lessuses this library.
less使用这个库。
A hello world program from here, to compile it in gcc, flag -lncurses is needed.
来自here的hello world程序,要在gcc中编译它,需要标志-lncurses。
#include <ncurses.h>
int main()
{
initscr(); /* Start curses mode */
printw("Hello World !!!"); /* Print Hello World */
refresh(); /* Print it on to the real screen */
getch(); /* Wait for user input */
endwin(); /* End curses mode */
return 0;
}

