Resizing a terminal and scrolling with ncurses

I am programming in C using the ncurses libraries (this is the first time), and I have two problems. I am on ubuntu with the default terminal (gnome terminal).

1) I need to change the size of the terminal. I used resizeter () and resize_term (), but they fail.

2) I use the scrollok () function, and the problem is that I lose the scroll lines (when I return using wscrl (), there are empty lines).

#include <ncurses.h> int main() { WINDOW *win, *win2; int i; char c; initscr(); cbreak(); noecho(); win=newwin(8,20,1,1); box(win,0,0); win2=newwin(6,18,2,2); scrollok(win2,1); wrefresh(win); wrefresh(win); for(i=0;i<15;i++){ c=wgetch(win2); if(c=='u'){ wscrl(win2,-1); wrefresh(win2); } else{ wprintw(win2,"%c\n",c); wrefresh(win2); } } delwin(win); delwin(win2); endwin(); return 0; } 
+4
source share
3 answers
  • You cannot resize the terminal window from ncurses. The functions you are talking about resize the terminal window, which is drawn with curses. The idea is that you catch the SIGWINCH signal and call resizeterm in the handler when the user resizeterm window outside the application (with the mouse, possibly).

  • This is the intended behavior, although poorly documented in ncurses and in the Unix / POSIX standard. NetBSD curses docs will explicitly indicate it:

    If n is positive, then stdscr scrolls up. n lines are lost above stdscr and n empty lines are inserted at the bottom. If n is negative, then stdscr scrolls down. n blank lines are inserted at the top of stdscr and n lines are lost at the bottom.

    Thus, you will have to manually save the input and retype it while scrolling.

+6
source

You cannot resize the terminal window from ncurses, but you can resize the terminal that resize the system call.

 #include <ncurses.h> #include <stdlib.h> int main(int argc, char *argv[]){ WINDOW *ventana1; system("resize -s 30 80"); initscr(); start_color(); ventana1 = newwin(15, 50, 0, 0); init_pair(1,COLOR_YELLOW,COLOR_BLUE); init_pair(2,COLOR_BLUE, COLOR_YELLOW); wbkgd(ventana1,COLOR_PAIR(1)); wprintw(ventana1, "POLLO"); wrefresh(ventana1); wgetch(ventana1); wgetch(ventana1); system("resize -s 20 60"); wbkgd(ventana1,COLOR_PAIR(2)); wprintw(ventana1, "POLLO"); wrefresh(ventana1); wgetch(ventana1); wgetch(ventana1); system("resize -s 35 85"); system("clear"); wbkgd(ventana1,COLOR_PAIR(1)); wprintw(ventana1, "POLLO"); wrefresh(ventana1); wgetch(ventana1); wgetch(ventana1); delwin(ventana1); endwin(); system("resize -s 25 75"); } 
+1
source

POSIX does not cover this case because the curses document is not part of POSIX. The open group supports documentation for both:

As indicated on the resizeterm page, you should not call this function from a signal handler because it calls "unsafe" functions. The topic of "unsafe" functions is discussed in several places; that in gcc the documentation is done to start.

As for the documentation, @larsmans seems to be quoted from scroll(3) but does not refer to comparable links for ncurses and "POSIX". What is it worth:

  • ncurses (this seems to be about points that are supposed to be unique to NetBSD)
  • X / Open (necessarily more general, as it is designed to cover various implementations).

Back to the OP question:

  • The sample program does not show the use of OP resizeterm and resize_term . This is not indicated, but presumably the OP resized the terminal window and the program did not respond. The resizeterm page for resizeterm is clear enough so that ncurses does not cause terminal resizing. To do this (on some terminals), you can use the -s resize option (utility for xterm ). If successful, resize the terminal, which in turn will send SIGWINCH . For this, ncurses has a predefined signal handler, but at the application level it is recommended to use KEY_RESIZE . There are several programs in ncurses-examples that do this.
  • moving lines up in a window necessarily moves some of the window. This means that the lines are shifted to the window to replace those that remain. A “window” is simple: a data view of a limited size. For views of different sizes, the developer is recommended to use "gaskets" (see the man page ). The notes on the scroll guide page mention some problems with the color of spaces (line replacements). It is about whether to leave replacements empty or fill them with application data. Curses does not do this automatically (not even for shims).
+1
source

Source: https://habr.com/ru/post/1336181/


All Articles