Search code examples
ncursescurses

Ncurses getnStr with existing base string


I have written a program that uses ncurses for the UI and the function mvwgetnstr to read in a string from a window. I want to allow users to make edits to the text they enter, so if they enter foo they can later go back and append bar. I cannot figure out how to do this with the curses API, the getnstr function only takes in a char buffer and length variable. Any ideas? I started making my own string input function, but it's difficult to keep everything constrained within the window.


Solution

  • It depends on what you want. There are many possible ways to organize a program:

    • getnstr reads the buffer from the standard screen (a window)
    • The wgetnstr function accepts a buffer from a given window. Using a separate window (or subwindow) reduces the problem of updates for the edited buffer interfering with other things on the screen.
    • getnstr editing is crude. If you use the form library (which in turn uses ncurses), that allows you to do more than just append/remove characters from the end of the input buffer.
    • write your own input-function, which can get complicated. The dialog program does this.

    The ncurses-examples might be helpful for reading and seeing how to build up a suitable input function.