views:

1143

answers:

7

hey guys i seen console apps print colors and seen apps such as ffmpeg print text over itself instead of a new line. How do i print over an existing line? i want to display fps in my console app either at the very top or very bottom and have regular printfs go there and scroll normally

-edit- I need this for windows but this is meant to be cross platform so i'll eventually have a linux and mac implementation

+2  A: 

The ASCII character number 8 (A.K.A. Ctrl-H, BS or Backspace) lets you back up one character. ASCII Character number 13 (A.K.A Ctrl-M, CR or Carriage Return) returns the cursor at the beggining of the line.

If you are working in C try putchar(8); and putchar(13);

kmkaplan
+2  A: 

Check out ncurses. It has bindings for most scripting languages.

PEZ
+9  A: 

There is two simple possibilities which work on linux as well as windows, but only for one line:

  • printf("\b"); will return for one character, so you might count how many character you want to backspace and fire this in a loop, or you know that you only write n numbers and do it like
    printf("\b\b\b\b\b\b\b\b\b\b");
  • printf("text to be overwritten by next printf\r"); this will return the cursor to the beginning of the line, so any next printf will overwrite it. Make sure to write a string of same length or longer so you overwrite it entirely.

If you want to rewrite several lines, there is nothing so portable as ncurses, there is libs for it on practically every operating system, and you don't have to take care of the ANSI-differences.

edit: added link to ncurses wikipedia page, gives great overview and introduction, as well as link list and maybe a translation to your preferred language

lImbus
impressive to see that upvotes keep getting in after half a year :)keep it coming :)
lImbus
+1 for Ncurses, but almost -1 again for the `\b` suggestion. Ugly solution, strongly suggest a cross-platform curses implementation instead.
elliottcable
+1  A: 

You can use Ncurses -

ncurses package is a subroutine library for terminal-independent screen-painting and input-event handling which presents a high level screen model to the programmer, hiding differences between terminal types and doing automatic optimization of output to change one screenfull of text into another

gimel
+1  A: 

you can use \r instead of \n

CiNN
+1  A: 

Depending on the platform which you are developing on there's probably a more powerful API which you could use, rather than old ASCII control codes.

e.g. If you are working on Win32 you can actually manipulate the console screen buffer directly.

A good place to start might be here http://msdn.microsoft.com/en-us/library/ms683171(VS.85).aspx

I have been looking for similar functions/API which would allow me to access the console as something other than a stream of text for other platforms. Haven't found anything yet, but then again, I haven't been looking that hard.

Hope it helps.

John Leidegren
+2  A: 

The magic of the colors, cursor locating and bliking and so on are inside ANSI escape codes. Any text console capable of handling ANSI codes can use them just printing them out to console (i.e. by means of echo in a bash script or printf() function in C).

Unix terminals support ANSI escape sequences and Windows world used to support them back in old MS-DOS days, but the multibyte console support put an end to this. There is more information here. However there are other ways out of just ANSI sequences printing available on Windows. Moreover if you have Cygwin installed on your Windows maching ANSI codes work just as great as on any Unix terminal.

Many people mention Ncurses library that is the de-facto standard for any gui-like text based applications. What this library does is to hide all the terminal differences (Windows/Unix flavours) to represent the same information as identical as possible across all the platforms, though from my own experience I tell you this is not always true (i.e. typical text window frames change because the especial chars are not available under all character encodings). The counterpart of using ncurses is that it is a complete API and it is much harder to start out with it than simply writing out some ANSI escape sequences for simple things such as change the font color, cleaning screen or moving back the cursor to a random position.

For the sake of completeness I paste an example of use of ANSI sequence under Linux that changes the prompt to blue and shows the date:

PS1="\[\033[34m\][\$(date +%H%M)][\u@\h:\w]$ "
Fernando Miguélez