Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 371 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Resize terminal and scrolling problem with ncurses

#1
I'm programming in C using ncurses libraries (it's the first time) and I've two problems. I'm on ubuntu with the default terminal (gnome terminal).

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

2) I use scrollok() function and the problem is that I lose scrolled lines (when I get back with wscrl(), there are blank 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;
}
Reply

#2
1. You can't resize the terminal window from ncurses. The functions you mention resize the part of the terminal window that is painted on by curses. The idea is you catch the `SIGWINCH` signal and call `resizeterm` in the handler when the user resizes the window *from outside the application* (using the mouse, probably).

2. This is intended behavior, though poorly documented in ncurses and in the Unix standard/POSIX. [NetBSD's curses docs][1] state it explicitly:

> If *n* is positive then `stdscr`
> is scrolled up. *n* lines are lost
> from the top of `stdscr` and *n* blank
> lines are inserted at the bottom. If
> *n* is negative then `stdscr` is scrolled down. *n* blank lines are
> inserted at the top of `stdscr` and
> *n* lines are lost from the bottom.

So you'll have to manually save input and reprint it when scrolling.


[1]:

[To see links please register here]

Reply

#3
You can't resize the terminal window from ncurses but you can resize the terminal which the [resize][1] 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]:

[To see links please register here]

Reply

#4
POSIX does not cover this case, because the curses document is not part of POSIX. The Open Group happens to maintain documentation for both:

- [X/Open CURSES Issue 4 Version 2][1] (overview)
- [X/Open Curses, Issue 4 Version 2][2] (documentation)
- [The Open Group Base Specifications Issue 7
IEEE Std 1003.1, 2013 Edition][3] (aka "POSIX")
- [`signal.h`][4] (note that `SIGWINCH` is absent)

As noted in the manual page for [`resizeterm`][5], you should not call that function from within a signal handler, because it calls "unsafe" functions. The topic of "unsafe" functions is discussed in several places; that in gcc's [documentation][6] would do for a start.

Regarding documentation, @larsmans appears to be quoting from [`scroll(3)`][7], but not citing comparable links for ncurses and "POSIX". For what it's worth:

- [ncurses][8] (seems to address the points implied to be unique to NetBSD)
- [X/Open][9] (is necessarily more general, as it is intended to cover different implementations)

Back to OP's question:

1. the sample program does not show OP's use of `resizeterm` nor of `resize_term`. It is not stated, but presumably OP resized the terminal window and the program did not respond. The manual page for `resizeterm` is clear enough that ncurses does not cause the terminal to resize. For *that* (on some terminals), one can use the `-s` option of [`resize`][10] (a utility program for [`xterm`][11]). If successful, that resizes the terminal, which in turn sends a `SIGWINCH`.
ncurses has a predefined signal handler for that, but at the *application* level, handling `KEY_RESIZE` is the recommended way. There are several programs in [ncurses-examples][12] which do this.
2. moving the lines *up* in a window necessarily moves some *out* of the window. That implies that lines are shifted *into* the window to *replace* those which have left. A "window" is just that: a limited-size view of data. For views with different size, the developer is advised to use "pads" (see [manual page][13]). The notes in the scroll manual page mention some issues with the color of blanks (the replacement lines). It is up to the application whether to leave the replacements blank or fill them with the application's data. Curses does not do that automatically (not even for pads).


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

[5]:

[To see links please register here]

[6]:

[To see links please register here]

[7]:

[To see links please register here]

[8]:

[To see links please register here]

[9]:

[To see links please register here]

[10]:

[To see links please register here]

[11]:

[To see links please register here]

[12]:

[To see links please register here]

[13]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through