diff --git a/src/lib.rs b/src/lib.rs index ba361c0..668e78c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,12 @@ pub struct Size { pub rows: u16, /// number of columns pub cols: u16, + + /// width in pixels + pub width: Option, + + /// height in pixels + pub height: Option, } #[cfg(unix)] diff --git a/src/nix.rs b/src/nix.rs index 1fb7f42..a5dfac1 100644 --- a/src/nix.rs +++ b/src/nix.rs @@ -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 { // 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 @@ -76,7 +67,7 @@ mod tests { let mut data = stdout.split_whitespace(); let rs = data.next().unwrap().parse::().unwrap(); let cs = data.next().unwrap().parse::().unwrap(); - if let Some(Size { rows, cols }) = get() { + if let Some(Size { rows, cols, .. }) = get() { assert_eq!(rows, rs); assert_eq!(cols, cs); } diff --git a/src/win.rs b/src/win.rs index 9d55f10..4cd600a 100644 --- a/src/win.rs +++ b/src/win.rs @@ -34,6 +34,9 @@ pub fn get() -> Option { 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, } }) }