您如何在终端中编辑现有文本(并四处移动光标)? [英] How do you edit existing text (and move the cursor around) in the terminal?
问题描述
我曾经看过这个演示,它打印出一段文本(就像你在输入 some-command --help
时得到的那样),然后它又跳回了几个关键字text 并更改文本颜色,在终端中打印出来之后.
I saw this demo once that printed out a paragraph of text (like you'd get when typing some-command --help
), and it then jumped back up to a couple keywords in the text and changed the text color, after it was already printed out in the terminal.
这对我来说似乎很疯狂.他们是怎么做到的?
That seems crazy to me. How did they do that?
开始思考,我猜 stdout
和 stdin
在技术上是一个IO 流",所以也许这是一个持续性的变量,用于跟踪一个光标?我记得在构建语言解析器时做过类似的事情.
Starting to think about it, I guess stdout
and stdin
are technically an "IO stream", so maybe that's a persistent variable that keeps track of the position of a cursor? I remember doing something like that when building a language parser.
目标是这样的:假设您在控制台中键入以下内容,它会输出一个空白数组,因为在 Node.js 中,它都是异步的,我们不想每次都在控制台中编写异步函数:
The goal would be this: say you type the following into the console, and it outputs a blank array because in Node.js, it's all async and we don't want to write the async function everytime in the console:
$ node app.js
> App.User.all()
=> []
然后当异步回调执行时,你回去编辑=>;[]
包含结果:
Then when the async callback executes, you go back and edit the => []
to include the result:
$ node app.js
> App.User.all()
=> [#<User id:1>, #<User id:2>...]
至少知道如何实现会很棒,即使还有很多其他问题需要解决(与这个问题无关.我知道你可以定义一个全局回调并执行类似 App.User.all(_c)
).
That would be awesome to at least know how to implement, even if there are a lot of other issues to work through (unrelated to this question. And I know you can define a global callback and do something like App.User.all(_c)
).
终端输出已打印后如何编辑?
How do you edit the terminal output after it's already been printed?
推荐答案
终于找到了demo":
Finally found that "demo":
- https://github.com/asyncly/cdir/blob/223fe0039fade4fad2bdcfc8c9bdc8c8c8c89#L24
- http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html一个>
定位光标: