blob: 275ab6023d2b0cd73554ddadccd1d3ca1d3c96c1 (
plain) (
blame)
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
|
use std::fs::File;
use std::io::Error;
use std::mem;
use std::os::raw::{c_int, c_ushort};
use std::os::unix::io::AsRawFd;
#[repr(C)]
struct Winsize {
ws_row: c_ushort,
ws_col: c_ushort,
ws_xpixel: c_ushort,
ws_ypixel: c_ushort,
}
const TIOCGWINSZ: c_int = 0x5413;
extern "C" {
fn ioctl(fd: c_int, request: c_int, ...) -> c_int;
}
pub fn get_terminal_size() -> Result<(u16, u16), Error> {
let stdout = File::open("/dev/tty")?;
let fd = stdout.as_raw_fd();
let mut ws: Winsize = unsafe { mem::zeroed() };
let result = unsafe { ioctl(fd, TIOCGWINSZ, &mut ws) };
if result == -1 {
return Err(Error::last_os_error());
}
Ok((ws.ws_col, ws.ws_row))
}
|