Fixed setting/getting terminal off-by-one dimensions windows (#242)

This commit is contained in:
Timon 2019-09-21 14:22:12 +02:00 committed by GitHub
parent 5216ecbdec
commit 3b444d077f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 11 deletions

View File

@ -8,6 +8,10 @@ pub fn exit() {
#[cfg(windows)]
pub fn get_terminal_size() -> Result<(u16, u16)> {
let buffer = ScreenBuffer::current()?;
Ok(buffer.info()?.terminal_size().into())
let terminal_size = ScreenBuffer::current()?.info()?.terminal_size();
// windows starts counting at 0, unix at 1, add one to replicated unix behaviour.
Ok((
(terminal_size.width + 1) as u16,
(terminal_size.height + 1) as u16,
))
}

View File

@ -85,8 +85,8 @@ mod tests {
use super::{AnsiTerminal, ITerminal};
/* ======================== ANSI =========================== */
// TODO - Test is disabled, because it's failing on Travis CI
#[test]
// TODO - Test is disabled, because it's failing on Travis CI
#[ignore]
fn test_resize_ansi() {
if try_enable_ansi() {
@ -94,11 +94,12 @@ mod tests {
let (width, height) = terminal.size().unwrap();
terminal.set_size(30, 30).unwrap();
terminal.set_size(35, 35).unwrap();
// see issue: https://github.com/eminence/terminal-size/issues/11
thread::sleep(time::Duration::from_millis(30));
assert_eq!((30, 30), terminal.size().unwrap());
assert_eq!((35, 35), terminal.size().unwrap());
// reset to previous size
terminal.set_size(width, height).unwrap();
// see issue: https://github.com/eminence/terminal-size/issues/11
thread::sleep(time::Duration::from_millis(30));

View File

@ -129,7 +129,7 @@ impl ITerminal for WinApiTerminal {
}
if resize_buffer {
if let Err(_) = screen_buffer.set_size(new_size.width, new_size.height) {
if let Err(_) = screen_buffer.set_size(new_size.width - 1, new_size.height - 1) {
return Err(ErrorKind::ResizingTerminalFailure(String::from(
"Something went wrong when setting screen buffer size.",
)));
@ -138,13 +138,14 @@ impl ITerminal for WinApiTerminal {
let mut window = window.clone();
// Preserve the position, but change the size.
window.bottom = window.top + height;
window.right = window.left + width;
window.bottom = window.top + height - 1;
window.right = window.left + width - 1;
console.set_console_info(true, window)?;
// If we resized the buffer, un-resize it.
if resize_buffer {
if let Err(_) = screen_buffer.set_size(current_size.width, current_size.height) {
if let Err(_) = screen_buffer.set_size(current_size.width - 1, current_size.height - 1)
{
return Err(ErrorKind::ResizingTerminalFailure(String::from(
"Something went wrong when setting screen buffer size.",
)));
@ -280,9 +281,7 @@ fn clear(start_location: Coord, cells_to_write: u32, current_attribute: u16) ->
mod tests {
use super::{ITerminal, WinApiTerminal};
// TODO - Test is ignored, because it returns wrong result (31 != 30)
#[test]
#[ignore]
fn test_resize_winapi() {
let terminal = WinApiTerminal::new();
@ -291,6 +290,7 @@ mod tests {
terminal.set_size(30, 30).unwrap();
assert_eq!((30, 30), terminal.size().unwrap());
// reset to previous size
terminal.set_size(width, height).unwrap();
assert_eq!((width, height), terminal.size().unwrap());
}