1
+ use winapi:: shared:: minwindef:: DWORD ;
1
2
use winapi:: um:: processenv:: GetStdHandle ;
2
- use winapi:: um:: winbase:: STD_OUTPUT_HANDLE ;
3
+ use winapi:: um:: winbase:: { STD_ERROR_HANDLE , STD_INPUT_HANDLE , STD_OUTPUT_HANDLE } ;
3
4
use winapi:: um:: wincon:: GetConsoleScreenBufferInfo ;
4
5
use winapi:: um:: wincon:: { CONSOLE_SCREEN_BUFFER_INFO , COORD , SMALL_RECT } ;
5
6
6
7
/// Query the current processes's output, returning its width and height as a
7
- /// number of characters.
8
+ /// number of characters.
8
9
///
9
10
/// # Errors
10
- ///
11
+ ///
11
12
/// Returns `None` if the output isn't to a terminal.
12
- ///
13
+ ///
13
14
/// # Example
14
- ///
15
+ ///
15
16
/// To get the dimensions of your terminal window, simply use the following:
16
- ///
17
+ ///
17
18
/// ```no_run
18
19
/// # use term_size;
19
20
/// if let Some((w, h)) = term_size::dimensions() {
@@ -23,42 +24,20 @@ use winapi::um::wincon::{CONSOLE_SCREEN_BUFFER_INFO, COORD, SMALL_RECT};
23
24
/// }
24
25
/// ```
25
26
pub fn dimensions ( ) -> Option < ( usize , usize ) > {
26
- let null_coord = COORD { X : 0 , Y : 0 } ;
27
- let null_smallrect = SMALL_RECT {
28
- Left : 0 ,
29
- Top : 0 ,
30
- Right : 0 ,
31
- Bottom : 0 ,
32
- } ;
33
-
34
- let stdout_h = unsafe { GetStdHandle ( STD_OUTPUT_HANDLE ) } ;
35
- let mut console_data = CONSOLE_SCREEN_BUFFER_INFO {
36
- dwSize : null_coord,
37
- dwCursorPosition : null_coord,
38
- wAttributes : 0 ,
39
- srWindow : null_smallrect,
40
- dwMaximumWindowSize : null_coord,
41
- } ;
42
-
43
- if unsafe { GetConsoleScreenBufferInfo ( stdout_h, & mut console_data) } != 0 {
44
- Some ( ( ( console_data. srWindow . Right - console_data. srWindow . Left + 1 ) as usize ,
45
- ( console_data. srWindow . Bottom - console_data. srWindow . Top + 1 ) as usize ) )
46
- } else {
47
- None
48
- }
27
+ query ( STD_INPUT_HANDLE )
49
28
}
50
29
51
30
/// Query the current processes's output, returning its width and height as a
52
31
/// number of characters. Returns `None` if the output isn't to a terminal.
53
32
///
54
33
/// # Errors
55
- ///
34
+ ///
56
35
/// Returns `None` if the output isn't to a terminal.
57
- ///
36
+ ///
58
37
/// # Example
59
- ///
38
+ ///
60
39
/// To get the dimensions of your terminal window, simply use the following:
61
- ///
40
+ ///
62
41
/// ```no_run
63
42
/// # use term_size;
64
43
/// if let Some((w, h)) = term_size::dimensions() {
@@ -67,20 +46,52 @@ pub fn dimensions() -> Option<(usize, usize)> {
67
46
/// println!("Unable to get term size :(")
68
47
/// }
69
48
/// ```
70
- pub fn dimensions_stdout ( ) -> Option < ( usize , usize ) > { dimensions ( ) }
49
+ pub fn dimensions_stdout ( ) -> Option < ( usize , usize ) > {
50
+ query ( STD_OUTPUT_HANDLE )
51
+ }
71
52
72
53
/// This isn't implemented for Windows
73
- ///
54
+ ///
74
55
/// # Panics
75
- ///
56
+ ///
76
57
/// This function `panic!`s unconditionally with the `unimplemented!`
77
58
/// macro
78
- pub fn dimensions_stdin ( ) -> Option < ( usize , usize ) > { unimplemented ! ( ) }
59
+ pub fn dimensions_stdin ( ) -> Option < ( usize , usize ) > {
60
+ query ( STD_INPUT_HANDLE )
61
+ }
79
62
80
63
/// This isn't implemented for Windows
81
- ///
64
+ ///
82
65
/// # Panics
83
- ///
66
+ ///
84
67
/// This function `panic!`s unconditionally with the `unimplemented!`
85
68
/// macro
86
- pub fn dimensions_stderr ( ) -> Option < ( usize , usize ) > { unimplemented ! ( ) }
69
+ pub fn dimensions_stderr ( ) -> Option < ( usize , usize ) > {
70
+ query ( STD_ERROR_HANDLE )
71
+ }
72
+
73
+ fn query ( hdl : DWORD ) -> Option < ( usize , usize ) > {
74
+ let null_coord = COORD { X : 0 , Y : 0 } ;
75
+ let null_smallrect = SMALL_RECT {
76
+ Left : 0 ,
77
+ Top : 0 ,
78
+ Right : 0 ,
79
+ Bottom : 0 ,
80
+ } ;
81
+
82
+ let hdl = unsafe { GetStdHandle ( hdl) } ;
83
+ let mut console_data = CONSOLE_SCREEN_BUFFER_INFO {
84
+ dwSize : null_coord,
85
+ dwCursorPosition : null_coord,
86
+ wAttributes : 0 ,
87
+ srWindow : null_smallrect,
88
+ dwMaximumWindowSize : null_coord,
89
+ } ;
90
+
91
+ if unsafe { GetConsoleScreenBufferInfo ( hdl, & mut console_data) } != 0 {
92
+ Some ( ( ( console_data. srWindow . Right - console_data. srWindow . Left + 1 ) as usize ,
93
+ ( console_data. srWindow . Bottom - console_data. srWindow . Top + 1 ) as usize ) )
94
+ } else {
95
+ None
96
+ }
97
+ }
0 commit comments