Skip to content

Commit 351e6b7

Browse files
committed
Implement TTY::get_winsize for Windows
Signed-off-by: Peter Atashian <retep998@gmail.com>
1 parent c7dd3c4 commit 351e6b7

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

src/libstd/sys/windows/c.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,32 @@ pub fn fd_set(set: &mut fd_set, s: libc::SOCKET) {
8484
set.fd_count += 1;
8585
}
8686

87+
pub type SHORT = libc::c_short;
88+
89+
#[repr(C)]
90+
pub struct COORD {
91+
pub X: SHORT,
92+
pub Y: SHORT,
93+
}
94+
95+
#[repr(C)]
96+
pub struct SMALL_RECT {
97+
pub Left: SHORT,
98+
pub Top: SHORT,
99+
pub Right: SHORT,
100+
pub Bottom: SHORT,
101+
}
102+
103+
#[repr(C)]
104+
pub struct CONSOLE_SCREEN_BUFFER_INFO {
105+
pub dwSize: COORD,
106+
pub dwCursorPosition: COORD,
107+
pub wAttributes: libc::WORD,
108+
pub srWindow: SMALL_RECT,
109+
pub dwMaximumWindowSize: COORD,
110+
}
111+
pub type PCONSOLE_SCREEN_BUFFER_INFO = *mut CONSOLE_SCREEN_BUFFER_INFO;
112+
87113
#[link(name = "ws2_32")]
88114
extern "system" {
89115
pub fn WSAStartup(wVersionRequested: libc::WORD,
@@ -247,4 +273,8 @@ extern "system" {
247273

248274
pub fn SetConsoleMode(hConsoleHandle: libc::HANDLE,
249275
lpMode: libc::DWORD) -> libc::BOOL;
276+
pub fn GetConsoleScreenBufferInfo(
277+
hConsoleOutput: libc::HANDLE,
278+
lpConsoleScreenBufferInfo: PCONSOLE_SCREEN_BUFFER_INFO,
279+
) -> libc::BOOL;
250280
}

src/libstd/sys/windows/tty.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ use iter::repeat;
3232
use libc::types::os::arch::extra::LPCVOID;
3333
use libc::{c_int, HANDLE, LPDWORD, DWORD, LPVOID};
3434
use libc::{get_osfhandle, CloseHandle};
35+
use mem;
3536
use ptr;
3637
use str::from_utf8;
3738
use super::c::{ENABLE_ECHO_INPUT, ENABLE_EXTENDED_FLAGS};
3839
use super::c::{ENABLE_INSERT_MODE, ENABLE_LINE_INPUT};
3940
use super::c::{ENABLE_PROCESSED_INPUT, ENABLE_QUICK_EDIT_MODE};
40-
use super::c::{ERROR_ILLEGAL_CHARACTER};
41+
use super::c::{ERROR_ILLEGAL_CHARACTER, CONSOLE_SCREEN_BUFFER_INFO};
4142
use super::c::{ReadConsoleW, WriteConsoleW, GetConsoleMode, SetConsoleMode};
43+
use super::c::{GetConsoleScreenBufferInfo};
4244

4345
fn invalid_encoding() -> IoError {
4446
IoError {
@@ -146,12 +148,12 @@ impl TTY {
146148
}
147149

148150
pub fn get_winsize(&mut self) -> IoResult<(int, int)> {
149-
// FIXME
150-
// Get console buffer via CreateFile with CONOUT$
151-
// Make a CONSOLE_SCREEN_BUFFER_INFO
152-
// Call GetConsoleScreenBufferInfo
153-
// Maybe call GetLargestConsoleWindowSize instead?
154-
Err(super::unimpl())
151+
let mut info: CONSOLE_SCREEN_BUFFER_INFO = unsafe { mem::zeroed() };
152+
match unsafe { GetConsoleScreenBufferInfo(self.handle, &mut info as *mut _) } {
153+
0 => Err(super::last_error()),
154+
_ => Ok(((info.srWindow.Right + 1 - info.srWindow.Left) as int,
155+
(info.srWindow.Bottom + 1 - info.srWindow.Top) as int)),
156+
}
155157
}
156158

157159
// Let us magically declare this as a TTY

0 commit comments

Comments
 (0)