0

I'm using Rust to write an ncurses app.

I'm trying to set the color of a subwin, however having no success. I'm not even sure the window is created in the first place, or it just doesn't want to set the color.

Here's a minimal example:

use ncurses::*;

fn main() {
    setlocale(LcCategory::all, "");
    initscr();
    keypad(stdscr(), true);
    start_color();
    init_pair(1, COLOR_RED, COLOR_RED);
    loop {
        let user_input = get_wch();
        match user_input.unwrap() {
            WchResult::Char(ch) => {
                match ch {
                    27 => break,
                    _ => {}
                }
            },
            WchResult::KeyCode(code) => {
                match code {
                    KEY_F5 => {
                        let ln = subwin(stdscr(), LINES(), 5, 0, 0);
                        wbkgd(ln, COLOR_PAIR(1));
                        refresh();
                    },
                    _ => {}
                }
            }
        }
    }
    endwin();
}

As you can see, I initialized a color pair and invoked start_colors().

What could be the issue?

adder
  • 3,512
  • 1
  • 16
  • 28

2 Answers2

0

In this chunk

                    let ln = subwin(stdscr(), LINES(), 5, 0, 0);
                    wbkgd(ln, COLOR_PAIR(1));
                    refresh();

the refresh overwrites the result from the subwin. Also, you would get better results by ORing the COLOR_PAIR with a space (see this).

Addressing the comments:

let user_input = get_wch();

also does a refresh (overwriting the result from the subwin).

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • I tried removing refresh() call, however nothing happens still. I also tried to OR a space with the color pair, it says it's not implemented. – adder Apr 27 '20 at 09:00
  • Removing the `refresh` doesn't fix the issue. Moreover, moving this chunk before the loop works, even though the `refresh` is present. – Jmb Apr 27 '20 at 09:01
  • I need this in the loop. – adder Apr 27 '20 at 09:06
  • @dedmauz69 I suspected you need this in the loop, but the fact that it works outside the loop shows that `refresh` can't be the problem. – Jmb Apr 27 '20 at 09:15
  • However the `refresh` doesn't seem to affect the result one way or the other: it works outside the loop with or without `refresh` and it doesn't work inside the loop with or without `refresh`. – Jmb Apr 27 '20 at 09:20
  • Yeah, the problem lies elsewhere. I first tried it without refresh, then added it after it didn't work out. It still wouldn't work. I'm not sure what's the issue. – adder Apr 27 '20 at 09:22
0

I think the problem might be that your not refreshing the sub-window. Try using wrefresh(ln) instead. Actually use both refresh and refresh(ln).

dylan
  • 289
  • 2
  • 11