11
11
use prelude:: v1:: * ;
12
12
13
13
use sys:: fs:: FileDesc ;
14
- use libc:: { self , c_int} ;
14
+ use libc:: { self , c_int, c_ulong , funcs } ;
15
15
use io:: { self , IoResult , IoError } ;
16
+ use sys:: c;
16
17
use sys_common;
17
18
18
19
pub struct TTY {
19
20
pub fd : FileDesc ,
20
21
}
21
22
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
+
22
30
impl TTY {
23
31
pub fn new ( fd : c_int ) -> IoResult < TTY > {
24
32
if unsafe { libc:: isatty ( fd) } != 0 {
@@ -41,8 +49,39 @@ impl TTY {
41
49
pub fn set_raw ( & mut self , _raw : bool ) -> IoResult < ( ) > {
42
50
Err ( sys_common:: unimpl ( ) )
43
51
}
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" ) ) ]
44
82
pub fn get_winsize ( & mut self ) -> IoResult < ( int , int ) > {
45
83
Err ( sys_common:: unimpl ( ) )
46
84
}
85
+
47
86
pub fn isatty ( & self ) -> bool { false }
48
87
}
0 commit comments