Skip to content

Implement TTY::get_winsize for Windows #20612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/libstd/sys/windows/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,32 @@ pub fn fd_set(set: &mut fd_set, s: libc::SOCKET) {
set.fd_count += 1;
}

pub type SHORT = libc::c_short;

#[repr(C)]
pub struct COORD {
pub X: SHORT,
pub Y: SHORT,
}

#[repr(C)]
pub struct SMALL_RECT {
pub Left: SHORT,
pub Top: SHORT,
pub Right: SHORT,
pub Bottom: SHORT,
}

#[repr(C)]
pub struct CONSOLE_SCREEN_BUFFER_INFO {
pub dwSize: COORD,
pub dwCursorPosition: COORD,
pub wAttributes: libc::WORD,
pub srWindow: SMALL_RECT,
pub dwMaximumWindowSize: COORD,
}
pub type PCONSOLE_SCREEN_BUFFER_INFO = *mut CONSOLE_SCREEN_BUFFER_INFO;

#[link(name = "ws2_32")]
extern "system" {
pub fn WSAStartup(wVersionRequested: libc::WORD,
Expand Down Expand Up @@ -247,4 +273,8 @@ extern "system" {

pub fn SetConsoleMode(hConsoleHandle: libc::HANDLE,
lpMode: libc::DWORD) -> libc::BOOL;
pub fn GetConsoleScreenBufferInfo(
hConsoleOutput: libc::HANDLE,
lpConsoleScreenBufferInfo: PCONSOLE_SCREEN_BUFFER_INFO,
) -> libc::BOOL;
}
16 changes: 9 additions & 7 deletions src/libstd/sys/windows/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ use iter::repeat;
use libc::types::os::arch::extra::LPCVOID;
use libc::{c_int, HANDLE, LPDWORD, DWORD, LPVOID};
use libc::{get_osfhandle, CloseHandle};
use mem;
use ptr;
use str::from_utf8;
use super::c::{ENABLE_ECHO_INPUT, ENABLE_EXTENDED_FLAGS};
use super::c::{ENABLE_INSERT_MODE, ENABLE_LINE_INPUT};
use super::c::{ENABLE_PROCESSED_INPUT, ENABLE_QUICK_EDIT_MODE};
use super::c::{ERROR_ILLEGAL_CHARACTER};
use super::c::{ERROR_ILLEGAL_CHARACTER, CONSOLE_SCREEN_BUFFER_INFO};
use super::c::{ReadConsoleW, WriteConsoleW, GetConsoleMode, SetConsoleMode};
use super::c::{GetConsoleScreenBufferInfo};

fn invalid_encoding() -> IoError {
IoError {
Expand Down Expand Up @@ -146,12 +148,12 @@ impl TTY {
}

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

// Let us magically declare this as a TTY
Expand Down