6

I'm adding strings to a window, with waddwstr() function, one line after other, in consecutive rows. I don't want ncurses to automatically wrap lines for me – I'm overwriting them with consecutive calls to waddwstr() and sometimes tail of previous line is left displaying. Can ncurses just stop when right edge of window is reached?

Jerry Epas
  • 245
  • 3
  • 9

2 Answers2

3

The non-wrapping functions have "ch" in their name, e.g., wadd_wchstr.

The same is true of the non-wide interfaces waddstr versus waddchstr.

However, the wrapping/non-wrapping functions differ by more than that. They use different parameter types. The wrapping functions rely upon the video attributes set via wattr_set, etc., while the non-wrapping functions combine the video-attributes with the character data:

Converting between the two forms can be a nuisance, because X/Open, etc., did not define functions for doing the conversion.

The manual page for bkgd describes how these video attributes are combined with the background character to obtain the actual display.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
0

The accepted answer (by Mr. Dickey) is correct. However, the "ch" functions do not work with ordinary C strings (array of bytes). Another solution is to create a wrapper for waddstr which checks the current cursor position and window size and prints only as much as would fit.

For example:

int waddstr_trunc(WINDOW *win, const char *str)
{
  int cur_x, max_x, dummy [[maybe_unused]];
  getyx(win, dummy, cur_x);
  getmaxyx(win, dummy, max_x);
  int w=max_x - cur_x;
  if (w <= 0) return 0;
  char *str2 = strndup(str, w);
  if (str2 == NULL) return 1;
  int rv = waddstr(win, str2);
  free(str2);
  return rv;
}
hackerb9
  • 1,545
  • 13
  • 14