0

I'm using Rust for my ncurses app.

When the user presses F5, line numbers window should be toggled. When the user presses the F5 key for the first time, the window appears as it is supposed to. However, on second key press, the window does not go away, it's still there, as if the delwin call does not succeed. I tried refreshing the screen after it, but have had no success.

Here's a minimal example:

use ncurses::*;

struct LineNumbers {
    window: WINDOW, 
    shown: bool
}

impl LineNumbers {
    fn new() -> LineNumbers {
        LineNumbers {
            window: newwin(LINES(), 5, 0, 0),
            shown: false                
        }
    }

    fn toggle(&mut self) {
        if self.shown == true {
            self.hide();
        } else {
            self.show();
        }
    }

    fn show(&mut self) {
        self.shown = true;
        wbkgd(self.window, COLOR_PAIR(1));
        wrefresh(self.window);
    }

    fn hide(&mut self) {
        self.shown = false;
        delwin(self.window);
        refresh();
    }
}


fn main() {
    setlocale(LcCategory::all, "");
    initscr();
    keypad(stdscr(), true);
    start_color();
    init_pair(1, COLOR_RED, COLOR_RED);
    let mut ln = LineNumbers::new();
    loop {
        let user_input = get_wch();
        match user_input.unwrap() {
            WchResult::Char(ch) => {
                match ch {
                    27 => break,
                    _ => {}
                }
            },
            WchResult::KeyCode(code) => {
                match code {
                    KEY_F5 => {
                        ln.toggle();
                    },
                    _ => {}
                }
            }
        }
    }
    endwin();
}

What could be the issue?

adder
  • 3,512
  • 1
  • 16
  • 28
  • 1
    I don't think that's how windows work. If you want whatever you've drawn to a window to go away you have to draw over it -- deleting the window only destroys the data structure you used to draw it, not the content itself. – trent Apr 27 '20 at 12:07
  • I'm not sure how can I achieve the effect I want. So, on first key press, the window should be created and what you type should be offset by 5 to the right. Then on second key press, the window should be deleted and what you type should appear right from the beginning (cols = 0). – adder Apr 27 '20 at 12:32

1 Answers1

0

You could touch the main window before doing a refresh.

Deleting a window won't do that for you (man delwin):

Calling delwin deletes the named window, freeing all memory associated with it (it does not actually erase the window's screen image). Sub- windows must be deleted before the main window can be deleted.

It seems that ncurses-rs has no documentation, but is a "thin layer" (a binding). Use the ncurses manpages.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105