tags:

views:

53

answers:

1

I'm trying to redraw the content of a simple loop. So far it prints out to the stdscr 100 lines, and then by using scrl to scroll n lines I get n blank rows.

What I want is to keep with the sequence. However, I'm not sure how to redraw the stdscr with the n extra lines. Any ideas will be appreciate it!

#include <ncurses.h>
int main(void)
{
    initscr();
    int maxy,maxx,y;
    getmaxyx(stdscr,maxy,maxx);
    scrollok(stdscr,TRUE);
    for(y=0;y<=100;y++)
        mvprintw(y,0,"This is some text written to line %d.",y);
    refresh();
    getch();
    scrl(5);
    refresh();
    getch();
    endwin();
    return(0);
}
A: 

Presumably, after the refresh() after scrl(), add the lines:

for (y = 96; y <= 100; y++)
    mvprintw(y, 0, "This is some text written as line %d.", y+5);
refresh();

I'm using magic numbers, of course. You should use something like:

enum { MAX_SCR_LINE = 100 };
enum { MAX_SCROLL   =   5 };

Then the loop is:

for (y = MAX_SCR_LINE - MAX_SCROLL + 1; y <= MAX_SCR_LINE; y++)
    mvprintw(y, 0, "This is some text written as line %d.", y + MAX_SCROLL);
refresh();

And if you need to repeat the process over, and over, then the calculation becomes:

enum { MAX_ITER = 10 };
for (int j = 0; j < MAX_ITER; j++)
{
    scrl(MAX_SCROLL);
    for (y = MAX_SCR_LINE - MAX_SCROLL + 1; y <= MAX_SCR_LINE; y++)
        mvprintw(y, 0, "This is the text written as line %d.",
                 y + (j + 1) * MAX_SCROLL);
 }

Etcetera...

(Code untested - beware bugs.)

Jonathan Leffler