From c7095c95d633e0a36ea78434bc83349a9711a187 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Sun, 9 Apr 2017 14:13:11 -0400 Subject: [PATCH] feat: allows getting the terminal size of all standard streams, or just particular ones `dimensions` will now attempt to poll all three standard streams to find the terminal size. `diminensions_{stdout,stderr,stdin}` have also been added to only poll a particular stream. `dimensions_stdout` is the equivilant of `term_size::dimensions` prior to this release --- src/lib.rs | 118 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 107 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cd1936d..a044e5c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ extern crate kernel32; #[cfg(target_os = "windows")] extern crate winapi; -pub use platform::dimensions; +pub use platform::{dimensions, dimensions_stdout, dimensions_stdin, dimensions_stderr}; #[cfg(any(target_os = "linux", target_os = "android", @@ -33,7 +33,7 @@ pub use platform::dimensions; target_os = "solaris"))] mod platform { use std::mem::zeroed; - use libc::{STDOUT_FILENO, c_int, c_ulong, winsize}; + use libc::{STDOUT_FILENO, STDIN_FILENO, STDERR_FILENO, c_int, c_ulong, winsize}; // Unfortunately the actual command is not standardised... #[cfg(any(target_os = "linux", target_os = "android"))] @@ -55,24 +55,118 @@ mod platform { fn ioctl(fd: c_int, request: c_ulong, ...) -> c_int; } - /// Runs the ioctl command. Returns (0, 0) if output is not to a terminal, or + /// Runs the ioctl command. Returns (0, 0) if all of the streams are not to a terminal, or /// there is an error. (0, 0) is an invalid size to have anyway, which is why /// it can be used as a nil value. - unsafe fn get_dimensions() -> winsize { + unsafe fn get_dimensions_any() -> winsize { let mut window: winsize = zeroed(); - let result = ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut window); + let mut result = ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut window); if result == -1 { - zeroed() - } else { - window + window = zeroed(); + result = ioctl(STDIN_FILENO, TIOCGWINSZ, &mut window); + if result == -1 { + window = zeroed(); + result = ioctl(STDERR_FILENO, TIOCGWINSZ, &mut window); + if result == -1 { + return zeroed(); + } + } } + window } - /// Query the current processes's output, returning its width and height as a - /// number of characters. Returns `None` if the output isn't to a terminal. + /// Runs the ioctl command. Returns (0, 0) if the output is not to a terminal, or + /// there is an error. (0, 0) is an invalid size to have anyway, which is why + /// it can be used as a nil value. + unsafe fn get_dimensions_out() -> winsize { + let mut window: winsize = zeroed(); + let result = ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut window); + + if result != -1 { + return window; + } + zeroed() + } + + /// Runs the ioctl command. Returns (0, 0) if the input is not to a terminal, or + /// there is an error. (0, 0) is an invalid size to have anyway, which is why + /// it can be used as a nil value. + unsafe fn get_dimensions_in() -> winsize { + let mut window: winsize = zeroed(); + let result = ioctl(STDIN_FILENO, TIOCGWINSZ, &mut window); + + if result != -1 { + return window; + } + zeroed() + } + + /// Runs the ioctl command. Returns (0, 0) if the error is not to a terminal, or + /// there is an error. (0, 0) is an invalid size to have anyway, which is why + /// it can be used as a nil value. + unsafe fn get_dimensions_err() -> winsize { + let mut window: winsize = zeroed(); + let result = ioctl(STDERR_FILENO, TIOCGWINSZ, &mut window); + + if result != -1 { + return window; + } + zeroed() + } + + /// Query the current processes's output (`stdout`), input (`stdin`), and error (`stderr`) in + /// that order, in the attempt to dtermine terminal width. If one of those streams is actually + /// a tty, this function returns its width and height as a number of characters. + /// + /// If *all* of the streams are not ttys or return any errors this function will return `None`. pub fn dimensions() -> Option<(usize, usize)> { - let w = unsafe { get_dimensions() }; + let w = unsafe { get_dimensions_any() }; + + if w.ws_col == 0 || w.ws_row == 0 { + None + } else { + Some((w.ws_col as usize, w.ws_row as usize)) + } + } + + /// Query the current processes's output (`stdout`) *only*, in the attempt to dtermine + /// terminal width. If that streams is actually a tty, this function returns its width + /// and height as a number of characters. + /// + /// If *all* of the streams are not ttys or return any errors this function will return `None`. + pub fn dimensions_stdout() -> Option<(usize, usize)> { + let w = unsafe { get_dimensions_out() }; + + if w.ws_col == 0 || w.ws_row == 0 { + None + } else { + Some((w.ws_col as usize, w.ws_row as usize)) + } + } + + /// Query the current processes's input (`stdin`) *only*, in the attempt to dtermine + /// terminal width. If that streams is actually a tty, this function returns its width + /// and height as a number of characters. + /// + /// If *all* of the streams are not ttys or return any errors this function will return `None`. + pub fn dimensions_stdin() -> Option<(usize, usize)> { + let w = unsafe { get_dimensions_in() }; + + if w.ws_col == 0 || w.ws_row == 0 { + None + } else { + Some((w.ws_col as usize, w.ws_row as usize)) + } + } + + /// Query the current processes's error output (`stderr`) *only*, in the attempt to dtermine + /// terminal width. If that streams is actually a tty, this function returns its width + /// and height as a number of characters. + /// + /// If *all* of the streams are not ttys or return any errors this function will return `None`. + pub fn dimensions_stderr() -> Option<(usize, usize)> { + let w = unsafe { get_dimensions_err() }; if w.ws_col == 0 || w.ws_row == 0 { None @@ -87,6 +181,8 @@ mod platform { use kernel32::{GetConsoleScreenBufferInfo, GetStdHandle}; use winapi::{CONSOLE_SCREEN_BUFFER_INFO, COORD, SMALL_RECT, STD_OUTPUT_HANDLE}; + /// Query the current processes's output, returning its width and height as a + /// number of characters. Returns `None` if the output isn't to a terminal. pub fn dimensions() -> Option<(usize, usize)> { let null_coord = COORD{ X: 0,