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
extern crate libc;
#[cfg(not(target_os = "windows"))]
use std::mem::zeroed;
#[cfg(not(target_os = "windows"))]
use libc::{STDOUT_FILENO, c_int, c_ulong, c_ushort};
#[cfg(not(target_os = "windows"))]
#[repr(C)]
struct Winsize {
ws_row: c_ushort,
ws_col: c_ushort,
}
#[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;
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)> {
None
}