tags:

views:

164

answers:

6

I can't seem to find the right way to ask the almighty google...

In such programs as a command-line progress bar, the output buffer seems to be directly manipulated. It can't print a character to a terminal in any place it wants. How is such control over a program's output controlled in standard C? Is there a special library that I can look up?

+5  A: 

look at curses it`s a lib for unix/linux

f0b0s
+3  A: 

If you just want a progress bar, you can just print a single 'X' for every 2% completion. This should fill 50 characters on a line.

If you want something more fancy, on Linux you can try the classic "curses" library, or if you just want a dialog box, you could try the library that the Debian install utilities use, but I forget its name.

keraba
I was using the progress bar more as an example, since the bars in wget et al are very fancy. I'm looking to be able to basically print characters wherever I want.Consequently, consider Rogue-like games. Is a curses-style library the way that these games create their environments in a terminal?
yeah... i used it to write tetris.
f0b0s
+2  A: 

It's not part of standard C. These things work by writing some special character sequences that are recognized by the terminal emulator which takes care of the cursor positioning and stuff.

cube
+3  A: 

you can do the progress bar with \r check this

for doing more advanced stuff you can use ncurses

hiena
A: 

The big gorilla here is the ncurses library, but you can do a lot of cool stuff with less learning curve. Try using \r to move to beginning of line and using simple control sequences to clear to end of line, turn bold on and off, etcetera. The tput(1) command is invaluable. For example, I wrote a simple application the does highlighted text, and to turn highlighting on and off I just called the commands tput smso and tput rmso. You can capture the results using C with popen(3); using a shell it is even easier.

Norman Ramsey
A: 

You could use ANSI escape coding to control the termincal output. This is how a lot of MUD games do their output.

Simeon Pilgrim
Can it be assumed that these escape sequences will work on any *nix terminal? I'm just toying around with these things so I might be working on my school's server via ssh or I might be on my own Linux machine. In either case I want the result to be uniform...
Yip, standard SSH/terminal shells handle these commands. You can do a quick test by outputting "\e[1;31mHello" (gcc) should print a red Hello on you *nix term.
Simeon Pilgrim