#[cfg(not(target_os = "windows"))]
extern crate libc;
#[cfg(target_os = "windows")]
extern crate kernel32;
#[cfg(target_os = "windows")]
extern crate winapi;
#[cfg(target_os = "windows")]
use kernel32::{GetConsoleScreenBufferInfo, GetStdHandle};
#[cfg(target_os = "windows")]
use winapi::{CONSOLE_SCREEN_BUFFER_INFO, COORD, SMALL_RECT, STD_OUTPUT_HANDLE};
#[cfg(not(target_os = "windows"))]
use std::mem::zeroed;
#[cfg(not(target_os = "windows"))]
use libc::{STDOUT_FILENO, c_int, c_ulong, winsize};
#[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" {
#[cfg(not(target_os = "windows"))]
pub fn ioctl(fd: c_int, request: c_ulong, ...) -> c_int;
}
#[cfg(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
}
}
#[cfg(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(target_os = "windows")]
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) };
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) as usize, (console_data.srWindow.Bottom - console_data.srWindow.Top) as usize))
} else {
None
}
}