diff --git a/Cargo.toml b/Cargo.toml index a739de5..5c5e986 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,10 +24,7 @@ unicode-width = "0.1" xi-unicode = "0.1.0" libc = "0.2" chan = "0.1" - -[dependencies.ioctl-rs] -optional = true -version = "0.2" +term_size = { version = "0.3.1", optional = true } [dependencies.num] default-features = false @@ -71,8 +68,8 @@ pretty-bytes = "0.2.2" blt-backend = ["bear-lib-terminal"] default = ["ncurses-backend"] markdown = ["pulldown-cmark"] -ncurses-backend = ["ncurses", "maplit", "ioctl-rs"] -pancurses-backend = ["pancurses", "maplit", "ioctl-rs"] +ncurses-backend = ["ncurses", "maplit", "term_size"] +pancurses-backend = ["pancurses", "maplit", "term_size"] termion-backend = ["termion"] [lib] diff --git a/src/backend/curses/mod.rs b/src/backend/curses/mod.rs index 92b980c..50fe809 100644 --- a/src/backend/curses/mod.rs +++ b/src/backend/curses/mod.rs @@ -1,20 +1,32 @@ //! Common module for the ncurses and pancurses backends. //! //! Requires either of `ncurses-backend` or `pancurses-backend`. -#![cfg(any(feature = "ncurses", feature = "pancurses"))] +#![cfg(any(feature = "ncurses-backend", feature = "pancurses-backend"))] + +extern crate term_size; + +use std::collections::HashMap; use event::{Event, Key}; -use std::collections::HashMap; use theme::{BaseColor, Color, ColorPair}; -mod sizes; +use vec::Vec2; -#[cfg(feature = "ncurses")] +#[cfg(feature = "ncurses-backend")] pub mod n; -#[cfg(feature = "pancurses")] +#[cfg(feature = "pancurses-backend")] pub mod pan; +/// Get the size of the terminal. +/// +/// Usually ncurses can do that by himself, but because we're playing with +/// threads, ncurses' signal handler is confused and he can't keep track of +/// the terminal size. Poor ncurses. +fn terminal_size() -> Vec2 { + term_size::dimensions().unwrap_or((0,0)).into() +} + fn split_i32(code: i32) -> Vec { (0..4).map(|i| ((code >> (8 * i)) & 0xFF) as u8).collect() } diff --git a/src/backend/curses/n.rs b/src/backend/curses/n.rs index f5556a2..473d774 100644 --- a/src/backend/curses/n.rs +++ b/src/backend/curses/n.rs @@ -264,8 +264,8 @@ impl Backend { /// /// We need to have ncurses update its representation of the screen. fn on_resize() { - // Get size using ioctl - let size = super::sizes::terminal_size(); + // Get size + let size = super::terminal_size(); // Send the size to ncurses ncurses::resize_term(size.y as i32, size.x as i32); diff --git a/src/backend/curses/pan.rs b/src/backend/curses/pan.rs index 7f6adfe..f67fb8d 100644 --- a/src/backend/curses/pan.rs +++ b/src/backend/curses/pan.rs @@ -357,8 +357,8 @@ impl Backend { /// /// We need to have ncurses update its representation of the screen. fn on_resize() { - // Get size using ioctl - let size = super::sizes::terminal_size(); + // Get size + let size = super::terminal_size(); // Send the size to ncurses pancurses::resize_term(size.y as i32, size.x as i32); diff --git a/src/backend/curses/sizes.rs b/src/backend/curses/sizes.rs deleted file mode 100644 index 6fba9b9..0000000 --- a/src/backend/curses/sizes.rs +++ /dev/null @@ -1,23 +0,0 @@ -extern crate ioctl_rs as ioctl; - -use libc::{c_ushort, STDOUT_FILENO}; -use std::mem; - -use vec::Vec2; - -#[repr(C)] -struct TermSize { - row: c_ushort, - col: c_ushort, - _x: c_ushort, - _y: c_ushort, -} - -/// Get the size of the terminal. -pub fn terminal_size() -> Vec2 { - unsafe { - let mut size: TermSize = mem::zeroed(); - ioctl::ioctl(STDOUT_FILENO, ioctl::TIOCGWINSZ, &mut size as *mut _); - Vec2::new(size.col as usize, size.row as usize) - } -}