Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ pub struct Size {
pub rows: u16,
/// number of columns
pub cols: u16,

/// width in pixels
pub width: Option<u16>,

/// height in pixels
pub height: Option<u16>,
}

#[cfg(unix)]
Expand Down
35 changes: 13 additions & 22 deletions src/nix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,29 @@ extern crate libc;
extern crate atty;

use self::super::Size;
use self::libc::{c_ushort, STDOUT_FILENO, TIOCGWINSZ};
use self::libc::{STDOUT_FILENO, winsize, TIOCGWINSZ};
use self::libc::ioctl;

/// A representation of the size of the current terminal
#[repr(C)]
#[derive(Debug)]
pub struct UnixSize {
/// number of rows
pub rows: c_ushort,
/// number of columns
pub cols: c_ushort,
x: c_ushort,
y: c_ushort,
}

/// Gets the current terminal size
pub fn get() -> Option<Size> {
// http://rosettacode.org/wiki/Terminal_control/Dimensions#Library:_BSD_libc
if atty::isnt() {
return None;
}
let us = UnixSize {
rows: 0,
cols: 0,
x: 0,
y: 0,
let mut us = winsize {
ws_row: 0,
ws_col: 0,
ws_xpixel: 0,
ws_ypixel: 0,
};
let r = unsafe { ioctl(STDOUT_FILENO, TIOCGWINSZ, &us) };
let r = unsafe { ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut us) };
if r == 0 {
Some(Size {
rows: us.rows,
cols: us.cols,
rows: us.ws_row,
cols: us.ws_col,

width: if us.ws_xpixel == 0 { None } else { Some(us.ws_xpixel) },
height: if us.ws_ypixel == 0 { None } else { Some(us.ws_ypixel) },
})
} else {
None
Expand Down Expand Up @@ -76,7 +67,7 @@ mod tests {
let mut data = stdout.split_whitespace();
let rs = data.next().unwrap().parse::<u16>().unwrap();
let cs = data.next().unwrap().parse::<u16>().unwrap();
if let Some(Size { rows, cols }) = get() {
if let Some(Size { rows, cols, .. }) = get() {
assert_eq!(rows, rs);
assert_eq!(cols, cs);
}
Expand Down
3 changes: 3 additions & 0 deletions src/win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub fn get() -> Option<Size> {
Size {
rows: (inf.srWindow.Bottom - inf.srWindow.Top + 1) as u16,
cols: (inf.srWindow.Right - inf.srWindow.Left + 1) as u16,

width: None,
height: None,
}
})
}