From ceddee9157b8af9dd46b8866bcc9963d292c2e92 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Wed, 29 Jun 2016 23:19:08 -0400 Subject: [PATCH] refactor(term.rs): moved term.rs into it's own crate so others can pull in just that portion The functionality can now be found in https://crates.io/crates/term_size Closes #549 --- Cargo.toml | 3 +- src/app/help.rs | 11 +++++-- src/lib.rs | 3 +- src/term.rs | 81 ------------------------------------------------- 4 files changed, 13 insertions(+), 85 deletions(-) delete mode 100644 src/term.rs diff --git a/Cargo.toml b/Cargo.toml index fe9a4c55c61..181636a922c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ strsim = { version = "~0.4.0", optional = true } yaml-rust = { version = "~0.3.2", optional = true } clippy = { version = "~0.0.74", optional = true } unicode-width = { version = "~0.1.3", optional = true } +term_size = { version = "~0.1.0", optional = true } [dev-dependencies] regex = "~0.1.69" @@ -29,7 +30,7 @@ default = ["suggestions", "color", "wrap_help"] suggestions = ["strsim"] color = ["ansi_term", "libc"] yaml = ["yaml-rust"] -wrap_help = ["libc", "unicode-width"] +wrap_help = ["libc", "unicode-width", "term_size"] lints = ["clippy", "nightly"] nightly = [] # for building with nightly and unstable features unstable = [] # for building with unstable features on stable Rust diff --git a/src/app/help.rs b/src/app/help.rs index b77e47539d8..2c60f20a531 100644 --- a/src/app/help.rs +++ b/src/app/help.rs @@ -12,7 +12,14 @@ use app::{App, AppSettings}; use app::parser::Parser; use fmt::{Format, Colorizer}; -use term; +#[cfg(all(feature = "wrap_help", not(target_os = "windows")))] +use term_size; +#[cfg(any(not(feature = "wrap_help"), target_os = "windows"))] +mod term_size { + pub fn dimensions() -> Option<(usize, usize)> { + None + } +} #[cfg(all(feature = "wrap_help", not(target_os = "windows")))] use unicode_width::UnicodeWidthStr; @@ -96,7 +103,7 @@ impl<'a> Help<'a> { hide_pv: hide_pv, term_w: match term_w { Some(width) => width, - None => term::dimensions().map(|(w, _)| w).unwrap_or(120), + None => term_size::dimensions().map(|(w, _)| w).unwrap_or(120), }, color: color, cizer: cizer, diff --git a/src/lib.rs b/src/lib.rs index 4538b4af177..22074ad9440 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -414,6 +414,8 @@ extern crate unicode_width; #[macro_use] extern crate bitflags; extern crate vec_map; +#[cfg(feature = "wrap_help")] +extern crate term_size; #[cfg(feature = "yaml")] pub use yaml_rust::YamlLoader; @@ -431,7 +433,6 @@ mod fmt; mod suggestions; mod errors; mod osstringext; -mod term; mod strext; const INTERNAL_ERROR_MSG: &'static str = "Fatal internal error. Please consider filing a bug \ diff --git a/src/term.rs b/src/term.rs deleted file mode 100644 index f4ac97a57b7..00000000000 --- a/src/term.rs +++ /dev/null @@ -1,81 +0,0 @@ -// The following was taken and adapated from exa source -// repo: https://github.com/ogham/exa -// commit: b9eb364823d0d4f9085eb220233c704a13d0f611 -// license: MIT - Copyright (c) 2014 Benjamin Sago - -//! System calls for getting the terminal size. -//! -//! Getting the terminal size is performed using an ioctl command that takes -//! the file handle to the terminal -- which in this case, is stdout -- and -//! populates a structure containing the values. -//! -//! The size is needed when the user wants the output formatted into columns: -//! the default grid view, or the hybrid grid-details view. - -#[cfg(all(feature = "wrap_help", not(target_os = "windows")))] -use std::mem::zeroed; -#[cfg(all(feature = "wrap_help", not(target_os = "windows")))] -use libc::{STDOUT_FILENO, c_int, c_ulong, c_ushort}; - - -/// The number of rows and columns of a terminal. -#[cfg(all(feature = "wrap_help", not(target_os = "windows")))] -#[repr(C)] -struct Winsize { - ws_row: c_ushort, - ws_col: c_ushort, -} - -// Unfortunately the actual command is not standardised... - -#[cfg(any(target_os = "linux", target_os = "android"))] -#[cfg(feature = "wrap_help")] -static TIOCGWINSZ: c_ulong = 0x5413; - -#[cfg(any(target_os = "macos", - target_os = "ios", - target_os = "bitrig", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd"))] -#[cfg(feature = "wrap_help")] -static TIOCGWINSZ: c_ulong = 0x40087468; - -extern "C" { -#[cfg(all(feature = "wrap_help", not(target_os = "windows")))] - pub 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 -/// there is an error. (0, 0) is an invalid size to have anyway, which is why -/// it can be used as a nil value. -#[cfg(all(feature = "wrap_help", not(target_os = "windows")))] -unsafe fn get_dimensions() -> Winsize { - let mut window: Winsize = zeroed(); - let result = ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut window); - - if result == -1 { - zeroed() - } else { - 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. -#[cfg(all(feature = "wrap_help", not(target_os = "windows")))] -pub fn dimensions() -> Option<(usize, usize)> { - let w = unsafe { get_dimensions() }; - - if w.ws_col == 0 || w.ws_row == 0 { - None - } else { - Some((w.ws_col as usize, w.ws_row as usize)) - } -} - -#[cfg(any(not(feature = "wrap_help"), target_os = "windows"))] -pub fn dimensions() -> Option<(usize, usize)> { - None -}