使用“备用屏幕"在 bash 脚本中 [英] Using the "alternate screen" in a bash script

查看:19
本文介绍了使用“备用屏幕"在 bash 脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

alternate screen 被许多用户交互"终端应用程序使用,例如 vim、htop、screen、alsamixer、less……它就像终端内容的不同缓冲区,它应用程序退出时消失,所以整个终端都恢复了,看起来应用程序没有输出任何东西.

The alternate screen is 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.

我想在我自己的 shell (bash) 脚本中实现完全相同的功能,只是它不必那么便携.我会坚持只使用 linux 和基于 xterm 的终端模拟器;但是如果可能的话,解决方案应该使用类似 tput 的东西.但是,我不想使用一些外部脚本语言(甚至 C 之类的东西).

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 tput if it's possible. However, I don't want to use some external scripting language (or even something like C).

虽然我不想使用 C(因为它应该是一个依赖尽可能少的 bash 脚本),但我查看了 less 的源代码.它似乎使用 terminfo 作为数据库并在其初始化中查找ti"终端功能.删除该行时,它不使用备用屏幕,所以我假设我找到了负责的代码行.

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.

然而,我在man terminfo中找不到这样的能力.但也许我在寻找解决方案的错误道路上.也许 terminfo/tput 不是我的朋友.

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.

那么(如何)我可以在 bash 脚本中使用备用屏幕吗?有人知道一个简单的应用程序,我可以在其中找到提示的源代码吗?(C 应用程序或 bash 脚本或其他……)

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...)

推荐答案

您可以使用以下命令切换到备用屏幕:

You can switch to the alternate screen using this command:

$ tput smcup

然后返回:

$ tput rmcup

这些命令只是为您的终端输出适当的转义序列.如果是 XTERM,它们将等同于(更知名但不太优雅或便携):

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):

$ echo -e "e[?1049h"

还有:

$ echo -e "e[?1049l"

更多终端控制命令见man 5 terminfo.

这篇关于使用“备用屏幕"在 bash 脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆