Skip to content
This repository was archived by the owner on Mar 10, 2021. It is now read-only.

Commit 7fd9eb2

Browse files
qryxipcamerondm9
authored andcommitted
WIP: Implement `dimensions_std[in|err]
1 parent ca5895f commit 7fd9eb2

File tree

1 file changed

+51
-40
lines changed

1 file changed

+51
-40
lines changed

src/platform/windows.rs

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1+
use winapi::shared::minwindef::DWORD;
12
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};
34
use winapi::um::wincon::GetConsoleScreenBufferInfo;
45
use winapi::um::wincon::{CONSOLE_SCREEN_BUFFER_INFO, COORD, SMALL_RECT};
56

67
/// Query the current processes's output, returning its width and height as a
7-
/// number of characters.
8+
/// number of characters.
89
///
910
/// # Errors
10-
///
11+
///
1112
/// Returns `None` if the output isn't to a terminal.
12-
///
13+
///
1314
/// # Example
14-
///
15+
///
1516
/// To get the dimensions of your terminal window, simply use the following:
16-
///
17+
///
1718
/// ```no_run
1819
/// # use term_size;
1920
/// if let Some((w, h)) = term_size::dimensions() {
@@ -23,42 +24,20 @@ use winapi::um::wincon::{CONSOLE_SCREEN_BUFFER_INFO, COORD, SMALL_RECT};
2324
/// }
2425
/// ```
2526
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)
4928
}
5029

5130
/// Query the current processes's output, returning its width and height as a
5231
/// number of characters. Returns `None` if the output isn't to a terminal.
5332
///
5433
/// # Errors
55-
///
34+
///
5635
/// Returns `None` if the output isn't to a terminal.
57-
///
36+
///
5837
/// # Example
59-
///
38+
///
6039
/// To get the dimensions of your terminal window, simply use the following:
61-
///
40+
///
6241
/// ```no_run
6342
/// # use term_size;
6443
/// if let Some((w, h)) = term_size::dimensions() {
@@ -67,20 +46,52 @@ pub fn dimensions() -> Option<(usize, usize)> {
6746
/// println!("Unable to get term size :(")
6847
/// }
6948
/// ```
70-
pub fn dimensions_stdout() -> Option<(usize, usize)> { dimensions() }
49+
pub fn dimensions_stdout() -> Option<(usize, usize)> {
50+
query(STD_OUTPUT_HANDLE)
51+
}
7152

7253
/// This isn't implemented for Windows
73-
///
54+
///
7455
/// # Panics
75-
///
56+
///
7657
/// This function `panic!`s unconditionally with the `unimplemented!`
7758
/// macro
78-
pub fn dimensions_stdin() -> Option<(usize, usize)> { unimplemented!() }
59+
pub fn dimensions_stdin() -> Option<(usize, usize)> {
60+
query(STD_INPUT_HANDLE)
61+
}
7962

8063
/// This isn't implemented for Windows
81-
///
64+
///
8265
/// # Panics
83-
///
66+
///
8467
/// This function `panic!`s unconditionally with the `unimplemented!`
8568
/// 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

Comments
 (0)