ncurses 等人如何做?等人。工作?
有几个类似 ncurses 的库可以帮助制作命令行 GUI。
简而言之,它们是如何工作的?
我的第一个想法是 ncurses 拦截所有键盘输入,并通过正常逐行输出来绘制每个“帧”。然而,仔细检查后发现,每个新帧都会覆盖前一帧。它如何修改已经输出的行?此外,它如何处理颜色?
编辑:同样的问题适用于任何具有“花哨”界面的东西,例如 vim 和 emacs。
There are several libraries like ncurses that assist in making command-line GUIs.
Simply put, how do they work?
My first thought was that ncurses intercepts all keyboard input, and draws each "frame" by outputting it line-by-line normally. Closer inspection, however, reveals that each new frame overwrites the previous one. How does it modify lines that have already been outputted? Furthermore, how does it handle color?
EDIT: The same question applies to anything with a "fancy" interface, like vim
and emacs
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
文本终端具有命令序列,可以执行诸如将光标移动到屏幕上的特定位置、插入字符、删除行等操作。
每种终端类型都不同,并且具有其自己的命令序列集。 ncurses 有一个数据库(详细信息请参见 terminfo)
ncurses 内部维护着 2 个屏幕视图:当前内容以及应用当前挂起的更改后屏幕的外观。一旦程序请求屏幕重绘,ncurses 就会计算出一种有效的方法来更新屏幕,使其看起来像所需的视图。确切的字符/命令序列输出取决于所使用的终端类型。
Text terminals have command sequences that do things like move the cursor to a particular position on the screen, insert characters, delete lines etc.
Each terminal type is different and has its own set of command sequences. ncurses has a databse (see terminfo for details)
Internally ncurses maintains 2 views of the screen: the current contents and what the screen should look like after the current pending changes are applied. Once the program requests a screen redraw, ncurses calculates an efficient way to update the screen to look like the desired view. The exact characters/command sequences output depend on what terminal type is in use.
curses(我认为还有 ncurses)通过在屏幕上移动光标来工作。有控制序列可以完成此类操作。再次查看代码,您就会看到它们。这些序列不是 ASCII 控制字符,它们可能是以 (umm...) ESC 开头的字符串。请查看此处以获得更高级别的解释。
curses (and ncurses, too, I think) works by moving the cursor around on the screen. There are control sequences to do such things. Take a look at the code again and you'll see them. These sequences are not ASCII control characters, they are strings starting with (umm...) ESC, maybe. Have a look here for a higher-level explanation.