From d34516686c067895f6f00ec6269219431537e28b Mon Sep 17 00:00:00 2001 From: Pavan Kumar Sunkara Date: Sun, 26 Apr 2020 18:57:54 +0200 Subject: [PATCH] Use terminal_size for determining term size --- Cargo.toml | 1 + src/unix_term.rs | 20 ++------------------ src/wasm_term.rs | 2 ++ src/windows_term.rs | 11 ++--------- 4 files changed, 7 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8c8b364f..4557f28a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ ansi-parsing = ["regex"] clicolors-control = "1.0.1" lazy_static = "1" libc = "0.2" +terminal_size = "0.1.11" regex = { version = "1.3.1", optional = true, default-features = false, features = ["std"] } unicode-width = { version = "0.1", optional = true } diff --git a/src/unix_term.rs b/src/unix_term.rs index baa6aa91..e7e1ef5f 100644 --- a/src/unix_term.rs +++ b/src/unix_term.rs @@ -2,7 +2,6 @@ use std::fmt::Display; use std::fs; use std::io; use std::io::{BufRead, BufReader}; -use std::mem; use std::os::unix::io::AsRawFd; use std::str; @@ -18,24 +17,9 @@ pub fn is_a_terminal(out: &Term) -> bool { unsafe { libc::isatty(out.as_raw_fd()) != 0 } } +#[inline] pub fn terminal_size() -> Option<(u16, u16)> { - unsafe { - if libc::isatty(libc::STDOUT_FILENO) != 1 { - return None; - } - - let mut winsize: libc::winsize = mem::zeroed(); - - // FIXME: ".into()" used as a temporary fix for a libc bug - // https://github.com/rust-lang/libc/pull/704 - #[allow(clippy::identity_conversion)] - libc::ioctl(libc::STDOUT_FILENO, libc::TIOCGWINSZ.into(), &mut winsize); - if winsize.ws_row > 0 && winsize.ws_col > 0 { - Some((winsize.ws_row as u16, winsize.ws_col as u16)) - } else { - None - } - } + terminal_size::terminal_size().map(|x| ((x.0).0, (x.1).0)) } pub fn read_secure() -> io::Result { diff --git a/src/wasm_term.rs b/src/wasm_term.rs index c2123949..9424ccd0 100644 --- a/src/wasm_term.rs +++ b/src/wasm_term.rs @@ -13,6 +13,7 @@ pub fn is_a_terminal(_out: &Term) -> bool { false } +#[inline] pub fn terminal_size() -> Option<(u16, u16)> { None } @@ -31,6 +32,7 @@ pub fn read_single_key() -> io::Result { )) } +#[inline] pub fn wants_emoji() -> bool { false } diff --git a/src/windows_term.rs b/src/windows_term.rs index 4e7dd4c1..4dceddf7 100644 --- a/src/windows_term.rs +++ b/src/windows_term.rs @@ -74,16 +74,9 @@ unsafe fn console_on_any(fds: &[DWORD]) -> bool { false } +#[inline] pub fn terminal_size() -> Option<(u16, u16)> { - let hand = unsafe { GetStdHandle(STD_OUTPUT_HANDLE) }; - if let Some((_, csbi)) = get_console_screen_buffer_info(hand) { - Some(( - (csbi.srWindow.Bottom - csbi.srWindow.Top) as u16, - (csbi.srWindow.Right - csbi.srWindow.Left) as u16, - )) - } else { - None - } + terminal_size::terminal_size().map(|x| ((x.0).0, (x.1).0)) } pub fn move_cursor_to(out: &Term, x: usize, y: usize) -> io::Result<()> {