Ruiqi Li
Ruiqi Li

Reputation: 197

Ncurses mvwprintw doesnt print

I have a simple program that has a main window and a small window on the bottom like (without the lines, thats just so you can see the two windows:

+------------------+
|                  |
|                  | 
|                  |
+------------------+
|                  |
+------------------+

I want the bottom area to be a place where you can type in, and here is my source code:

#include <termios.h>
#include <bits/stdc++.h>
#include <ncurses.h>
int main()
{
    int scrx;
    int scry;

    initscr();
    cbreak();
    noecho();
    clear();
    raw();

    getmaxyx(stdscr, scrx, scry);
    WINDOW* input = newwin(1, scrx, scry, 0);

    std::string cmdbuf;

    while(true)
    {
        int newx;
        int newy;
        getmaxyx(stdscr, newx, newy);

        if(newx != scrx || newy != scry)
        {
            // do stuff;
        }

        char c = wgetch(input);
        cmdbuf.push_back(c);
        werase(input);

        mvwprintw(input, 0, 0, cmdbuf.c_str());
        refresh();
        wrefresh(input);
    }
}

However, it doesn't seem to print anything, just move my cursor (which gets suck halfway across the screen). How can I make it so that text actually gets printed and that my cursor actually moves across the full screen?

Upvotes: 2

Views: 1228

Answers (3)

Gavin Downard
Gavin Downard

Reputation: 33

The declaration of newwin is:

WINDOW *newwin(
         int nlines, int ncols,
         int begin_y, int begin_x);

You are calling:

newwin(1,scry,scrx,0)

Which sets the size of you window to 1 tall and scry wide, and puts it at coordinates (0,srcx). What you want is:

newwin(1,scry,scrx-1,0)

Where 1 is the height of the window.

Also, cbreak overrides raw, so there is no point to calling both.

Upvotes: 1

user3091673
user3091673

Reputation:

Tidied it up for you a bit. Press 'q' to quit. You get the idea.

#include <termios.h>                                                                                                                                                                         
#include <bits/stdc++.h>
#include <ncurses.h>

int main()
{
  int scrx, scry;
  initscr();
  getmaxyx(stdscr, scry, scrx);
  WINDOW *w = newwin(1, scrx, scry - 1, 0);
  std::string cmdbuf {};
  char c = '\0';

  while (c != 'q')
  {
    int newx, newy;
    getmaxyx(stdscr, newx, newy);

    if(newx != scrx || newy != scry)
    {
      // do stuff;
    }

    c = wgetch(w);
    cmdbuf += c;
    mvwprintw(w, 0, 0, "%s", cmdbuf.c_str());
    wrefresh(w);
  }

  delwin(w);
  endwin();
}

Upvotes: 1

Thomas Dickey
Thomas Dickey

Reputation: 54583

The refresh is overwriting the mvwprintw because they're different windows. For the given example, there's no reason to refresh stdscr because nothing (except for the initscr call) has updated that window. Moving refresh out of the loop would help (but the "do stuff" can obviously interfere with that).

The newx/newy logic is too fragmentary to comment on (I'd use getbegyx ...).

Upvotes: 1

Related Questions