Search code examples
ncurses

How to Prevent Cursor From Reverting to Far Left of Screen after Newlines in Ncurses?


Using the function mvprintw(rowOffset, colOffset, textToPrint) from Ncurses, when I print a newline character, if colOffset is anything other than 0, I get a rectangle that looks like this:

   xxxxxxxx
x      x
xxxxxxxx

when I want the rest of the rectangle to align with the top

for

#include <ncurses.h>

    void main()
    {
        initscr();
        mvprintw(7,3,"xxxxxxxx\nx      x\nxxxxxxxx\n");
        refresh();
    }

How can I fix this?


Solution

  • You can create a window, which starts at 7,3, and writes to the window will wrap on newline to column-offset 3, e.g.,

    #include <ncurses.h>
    
    void main()
    {
        WINDOW *w;
        initscr();
        w = newwin(10,20, 7, 3);
        wprintw(w, "xxxxxxxx\nx      x\nxxxxxxxx\n");
        wrefresh(w);
        wgetch(w);
    }
    

    This creates a 10-line window. You could create a larger one, by taking into account the actual screen-size (e.g., LINES).