历史记录 - 控制屏幕在适当位置滚动文本(向上箭头 -> 在适当位置/相同位置循环显示命令)
当您滚动浏览历史记录时,文本会根据提示发生变化。我已经查看了所涉及的结构,但还没有足够深入地研究源代码,以找出它如何更改屏幕上的文本而不更改其上面的所有文本(提示符,已打印出的过去文本。)
它不能只是清除屏幕并重新打印所有内容,而仅在提示后进行微小的更改 - 那样效率非常低。他们是如何做到这一点的?
这可能听起来很愚蠢,但我尝试打印一个退格字符,这会导致将光标移动到提示中,当您键入时,它只会写在提示上方。
我只是不明白他们如何打印文本并更改它或循环浏览文本。
如果有人知道这是如何完成的,我将非常感激。
谢谢
When you scroll through the history the text changes at the prompt. I've looked at the structures involved but haven't looked deep enough in the source code to find out how it changes the text on the screen without changing all the text above it (the prompt, past text that has been printed out.)
It can't just clear the screen and reprint everything out with only the minor change after the prompt-- that would just be so inefficient. How do they do this?
It probably sounds really dumb, but I tried printing a backspace character which results in moving the cursor into the prompt in which when you type it just writes over top of the prompt.
I just can't figure how they print out text and change it or cycle through text in place.
If anyone knows how this is done I would really appreciate it.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
bash
使用 GNUreadline
库 来执行此操作。它最终是terminfo
库的包装器(来自 System V 或ncurses
库),其目的是使用终端描述来弄清楚如何进行这些更新。如果终端类型未知或声明不正确,您会发现要么最终得到垃圾,要么所有漂亮的就地更新都消失了。终端描述看起来像
并描述了如何进行各种屏幕更新、可用的功能和操作键以及它们发送的内容以及特殊的终端行为(画线字符、颜色和其他形式的突出显示、是否可以在线的中间等)。通常,您不需要理解它,除非设置错误或者您喜欢高级终端黑客技术。 (如果您确实想理解它,请从terminfo(5)和其他
curses
/ncurses
/开始terminfo
文档。如果不出意外,您将学习如何制作真正精美的 shell 提示。)bash
uses the GNUreadline
library to do this. It's ultimately a wrapper over theterminfo
library (from System V or thencurses
library), whose purpose is to use terminal descriptions to figure out how to do those updates. If the terminal type is unknown or declared incorrectly, you'll notice that you either end up with garbage or all the pretty in-place updates go away.A terminal description looks something like
and describes how to do various screen updates, what function and action keys are available and what they send, and special terminal behaviors (line-drawing characters, colors and other forms of highlighting, whether it can insert characters in the middle of a line, etc.). You don't need to understand it, usually, unless something is set up wrong or you're into advanced terminal hackery. (If you do want to understand it, start with
terminfo(5)
and the othercurses
/ncurses
/terminfo
documentation. If nothing else, you'll learn how to make really fancy shell prompts.)