Interactive command-line applications like less
, vim
, and the Python interpreter seem to run in a different context.
When you exit, you return to the shell exactly as it was when you launched them, with no trace of the output of the application as you scroll the terminal display upward.
My question is: How is this accomplished? How would I write an application like that? One that "takes over" the shell, and then "gives it back" afterwards?
It's a fundamental concept I'm missing here. I'm aware of standard input, standard output, and standard error streams. How to write to them from a program, and how to use the shell to redirect them to files. But I don't understand how a text-mode interface can "capture" and "repaint" the terminal temporarily, and then disappear leaving no trace.