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
mod err;
pub use self::err::WinszedError;
use std::ops::{BitAnd, Mul};
use ::libc;
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub struct Winszed {
pub ws_row: libc::c_ushort,
pub ws_col: libc::c_ushort,
pub ws_xpixel: libc::c_ushort,
pub ws_ypixel: libc::c_ushort,
}
impl Winszed {
pub fn new(fd: libc::c_int) -> Result<Self, WinszedError> {
unsafe {
let winsz: Winszed = Winszed::default();
match libc::ioctl(fd, libc::TIOCGWINSZ, &winsz) {
-1 => Err(WinszedError::GwFail),
_ => Ok(winsz),
}
}
}
pub fn get_row(&self) -> libc::size_t {
self.ws_row as libc::size_t
}
pub fn get_col(&self) -> libc::size_t {
self.ws_col as libc::size_t
}
pub fn get_irow(&self) -> libc::ssize_t {
self.ws_row as libc::ssize_t
}
pub fn get_icol(&self) -> libc::ssize_t {
self.ws_col as libc::ssize_t
}
pub fn row_by_col(&self) -> libc::size_t {
self.get_row().mul(&self.get_col())
}
pub fn get_ref_row(&mut self) -> &mut libc::c_ushort {
&mut self.ws_row
}
pub fn get_xpixel(&self) -> libc::c_uint {
self.ws_xpixel as libc::c_uint
}
pub fn get_ypixel(&self) -> libc::c_uint {
self.ws_ypixel as libc::c_uint
}
#[allow(dead_code)]
pub fn from_winsized(fd: libc::c_int, winsize: &Winszed) -> Result<(), WinszedError> {
unsafe {
match libc::ioctl(fd, libc::TIOCSWINSZ, winsize) {
-1 => Err(WinszedError::GsFail),
_ => Ok(()),
}
}
}
}
impl PartialEq for Winszed {
fn eq(&self, other: &Winszed) -> bool {
self.ws_row.eq(&other.ws_row).bitand(
self.ws_col.eq(&other.ws_col)
)
}
}