From b34fb14957f877173c21793425a79890574a8639 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Thu, 5 Apr 2018 17:36:58 -0700 Subject: [PATCH] Use ncurses::newterm instead of initscr This leaves stdout untouched, so it can better be composed in other commands. --- Cargo.toml | 1 + src/backend/curses/n.rs | 11 +++++++---- src/lib.rs | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a947a51..69bc1be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ toml = "0.4" unicode-segmentation = "1.0" unicode-width = "0.1" xi-unicode = "0.1.0" +libc = "0.2" [dependencies.maplit] optional = true diff --git a/src/backend/curses/n.rs b/src/backend/curses/n.rs index 1ea1c66..3c5225c 100644 --- a/src/backend/curses/n.rs +++ b/src/backend/curses/n.rs @@ -4,8 +4,10 @@ use self::ncurses::mmask_t; use self::super::{find_closest, split_i32}; use backend; use event::{Event, Key, MouseButton, MouseEvent}; +use libc; use std::cell::{Cell, RefCell}; use std::collections::HashMap; +use std::ffi::CString; use std::io::{stdout, Write}; use theme::{Color, ColorPair, Effect}; use utf8; @@ -32,7 +34,10 @@ impl Backend { // Default delay is way too long. 25 is imperceptible yet works fine. ::std::env::set_var("ESCDELAY", "25"); - ncurses::initscr(); + let tty_path = CString::new("/dev/tty").unwrap(); + let mode = CString::new("r+").unwrap(); + let tty = unsafe { libc::fopen(tty_path.as_ptr(), mode.as_ptr()) }; + ncurses::newterm(None, tty, tty); ncurses::keypad(ncurses::stdscr(), true); // This disables mouse click detection, @@ -73,9 +78,7 @@ impl Backend { /// Save a new color pair. fn insert_color( - &self, - pairs: &mut HashMap, - pair: ColorPair, + &self, pairs: &mut HashMap, pair: ColorPair ) -> i16 { let n = 1 + pairs.len() as i16; let target = if ncurses::COLOR_PAIRS() > i32::from(n) { diff --git a/src/lib.rs b/src/lib.rs index 487c0a1..968b752 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,6 +73,7 @@ extern crate log; extern crate maplit; extern crate num; +extern crate libc; extern crate owning_ref; extern crate toml; extern crate unicode_segmentation;