34

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?

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.

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

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?

Lance
  • 75,200
  • 93
  • 289
  • 503

2 Answers2

67

Finally found that "demo":

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
Lance
  • 75,200
  • 93
  • 289
  • 503
  • 1
    It's also important to note that the last 2 codes (Save cursor position and restore cursor position) are not supported everywhere. As stated in the 2nd source provided by you: `The latter two codes are NOT honoured by many terminal emulators. The only ones that I'm aware of that do are xterm and nxterm - even though the majority of terminal emulators are based on xterm code.` – GGG Jul 21 '16 at 19:28
  • 13
    Can't be used in template strings or strict mode, but you can replace the octal `\033` with hex `\x1b` or unicode `\u001b`. – Jason Goemaat Aug 24 '16 at 09:36
0

If you are working in Mac OS X, I believe the program Eddie does something like "edit terminal output after it's already been printed". (see "Eddie (text editor) at Wikipedia" ).

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.

I would have tried with Node but I could not install it (Node) under Mac OS X 10.5.8.

Josh M.
  • 26,437
  • 24
  • 119
  • 200