jeaye / ncurses-rs

A low-level ncurses wrapper for Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Strange -- Can't read redirected output when using ncurses!

skylaner opened this issue · comments

Here is a little program I've written:

extern crate ncurses;

use std::io;
use std::io::prelude::*;
use std::process;

fn main() {
    let mut contents = String::new();
    match read_contents(&mut contents) {
        Ok(0) => {
            eprint!("No input!\n");
            process::exit(0);
        },

        Ok(result) => result,

        Err(e) => {
            eprint!("Can't read input: {}\n", e);
            process::exit(1);
        }
    };

    run(&contents);
}

fn read_contents(buf: &mut String) -> io::Result<usize> {
    let mut stdin = io::stdin();

    stdin.read_to_string(buf)
}

fn run(contents: &String) {
    ncurses::initscr();

    ncurses::printw(contents);
    ncurses::refresh();
    ncurses::getch();

    ncurses::endwin();
}

This works fine with keyboard input but when I redirect input from a file or pipe from another program, nothing is output!

If I remove printw and use println! everything works fine.

In short, these won't work:

$ pwd | ./main
$ ./main < main.rs

Thanks for reporting this. I can't help much, unfortunately, since I haven't used Rust or ncurses in a couple of years. In general, I recommend writing the ncurses code in C to verify everything works there. Then port it to Rust (which is mostly 1:1, since ncurses-rs is so light) and it should remain the same. In cases where behavior changes, take a look at the ncurses wrapping of the C fn to see if there's something wrong. As mentioned, each fn wrapper is just a couple of lines, so it shouldn't be much to take in.

We'll certainly leave this open though, until it's resolved.