Home > Enterprise >  C/Ncurses how to wrap text in a textfield
C/Ncurses how to wrap text in a textfield

Time:08-03

I would like to wrap a short text in a Text Box in Ncurses, but somehow my text keeps going off screen. How can I wrap the text so it (automatically) goes to a new line when reaching the end of the screen on the right?

I tried playing with '\n' and setting limits but witout results. Any tips what I am doing wrong? See below code for what is going on.

Thanks from a beginner programmer.

#include <ncurses.h>
#include <string.h>

void text(WINDOW* textborder, int wymax, int wxmax, char text5[], int size)
{
        
    for (int i=0;i<size;i  )
    {
        mvwaddch(textborder,2,i i, text5[i]);
        if (i==wxmax)
        {
            mvwaddch(textborder,2,i i, '\n');
        }
    }
        
}


int main()
{
    
    char text5[]={"Somebody is watching over us... controlling us. It's true, I tell you. It's true! We are merely sprites that dance at the beck and call of our button-pressing overlord. This is a video game. Don't you see? We are characters in a video game."};
    int size;
    size=strlen(text5);
    
    int wymax; int wxmax;
    
    initscr();
    
    WINDOW* textborder=newwin(LINES/4, COLS, LINES-LINES/4, 0);
    box(textborder,-1,-1);
    getmaxyx(textborder, wymax,wxmax);
    wxmax=wxmax-4;
    
    text(textborder, wymax, wxmax, text5, size);
    
    wgetch(textborder);
    endwin();
    return 0;
}

CodePudding user response:

In theory the text should wrap itself. I think your issue may be coming from using mvwaddch, ch generally causes the text not to wrap. This may help Ncurses no-wrap mode when adding strings to window. Sorry I can't be more helpful :)

  • Related