Skip to content

Commit 0899807

Browse files
committed
Auto merge of #20613 - dgriffen:master, r=alexcrichton
2 parents 9bac017 + ec88426 commit 0899807

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

src/libstd/sys/unix/tty.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@
1111
use prelude::v1::*;
1212

1313
use sys::fs::FileDesc;
14-
use libc::{self, c_int};
14+
use libc::{self, c_int, c_ulong, funcs};
1515
use io::{self, IoResult, IoError};
16+
use sys::c;
1617
use sys_common;
1718

1819
pub struct TTY {
1920
pub fd: FileDesc,
2021
}
2122

23+
#[cfg(any(target_os = "macos",
24+
target_os = "freebsd"))]
25+
const TIOCGWINSZ: c_ulong = 0x40087468;
26+
27+
#[cfg(any(target_os = "linux", target_os = "android"))]
28+
const TIOCGWINSZ: c_ulong = 0x00005413;
29+
2230
impl TTY {
2331
pub fn new(fd: c_int) -> IoResult<TTY> {
2432
if unsafe { libc::isatty(fd) } != 0 {
@@ -41,8 +49,39 @@ impl TTY {
4149
pub fn set_raw(&mut self, _raw: bool) -> IoResult<()> {
4250
Err(sys_common::unimpl())
4351
}
52+
53+
#[cfg(any(target_os = "linux",
54+
target_os = "android",
55+
target_os = "macos",
56+
target_os = "freebsd"))]
57+
pub fn get_winsize(&mut self) -> IoResult<(int, int)> {
58+
unsafe {
59+
#[repr(C)]
60+
struct winsize {
61+
ws_row: u16,
62+
ws_col: u16,
63+
ws_xpixel: u16,
64+
ws_ypixel: u16
65+
}
66+
67+
let mut size = winsize { ws_row: 0, ws_col: 0, ws_xpixel: 0, ws_ypixel: 0 };
68+
if c::ioctl(self.fd.fd(), TIOCGWINSZ, &mut size) == -1 {
69+
Err(IoError {
70+
kind: io::OtherIoError,
71+
desc: "Size of terminal could not be determined",
72+
detail: None,
73+
})
74+
} else {
75+
Ok((size.ws_col as int, size.ws_row as int))
76+
}
77+
}
78+
}
79+
80+
#[cfg(any(target_os = "ios",
81+
target_os = "dragonfly"))]
4482
pub fn get_winsize(&mut self) -> IoResult<(int, int)> {
4583
Err(sys_common::unimpl())
4684
}
85+
4786
pub fn isatty(&self) -> bool { false }
4887
}

0 commit comments

Comments
 (0)