ioctl_term_light/
default.rs

1#![allow(unused)]
2use crate::c::ioctl_get_dimensions;
3use crate::impl_raw_fd;
4use crate::Dimensions;
5use std::os::fd::AsRawFd;
6
7impl_raw_fd!(Stdin, 0);
8impl_raw_fd!(Stdout, 1);
9impl_raw_fd!(Stderr, 2);
10
11/// Attempts to obtain a tuple with (rows, columns) of the given [std::os::fd::AsRawFd] implementor.
12///
13/// ```
14/// use ioctl_term_light::tuple_from_raw_fd;
15///
16/// assert_eq!(tuple_from_raw_fd(ioctl_term_light::Stdout), None);
17///
18/// assert_eq!(tuple_from_raw_fd(ioctl_term_light::Stdin), None);
19///
20/// assert_eq!(tuple_from_raw_fd(ioctl_term_light::Stderr), None);
21/// ```
22pub fn tuple_from_raw_fd<FileDescriptor: AsRawFd>(fd: FileDescriptor) -> Option<(u16, u16)> {
23    let no = fd.as_raw_fd();
24    let ws = unsafe { ioctl_get_dimensions(no) };
25    if ws.ws_col == 0 || ws.ws_row == 0 {
26        None
27    } else {
28        Some((ws.ws_col as u16, ws.ws_row as u16))
29    }
30}
31
32/// Returns a tuple with (rows, columns) of the current process'es (stdout, stdin and stderr) sequentially.
33///
34/// ```
35/// use ioctl_term_light::tuple;
36///
37/// assert_eq!(tuple(), (0, 0));
38/// ```
39pub fn tuple() -> (u16, u16) {
40    None::<(u16, u16)>
41        .or_else(|| tuple_from_raw_fd(Stdout))
42        .or_else(|| tuple_from_raw_fd(Stdin))
43        .or_else(|| tuple_from_raw_fd(Stderr))
44        .unwrap_or_else(|| (0u16, 0u16))
45}
46
47/// Returns the number of columns in the current terminal via [tuple](crate::tuple)
48///
49/// ```
50/// use ioctl_term_light::cols;
51///
52/// assert_eq!(cols(), 0);
53/// ```
54pub fn cols() -> u16 {
55    tuple().0
56}
57
58/// Returns the number of rows in the current terminal via [tuple](crate::tuple)
59///
60/// ```
61/// use ioctl_term_light::rows;
62///
63/// assert_eq!(rows(), 0);
64/// ```
65pub fn rows() -> u16 {
66    tuple().1
67}
68
69#[cfg(test)]
70mod tests {
71    use super::{cols, rows, tuple, tuple_from_raw_fd, Stderr, Stdin, Stdout};
72
73    #[test]
74    fn test_tuple_from_raw_fd_stdout() {
75        assert_ne!(
76            tuple_from_raw_fd(Stdout),
77            None,
78            "expected tuple_from_raw_fd(Stdout) to not be None"
79        );
80        assert_ne!(
81            tuple_from_raw_fd(Stdout),
82            Some((0, 0)),
83            "expected tuple_from_raw_fd(Stdout) to not be Some((0, 0))"
84        );
85    }
86
87    #[test]
88    fn test_tuple_from_raw_fd_stdin() {
89        assert_ne!(
90            tuple_from_raw_fd(Stdin),
91            None,
92            "expected tuple_from_raw_fd(Stdin) to not be None"
93        );
94        assert_ne!(
95            tuple_from_raw_fd(Stdin),
96            Some((0, 0)),
97            "expected tuple_from_raw_fd(Stdin) to not be Some((0, 0))"
98        );
99    }
100
101    #[test]
102    fn test_tuple_from_raw_fd_stderr() {
103        assert_ne!(
104            tuple_from_raw_fd(Stderr),
105            None,
106            "expected tuple_from_raw_fd(Stderr) to not be None"
107        );
108        assert_ne!(
109            tuple_from_raw_fd(Stderr),
110            Some((0, 0)),
111            "expected tuple_from_raw_fd(Stderr) to not be Some((0, 0))"
112        );
113    }
114
115    #[test]
116    fn test_cols() {
117        let result = cols();
118        assert_ne!(result, 0, "expected cols() to not be 0 (zero)");
119    }
120
121    #[test]
122    fn test_rows() {
123        let result = rows();
124        assert_ne!(result, 0, "expected rows() to not be 0 (zero)");
125    }
126
127    #[test]
128    fn test_tuple() {
129        let result = tuple();
130        assert_ne!(result, (0, 0), "expected tuple() to not be (0, 0)");
131    }
132}
133
134#[macro_export]
135macro_rules! impl_raw_fd {
136    ($name:ident, $fd:literal) => {
137        /// Represents the standard file descriptor (.i.e.: stdout, stdin or stderr), equivalent to [`std::io::stdout`](std::io::stdout), [`std::io::stdin`](std::io::stdin) or [`std::io::stderr`](std::io::stderr) (respectively)
138        #[derive(Clone, Copy, Debug)]
139        pub struct $name;
140        impl std::os::fd::AsRawFd for $name {
141            fn as_raw_fd(&self) -> std::os::fd::RawFd {
142                $fd
143            }
144        }
145    };
146}