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.
我看过这个演示,打印出一段文字(就像你在输入some-command --help时得到的那样),然后它跳回到文本中的几个关键字并改变文本颜色,之后已经在终端打印出来了。
That seems crazy to me. How did they do that?
这对我来说似乎很疯狂。他们是怎么做到的?
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.
开始考虑它,我猜stdout和stdin在技术上是一个“IO流”,所以也许这是一个跟踪光标位置的持久变量?我记得在构建语言解析器时做了类似的事情。
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.js中,它都是异步的,我们不想每次在控制台中编写异步函数:
$ 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>...]
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)
).
即使有很多其他问题需要解决,这至少知道如何实现也是很棒的(与这个问题无关。我知道你可以定义一个全局回调并执行类似App.User.all(_c)的操作))。
How do you edit the terminal output after it's already been printed?
如何在打印后编辑终端输出?
2 个解决方案
#1
40
Finally found that "demo":
终于找到了“demo”:
- https://github.com/asyncly/cdir/blob/223fe0039fade4fad2bb08c2f7affac3bdcf2f89/cdir.js#L24
- https://github.com/asyncly/cdir/blob/223fe0039fade4fad2bb08c2f7affac3bdcf2f89/cdir.js#L24
- http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html
- http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html
-
http://ascii-table.com/ansi-escape-sequences-vt-100.php
http://ascii-table.com/ansi-escape-sequences-vt-100.php
-
Position the Cursor:
\033[<L>;<C>H
or\033[<L>;<C>f
(puts the cursor at line L and column C)定位光标:\ 033 [
; H或\ 033 [ ; f(将光标置于L行和C列) - Move the cursor up N lines:
\033[<N>A
-
将光标向上移动N行:\ 033 [
A - Move the cursor down N lines:
\033[<N>B
-
将光标向下移动N行:\ 033 [
B - Move the cursor forward N columns:
\033[<N>C
-
将光标向前移动N列:\ 033 [
C - Move the cursor backward N columns:
\033[<N>D
-
向后移动光标N列:\ 033 [
D. - Clear the screen, move to (0,0):
\033[2J
- 清除屏幕,移至(0,0):\ 033 [2J
- Erase to end of line:
\033[K
- 擦除到行尾:\ 033 [K.
- Save cursor position:
\033[s
- 保存光标位置:\ 033 [s
- Restore cursor position:
\033[u
- 恢复光标位置:\ 033 [u
#2
0
If you are working in Mac OX X, I believe the program Eddie does something like "edit terminal output after it's already been printed". (see "Eddie (text editor) at Wikipedia" ).
如果您使用的是Mac OX X,我相信Eddie程序会执行“在已经打印后编辑终端输出”之类的操作。 (参见“*的Eddie(文本编辑器)”)。
Under the file menu, choose "New" and then under "Settings/Document Settings..." choose "Shell Window". After that try a couple of unix commands: date, ls, cal, etc. Then put your cursor and edit these.
在文件菜单下,选择“新建”,然后在“设置/文档设置...”下选择“外壳窗口”。之后尝试几个unix命令:date,ls,cal等。然后放上光标并编辑它们。
I would have tried with Node but I could not install it (Node) under Mac OS X 10.5.8.
我会尝试使用Node但我无法在Mac OS X 10.5.8下安装它(Node)。
#1
40
Finally found that "demo":
终于找到了“demo”:
- https://github.com/asyncly/cdir/blob/223fe0039fade4fad2bb08c2f7affac3bdcf2f89/cdir.js#L24
- https://github.com/asyncly/cdir/blob/223fe0039fade4fad2bb08c2f7affac3bdcf2f89/cdir.js#L24
- http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html
- http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html
-
http://ascii-table.com/ansi-escape-sequences-vt-100.php
http://ascii-table.com/ansi-escape-sequences-vt-100.php
-
Position the Cursor:
\033[<L>;<C>H
or\033[<L>;<C>f
(puts the cursor at line L and column C)定位光标:\ 033 [
; H或\ 033 [ ; f(将光标置于L行和C列) - Move the cursor up N lines:
\033[<N>A
-
将光标向上移动N行:\ 033 [
A - Move the cursor down N lines:
\033[<N>B
-
将光标向下移动N行:\ 033 [
B - Move the cursor forward N columns:
\033[<N>C
-
将光标向前移动N列:\ 033 [
C - Move the cursor backward N columns:
\033[<N>D
-
向后移动光标N列:\ 033 [
D. - Clear the screen, move to (0,0):
\033[2J
- 清除屏幕,移至(0,0):\ 033 [2J
- Erase to end of line:
\033[K
- 擦除到行尾:\ 033 [K.
- Save cursor position:
\033[s
- 保存光标位置:\ 033 [s
- Restore cursor position:
\033[u
- 恢复光标位置:\ 033 [u
#2
0
If you are working in Mac OX X, I believe the program Eddie does something like "edit terminal output after it's already been printed". (see "Eddie (text editor) at Wikipedia" ).
如果您使用的是Mac OX X,我相信Eddie程序会执行“在已经打印后编辑终端输出”之类的操作。 (参见“*的Eddie(文本编辑器)”)。
Under the file menu, choose "New" and then under "Settings/Document Settings..." choose "Shell Window". After that try a couple of unix commands: date, ls, cal, etc. Then put your cursor and edit these.
在文件菜单下,选择“新建”,然后在“设置/文档设置...”下选择“外壳窗口”。之后尝试几个unix命令:date,ls,cal等。然后放上光标并编辑它们。
I would have tried with Node but I could not install it (Node) under Mac OS X 10.5.8.
我会尝试使用Node但我无法在Mac OS X 10.5.8下安装它(Node)。