ioctl_term_light/
dimensions.rs

1#![allow(unused)]
2use crate::c::{c_int, ioctl_get_dimensions};
3use crate::default::{tuple, tuple_from_raw_fd};
4use std::os::fd::AsRawFd;
5
6#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
7pub struct Dimensions {
8    pub width: u16,
9    pub height: u16,
10}
11impl Dimensions {
12    pub fn to_tuple<Size: From<u16>>(&self) -> (Size, Size) {
13        (Size::from(self.width), Size::from(self.height))
14    }
15    pub fn to_slice<Size: From<u16>>(&self) -> [Size; 2] {
16        [Size::from(self.width), Size::from(self.height)]
17    }
18
19    pub fn to_u16_tuple(&self) -> (u16, u16) {
20        self.to_tuple::<u16>().into()
21    }
22    pub fn to_u16_slice(&self) -> (u16, u16) {
23        self.to_slice::<u16>().into()
24    }
25
26    pub fn to_usize_tuple(&self) -> (usize, usize) {
27        self.to_tuple::<usize>().into()
28    }
29    pub fn to_usize_slice(&self) -> (usize, usize) {
30        self.to_slice::<usize>().into()
31    }
32
33    pub fn from_raw_fd<FileDescriptor: AsRawFd>(fd: FileDescriptor) -> Dimensions {
34        let tuple = tuple_from_raw_fd(fd);
35        Dimensions::from(tuple)
36    }
37    pub fn get() -> Dimensions {
38        let tuple = tuple();
39        Dimensions::from(tuple)
40    }
41}
42impl<Size> Into<(Size, Size)> for Dimensions
43where
44    Size: From<u16>,
45{
46    fn into(self) -> (Size, Size) {
47        self.to_tuple()
48    }
49}
50impl<Size> Into<[Size; 2]> for Dimensions
51where
52    Size: From<u16>,
53{
54    fn into(self) -> [Size; 2] {
55        self.to_slice()
56    }
57}
58
59impl From<(u16, u16)> for Dimensions {
60    fn from(dim: (u16, u16)) -> Dimensions {
61        let (width, height) = dim;
62        Dimensions { width, height }
63    }
64}
65
66impl From<Option<(u16, u16)>> for Dimensions {
67    fn from(dim: Option<(u16, u16)>) -> Dimensions {
68        let (width, height) = match dim {
69            Some(dim) => dim,
70            None => (0, 0),
71        };
72        Dimensions { width, height }
73    }
74}
75
76#[cfg(feature = "u8")]
77mod dimensions_u8 {
78    use super::Dimensions;
79    impl From<(u8, u8)> for Dimensions {
80        fn from(dim: (u8, u8)) -> Dimensions {
81            let (width, height) = dim;
82            let width = width as u16;
83            let height = height as u16;
84            Dimensions { width, height }
85        }
86    }
87
88    impl From<Option<(u8, u8)>> for Dimensions {
89        fn from(dim: Option<(u8, u8)>) -> Dimensions {
90            let (width, height) = match dim {
91                Some(dim) => dim,
92                None => (0, 0),
93            };
94            let width = width as u16;
95            let height = height as u16;
96
97            Dimensions { width, height }
98        }
99    }
100}