You are not logged in.

#1 2013-01-10 14:29:40

debdj
Member
Registered: 2012-01-19
Posts: 163

[C] Print and update three separate lines in console

Hello. I actually want to update three different lines in a virtual console simultaneously, i.e. in a loop they will be constantly updated and they are three separate lines. Now of course the line are separated by \n s but I don't want to keep printing new lines all the way throughout the loop. Just the three lines should be updated as the loop proceeds. Hope you get me.

How can it be done? \r would reset the cursor to the leftmost but that wont deliver three different lines..

Are there functions to manipulate the console to delete/replace lines?

I'd like to stick to libc if possible. I think curses would be a overkill for this simple need. But, well, if possible.

Last edited by debdj (2013-01-10 14:30:00)

Offline

#2 2013-01-10 14:34:16

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,497
Website

Re: [C] Print and update three separate lines in console

(n)curses is not an overkill, it is the tool specifically for this job.

You could embed escape codes for cursor movement.  There are escape codes to move the cursor up X number of lines, and clear lines.  But these are shell/terminal dependent.  You'd have to write different versions for different terminals.  Once you had all those versions, and the code to test which kind of terminal the program was running in, you'd have essentially rewritten the curses library (but probably not as well as the original).

So, if this is something  that will only be used by you and only in one terminal, you can just look up the escape codes your your specific situation, but if you expect this to work reliably, you should use (n)curses.

The following would (mostly?) work for a program running under bash under most terminals, but it may fail completely in other contexts.

int i, loops = 10;
for (i = 0; i < loops; i++) {
    printf("This is line one, loop %d\nline two loop %d\nline three loop %d\n\033[3A",i,i,i);
    sleep(1);
}
puts("\n\n\n"); /* some clean up so next prompt shows up correctly */

Last edited by Trilby (2013-01-10 14:42:39)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#3 2013-01-10 16:31:16

debdj
Member
Registered: 2012-01-19
Posts: 163

Re: [C] Print and update three separate lines in console

Thanks a lot. Its working fine in my(computer's ;p) terminal. I've started reading the ncurses man and tldp docs on ncurses anyway.

Offline

#4 2013-01-13 02:32:51

Radioactiveman
Member
From: Germany
Registered: 2010-05-13
Posts: 376

Re: [C] Print and update three separate lines in console

If you think ncurses is overkill, you can also use this (with the disadvantages mentioned by Trilby):

#define POSITION(row, column) printf("\033[%d;%dH", row, column)

Offline

Board footer

Powered by FluxBB