5

Ok so I was just wondering how console applications like top(1) for linux overwrite multiple lines. I want to know how to do it for C++ or C. I know how to overwrite 1 line by just putting a \r but I wanted to know how to do so for multiple lines, again like in top or if its even possible.

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
csteifel
  • 2,854
  • 6
  • 35
  • 61

2 Answers2

3

They use terminal escape sequences to move the cursor around the screen. A few even use direct framebuffer access, to treat the screen as an array of characters which can be directly indexed.

The curses (or ncurses) library is a good abstraction layer.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Just a note, but curses doesn't really exist on Windows: [Is ncurses available for Windows](http://stackoverflow.com/q/138153/16487) – C. Ross Jan 18 '12 at 02:57
  • @C.Ross: That question asks about replacement terminal emulators that support the curses API. `curses` is certainly available for console mode applications. – Ben Voigt Jan 18 '12 at 03:00
  • pdcurses actually is very usable, I haven't had any problems with porting my ncurses applications using basic ASCII characters to pdcurses on Windows. if you're using ncursesw, the UTF-8 compatible incarnation of ncurses, you'll probably run into a lot more trouble. – Daniel Kamil Kozar Jan 18 '12 at 03:28
  • @Daniel: cygwin-installed ncurses apps such as vim work just fine, I don't see why a self-developed app should have any more problems. – Ben Voigt Jan 18 '12 at 04:01
2

This may not directly address the question, but this sort of thing is dependent on the terminal and is commonly done with a curses implementation (ncurses is the most widely used).

ezod
  • 7,261
  • 2
  • 24
  • 34