summaryrefslogtreecommitdiffstats
path: root/vendor/termize/src
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/termize/src')
-rw-r--r--vendor/termize/src/lib.rs34
-rw-r--r--vendor/termize/src/platform/mod.rs15
-rw-r--r--vendor/termize/src/platform/unix.rs232
-rw-r--r--vendor/termize/src/platform/unsupported.rs12
-rw-r--r--vendor/termize/src/platform/windows.rs107
5 files changed, 400 insertions, 0 deletions
diff --git a/vendor/termize/src/lib.rs b/vendor/termize/src/lib.rs
new file mode 100644
index 000000000..87be69413
--- /dev/null
+++ b/vendor/termize/src/lib.rs
@@ -0,0 +1,34 @@
+//! 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.
+//!
+//! # Example
+//!
+//! To get the dimensions of your terminal window, simply use the following:
+//!
+//! ```no_run
+//! if let Some((w, h)) = termize::dimensions() {
+//! println!("Width: {}\nHeight: {}", w, h);
+//! } else {
+//! println!("Unable to get term size :(");
+//! }
+//! ```
+#![deny(
+ missing_docs,
+ missing_debug_implementations,
+ missing_copy_implementations,
+ trivial_casts,
+ unused_import_braces,
+ unused_allocation,
+ unused_qualifications,
+ trivial_numeric_casts
+)]
+
+// A facade to allow exposing functions depending on the platform
+mod platform;
+pub use crate::platform::{dimensions, dimensions_stderr, dimensions_stdin, dimensions_stdout};
diff --git a/vendor/termize/src/platform/mod.rs b/vendor/termize/src/platform/mod.rs
new file mode 100644
index 000000000..c48d001e9
--- /dev/null
+++ b/vendor/termize/src/platform/mod.rs
@@ -0,0 +1,15 @@
+#[cfg(unix)]
+mod unix;
+#[cfg(unix)]
+pub use self::unix::{dimensions, dimensions_stderr, dimensions_stdin, dimensions_stdout};
+
+#[cfg(windows)]
+mod windows;
+#[cfg(windows)]
+pub use self::windows::{dimensions, dimensions_stderr, dimensions_stdin, dimensions_stdout};
+
+// makes project compilable on unsupported platforms
+#[cfg(not(any(unix, windows)))]
+mod unsupported;
+#[cfg(not(any(unix, windows)))]
+pub use self::unsupported::{dimensions, dimensions_stderr, dimensions_stdin, dimensions_stdout};
diff --git a/vendor/termize/src/platform/unix.rs b/vendor/termize/src/platform/unix.rs
new file mode 100644
index 000000000..0420789b7
--- /dev/null
+++ b/vendor/termize/src/platform/unix.rs
@@ -0,0 +1,232 @@
+// Supress warnings for `TIOCGWINSZ.into()` since freebsd requires it.
+#![allow(clippy::identity_conversion)]
+
+use libc::{ioctl, winsize, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO, TIOCGWINSZ};
+use std::mem::zeroed;
+
+/// 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_any() -> winsize {
+ let mut window: winsize = zeroed();
+ let mut result = ioctl(STDOUT_FILENO, TIOCGWINSZ.into(), &mut window);
+
+ if result == -1 {
+ window = zeroed();
+ result = ioctl(STDIN_FILENO, TIOCGWINSZ.into(), &mut window);
+ if result == -1 {
+ window = zeroed();
+ result = ioctl(STDERR_FILENO, TIOCGWINSZ.into(), &mut window);
+ if result == -1 {
+ return zeroed();
+ }
+ }
+ }
+ window
+}
+
+/// 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.into(), &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.into(), &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.into(), &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 determine terminal width. If one of those streams is actually
+/// a tty, this function returns its width and height as a number of characters.
+///
+/// # Errors
+///
+/// If *all* of the streams are not ttys or return any errors this function will return `None`.
+///
+/// # Example
+///
+/// To get the dimensions of your terminal window, simply use the following:
+///
+/// ```no_run
+/// if let Some((w, h)) = termize::dimensions() {
+/// println!("Width: {}\nHeight: {}", w, h);
+/// } else {
+/// println!("Unable to get term size :(");
+/// }
+/// ```
+pub fn dimensions() -> Option<(usize, usize)> {
+ 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 determine
+/// terminal width. If that stream is actually a tty, this function returns its width
+/// and height as a number of characters.
+///
+/// # Errors
+///
+/// If the stream is not a tty or return any errors this function will return `None`.
+///
+/// # Example
+///
+/// To get the dimensions of your terminal window, simply use the following:
+///
+/// ```no_run
+/// if let Some((w, h)) = termize::dimensions_stdout() {
+/// println!("Width: {}\nHeight: {}", w, h);
+/// } else {
+/// println!("Unable to get term size :(");
+/// }
+/// ```
+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 determine
+/// terminal width. If that stream is actually a tty, this function returns its width
+/// and height as a number of characters.
+///
+/// # Errors
+///
+/// If the stream is not a tty or return any errors this function will return `None`.
+///
+/// # Example
+///
+/// To get the dimensions of your terminal window, simply use the following:
+///
+/// ```no_run
+/// if let Some((w, h)) = termize::dimensions_stdin() {
+/// println!("Width: {}\nHeight: {}", w, h);
+/// } else {
+/// println!("Unable to get term size :(");
+/// }
+/// ```
+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 stream is actually a tty, this function returns its width
+/// and height as a number of characters.
+///
+/// # Errors
+///
+/// If the stream is not a tty or return any errors this function will return `None`.
+///
+/// # Example
+///
+/// To get the dimensions of your terminal window, simply use the following:
+///
+/// ```no_run
+/// if let Some((w, h)) = termize::dimensions_stderr() {
+/// println!("Width: {}\nHeight: {}", w, h);
+/// } else {
+/// println!("Unable to get term size :(");
+/// }
+/// ```
+pub fn dimensions_stderr() -> Option<(usize, usize)> {
+ let w = unsafe { get_dimensions_err() };
+
+ if w.ws_col == 0 || w.ws_row == 0 {
+ None
+ } else {
+ Some((w.ws_col as usize, w.ws_row as usize))
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::dimensions;
+ use std::process::{Command, Output, Stdio};
+
+ #[cfg(target_os = "macos")]
+ fn stty_size() -> Output {
+ Command::new("stty")
+ .arg("-f")
+ .arg("/dev/stderr")
+ .arg("size")
+ .stderr(Stdio::inherit())
+ .output()
+ .unwrap()
+ }
+
+ #[cfg(not(target_os = "macos"))]
+ fn stty_size() -> Output {
+ Command::new("stty")
+ .arg("-F")
+ .arg("/dev/stderr")
+ .arg("size")
+ .stderr(Stdio::inherit())
+ .output()
+ .expect("failed to run `stty_size()`")
+ }
+
+ #[test]
+ fn test_shell() {
+ let output = stty_size();
+ let stdout = String::from_utf8(output.stdout).expect("failed to turn into String");
+ let mut data = stdout.split_whitespace();
+ let rs = data
+ .next()
+ .unwrap_or("0")
+ .parse::<usize>()
+ .expect("failed to parse rows");
+ let cs = data
+ .next()
+ .unwrap_or("0")
+ .parse::<usize>()
+ .expect("failed to parse cols");
+ println!("stdout: {}", stdout);
+ println!("rows: {}\ncols: {}", rs, cs);
+ if let Some((w, h)) = dimensions() {
+ assert_eq!(rs, h);
+ assert_eq!(cs, w);
+ }
+ }
+}
diff --git a/vendor/termize/src/platform/unsupported.rs b/vendor/termize/src/platform/unsupported.rs
new file mode 100644
index 000000000..a246eee58
--- /dev/null
+++ b/vendor/termize/src/platform/unsupported.rs
@@ -0,0 +1,12 @@
+pub fn dimensions() -> Option<(usize, usize)> {
+ None
+}
+pub fn dimensions_stdout() -> Option<(usize, usize)> {
+ None
+}
+pub fn dimensions_stdin() -> Option<(usize, usize)> {
+ None
+}
+pub fn dimensions_stderr() -> Option<(usize, usize)> {
+ None
+}
diff --git a/vendor/termize/src/platform/windows.rs b/vendor/termize/src/platform/windows.rs
new file mode 100644
index 000000000..6d9d0c1a2
--- /dev/null
+++ b/vendor/termize/src/platform/windows.rs
@@ -0,0 +1,107 @@
+use winapi::um::handleapi::INVALID_HANDLE_VALUE;
+use winapi::um::processenv::GetStdHandle;
+use winapi::um::winbase::STD_OUTPUT_HANDLE;
+use winapi::um::wincon::GetConsoleScreenBufferInfo;
+use winapi::um::wincon::{CONSOLE_SCREEN_BUFFER_INFO, COORD, SMALL_RECT};
+
+/// Query the current processes's output, returning its width and height as a
+/// number of characters.
+///
+/// # Errors
+///
+/// Returns `None` if the output isn't to a terminal.
+///
+/// # Example
+///
+/// To get the dimensions of your terminal window, simply use the following:
+///
+/// ```no_run
+/// if let Some((w, h)) = termize::dimensions() {
+/// println!("Width: {}\nHeight: {}", w, h);
+/// } else {
+/// println!("Unable to get term size :(");
+/// }
+/// ```
+pub fn dimensions() -> Option<(usize, usize)> {
+ let null_coord = COORD { X: 0, Y: 0 };
+ let null_smallrect = SMALL_RECT {
+ Left: 0,
+ Top: 0,
+ Right: 0,
+ Bottom: 0,
+ };
+
+ let stdout_h = unsafe { GetStdHandle(STD_OUTPUT_HANDLE) };
+ if stdout_h == INVALID_HANDLE_VALUE {
+ return None;
+ }
+
+ let mut console_data = CONSOLE_SCREEN_BUFFER_INFO {
+ dwSize: null_coord,
+ dwCursorPosition: null_coord,
+ wAttributes: 0,
+ srWindow: null_smallrect,
+ dwMaximumWindowSize: null_coord,
+ };
+
+ if unsafe { GetConsoleScreenBufferInfo(stdout_h, &mut console_data) } != 0 {
+ Some((
+ (console_data.srWindow.Right - console_data.srWindow.Left + 1) as usize,
+ (console_data.srWindow.Bottom - console_data.srWindow.Top + 1) as usize,
+ ))
+ } else {
+ None
+ }
+}
+
+/// 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.
+///
+/// # Errors
+///
+/// Returns `None` if the output isn't to a terminal.
+///
+/// # Example
+///
+/// To get the dimensions of your terminal window, simply use the following:
+///
+/// ```no_run
+/// if let Some((w, h)) = termize::dimensions() {
+/// println!("Width: {}\nHeight: {}", w, h);
+/// } else {
+/// println!("Unable to get term size :(");
+/// }
+/// ```
+pub fn dimensions_stdout() -> Option<(usize, usize)> {
+ dimensions()
+}
+
+/// This isn't implemented for Windows
+///
+/// # Panics
+///
+/// This function `panic!`s unconditionally with the `unimplemented!`
+/// macro
+pub fn dimensions_stdin() -> Option<(usize, usize)> {
+ unimplemented!()
+}
+
+/// This isn't implemented for Windows
+///
+/// # Panics
+///
+/// This function `panic!`s unconditionally with the `unimplemented!`
+/// macro
+pub fn dimensions_stderr() -> Option<(usize, usize)> {
+ unimplemented!()
+}
+
+// Just check if function works well. `dimensions()` on no terminal always
+// returns `None` like CI so don't check with `is_some()`. Please test with
+// with `--nocapture` on local, to check terminal size.
+#[test]
+fn just_check_work() {
+ if let Some((w, h)) = dimensions() {
+ println!("width: {}\nheight: {}", w, h);
+ }
+}