Do not flush stdout in ncurses backend

This commit is contained in:
Alexandre Bury 2018-04-09 10:06:11 -07:00
parent 2729e77838
commit d5178e778b

View File

@ -10,7 +10,7 @@ use std::collections::HashMap;
use std::ffi::CString; use std::ffi::CString;
use std::fs::File; use std::fs::File;
use std::io; use std::io;
use std::io::{stdout, Write}; use std::io::{Write};
use theme::{Color, ColorPair, Effect}; use theme::{Color, ColorPair, Effect};
use utf8; use utf8;
use vec::Vec2; use vec::Vec2;
@ -25,10 +25,15 @@ pub struct Backend {
event_queue: Vec<Event>, event_queue: Vec<Event>,
} }
/// Writes some bytes directly to `/dev/tty`
///
/// Since this is not going to be used often, we can afford to re-open the
/// file every time.
fn write_to_tty(bytes: &[u8]) -> io::Result<()> { fn write_to_tty(bytes: &[u8]) -> io::Result<()> {
let mut tty_output = let mut tty_output =
File::create("/dev/tty").expect("cursive can only run with a tty"); File::create("/dev/tty").expect("cursive can only run with a tty");
tty_output.write_all(bytes)?; tty_output.write_all(bytes)?;
// tty_output will be flushed automatically at the end of the function.
Ok(()) Ok(())
} }
@ -70,7 +75,6 @@ impl Backend {
// (Mouse move when a button is pressed). // (Mouse move when a button is pressed).
// Replacing 1002 with 1003 would give us ANY mouse move. // Replacing 1002 with 1003 would give us ANY mouse move.
write_to_tty(b"\x1B[?1002h").unwrap(); write_to_tty(b"\x1B[?1002h").unwrap();
stdout().flush().expect("could not flush stdout");
let c = Backend { let c = Backend {
current_style: Cell::new(ColorPair::from_256colors(0, 0)), current_style: Cell::new(ColorPair::from_256colors(0, 0)),
@ -226,7 +230,6 @@ impl backend::Backend for Backend {
fn finish(&mut self) { fn finish(&mut self) {
write_to_tty(b"\x1B[?1002l").unwrap(); write_to_tty(b"\x1B[?1002l").unwrap();
stdout().flush().expect("could not flush stdout");
ncurses::endwin(); ncurses::endwin();
} }