1

I use Xshell to connect to a remote host and write ncurse programs. But it was found that the characters could not be printed in a cycle to the right.

#include <ncurses.h>
int main() {
    initscr();
    for (int i = 0;i < 10; i++){
        addch('o');
    }
    refresh();
    getch();
    endwin();
    return 0;
}

Result: Only the first character is printed.

In order to print it all out, I had to add the refresh code

for (int i = 0; i < 10;i++) {
    addch('o');
    refresh();//print all 'o'
}

I think the BIG problem makes the box function unavailable, because the two Horizontal line of a box are not completed, it only have the first '-' too.

int main() {
    initscr();
    curs_set(0);
    WINDOW *win;
    win = newwin(10, 40, 9, 20);
    box(win, 0, 0);
    refresh();
    wrefresh(win);
    getch();
    endwin();
    return 0;
}

Result: Notice the two horizontal lines up and down

I can't figure out this problem.

2021.3.11
I have I have identified the problem. The problem is with Xshell. I connect my host by Alibaba Cloud Server official website, and the program have no problem. But I still don't konw how to set up my xshell.

shubulan
  • 7
  • 3
  • 1
    Please describe only one problem, ideally demonstrating it with a [mre]. Asking about two problems is confusing and makes the question unfocused. – Yunnosch Mar 10 '21 at 07:19
  • 1
    Please do not add pictures showing plain text. Instead just copy&paste the text into your question. Youu can use the `edit` button below your question to fix that. – Gerhardh Mar 10 '21 at 07:23
  • 1
    You claim that only the first character is printed. That is not true. Your code prints `o` (lower case letter o) while your picture contains a `0` (digit zero). Something does no match here. – Gerhardh Mar 10 '21 at 07:26
  • thank you for your feedback. It seems that there is a problem with my picture and it has been revised. – shubulan Mar 10 '21 at 08:52
  • 1
    That's two questions (close the first because the example doesn't match the picture, and the second is a duplicate because the refresh from `getch` interferes with the `wrefresh` -- and again, doesn't match the picture). – Thomas Dickey Mar 10 '21 at 08:53
  • 1) maybe try `(addch)('o');` to use the real function rather than a (possible) macro (which should work anyway, but ...). see reference: https://invisible-island.net/ncurses/man/curs_addch.3x.html – pmg Mar 10 '21 at 09:02
  • 1
    You replaced your picture by another picture. Please avoid using pictures where text is much more apropriate. – Gerhardh Mar 10 '21 at 12:18
  • Not reproducible. Check your TERM environment variable and make sure it corresponds to your terminal emulator. – n. m. could be an AI Mar 10 '21 at 15:43

1 Answers1

1

Using the following code (save to test.c and compile with gcc -o test test.c -lncurses) I can draw a border, put text inside it, and also put text in the main window.

#include <ncurses.h>

// Print the character 'ch' in the window
// 'win', and do this 'repeat' times.
int print_chars(WINDOW *win, char ch, int repeat) {
    if (win == NULL) {
    // Print to main window
        for (int i=0; i<repeat; i++) {
            addch(ch);  
        refresh();
        }
    } else {
    // Print to the named window
        for (int i=0; i<repeat; i++) {
            waddch(win, ch);    
        }
        wrefresh(win);
    }
    return 0;
}

int main(int argc, char **argv) 
{
    WINDOW *border_win, *win;
    
    initscr();
    refresh(); // This seems to be needed; 
               // I don't know why.
    
    // Create a window to show the border
    border_win = newwin(10,40,9,20);
    box(border_win,0,0);
    wrefresh(border_win);
    
    // Create a window inside that, which 
    // we will write in.  Otherwise it 
    // seems we will overwrite the border.
    win = newwin(8,38,10,21);
    print_chars(win, 'a', 10);

    // Write something in the main window
    // as well, just to show it works
    print_chars(NULL, 'o', 10);
    
    getch();
    endwin();
    return 0;
}
Tony
  • 1,645
  • 1
  • 10
  • 13