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;

/// The enum `Winszed` is the size of the tty window.

#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub struct Winszed {
    /// Rows, in characters.
    pub ws_row: libc::c_ushort,
    /// Columns, in characters.
    pub ws_col: libc::c_ushort,
    /// Horizontal size, pixels.
    pub ws_xpixel: libc::c_ushort,
    /// Vertical size, pixels.
    pub ws_ypixel: libc::c_ushort, 
}

impl Winszed {

    /// The constructor method `new` returns the window size.
    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),
            }
        }
    }

    /// The accessor function `get_row` returns the number of rows.
    pub fn get_row(&self) -> libc::size_t {
      self.ws_row as libc::size_t
    }

    /// The accessor function `get_col` returns the number of columns.
    pub fn get_col(&self) -> libc::size_t {
      self.ws_col as libc::size_t
    }

    /// The accessor function `get_irow` returns the number of rows.
    pub fn get_irow(&self) -> libc::ssize_t {
        self.ws_row as libc::ssize_t
    }

    /// The accessor function `get_icol` returns the number of columns.
    pub fn get_icol(&self) -> libc::ssize_t {
        self.ws_col as libc::ssize_t
    }

    /// The accessor function `get_row` returns the multiplication
    /// of row by colum.
    pub fn row_by_col(&self) -> libc::size_t {
        self.get_row().mul(&self.get_col())
    }


    /// The accessor function `get_ref_row` returns a ref on the number of rows.
    pub fn get_ref_row(&mut self) -> &mut libc::c_ushort {
        &mut self.ws_row
    }

    /// The accessor function `get_xpixel` returns the horizontal size.
    pub fn get_xpixel(&self) -> libc::c_uint {
        self.ws_xpixel as libc::c_uint
    }

    /// The accessor function `get_ypixel` returns the vertical size.
    pub fn get_ypixel(&self) -> libc::c_uint {
        self.ws_ypixel as libc::c_uint
    }

    /// The method `from_winsized` changes the window size.
    #[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)
        )
    }
}