MrUser
MrUser

Reputation: 1237

Linux command line application with persistent interface

How do programs (specifically in Linux) like top keep their interface on the screen without it scrolling? Do they create a new screen session, or is there a standard library for creating GUIs like that? If they do use screen, then why is the output still written to stdout when I exit top?

Upvotes: 0

Views: 231

Answers (1)

Phillip
Phillip

Reputation: 13668

There is a standard library, it is called ncurses. But there's no need to use it - the console_codes(4) man page describes how to do such things by writing special character sequences to a terminal (emulator), and curses is nothing but a wrapper around that.

To clear the screen like top does, you'll need to echo ESC [ 2 J to the terminal to clear it, followed by ESC [ H to reset the cursor to the origin, i.e.

echo -ne "\033[2J\033[H"

Note that top might actually be more intelligent and not redraw the entire screen but only the parts which changed. See the manual page for more details. (You can do more fancy stuff with console codes, like changing colors, underline, bold, or even use the mouse in x terminals.)

Upvotes: 1

Related Questions