1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
use libc::{STDOUT_FILENO, STDIN_FILENO, STDERR_FILENO, c_int, c_ulong, winsize};
use std::mem::zeroed;
// Unfortunately the actual command is not standardised...
#[cfg(any(target_os = "linux", target_os = "android"))]
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"))]
static TIOCGWINSZ: c_ulong = 0x40087468;
#[cfg(target_os = "solaris")]
static TIOCGWINSZ: c_ulong = 0x5468;
extern "C" {
fn ioctl(fd: c_int, request: c_ulong, ...) -> c_int;
}
/// 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, &mut window);
if result == -1 {
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
}
/// 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 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
/// # use term_size;
/// if let Some((w, h)) = term_size::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
/// # use term_size;
/// if let Some((w, h)) = term_size::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
/// # use term_size;
/// if let Some((w, h)) = term_size::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 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
/// # use term_size;
/// if let Some((w, h)) = term_size::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))
}
}