feat(help): support wrapping help at console width on windows

Closes #455
This commit is contained in:
nabijaczleweli 2016-08-26 13:11:55 +02:00
parent 853a90de01
commit 0d93d9b953
No known key found for this signature in database
GPG key ID: BCFD0B018D2658F1
3 changed files with 42 additions and 1 deletions

View file

@ -23,6 +23,10 @@ unicode-width = "~0.1.3"
unicode-segmentation = "~0.1.2"
term_size = { version = "~0.1.0", optional = true }
[target.'cfg(windows)'.dependencies]
kernel32-sys = "~0.2.2"
winapi = "~0.2.8"
[dev-dependencies]
regex = "~0.1.69"

View file

@ -16,7 +16,40 @@ use fmt::{Format, Colorizer};
#[cfg(all(feature = "wrap_help", not(target_os = "windows")))]
use term_size;
#[cfg(any(not(feature = "wrap_help"), target_os = "windows"))]
#[cfg(all(feature = "wrap_help", target_os = "windows"))]
mod term_size {
use kernel32::{GetConsoleScreenBufferInfo, GetStdHandle};
use winapi::{CONSOLE_SCREEN_BUFFER_INFO, COORD, SMALL_RECT, STD_OUTPUT_HANDLE};
pub fn dimensions() -> Option<(usize, usize)> {
let null_coord = COORD{
X: 0,
Y: 0,
};
let null_smallrect = SMALL_RECT{
Left: 0,
Top: 0,
Right: 0,
Bottom: 0,
};
let stdout_h = unsafe { GetStdHandle(STD_OUTPUT_HANDLE) };
let mut console_data = CONSOLE_SCREEN_BUFFER_INFO{
dwSize: null_coord,
dwCursorPosition: null_coord,
wAttributes: 0,
srWindow: null_smallrect,
dwMaximumWindowSize: null_coord,
};
if unsafe { GetConsoleScreenBufferInfo(stdout_h, &mut console_data) } != 0 {
Some(((console_data.srWindow.Right - console_data.srWindow.Left) as usize, (console_data.srWindow.Bottom - console_data.srWindow.Top) as usize))
} else {
None
}
}
}
#[cfg(not(feature = "wrap_help"))]
mod term_size {
pub fn dimensions() -> Option<(usize, usize)> {
None

View file

@ -416,6 +416,10 @@ extern crate vec_map;
#[cfg(feature = "wrap_help")]
extern crate term_size;
extern crate unicode_segmentation;
#[cfg(target_os = "windows")]
extern crate kernel32;
#[cfg(target_os = "windows")]
extern crate winapi;
#[cfg(feature = "yaml")]
pub use yaml_rust::YamlLoader;