From 65b11a04f10a2438f0d6ba263a78d16367c3aac0 Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 11 Sep 2017 20:10:34 +0000 Subject: [PATCH] console.c: set winsize on Windows * ext/io/console/console.c (console_set_winsize): retry shrinking window and screen buffer. [ruby-core:82741] [Bug #13888] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59844 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/io/console/console.c | 23 ++++++++++++++--------- test/io/console/test_io_console.rb | 5 +++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/ext/io/console/console.c b/ext/io/console/console.c index 6f45537e27..e09213d383 100644 --- a/ext/io/console/console.c +++ b/ext/io/console/console.c @@ -531,6 +531,7 @@ console_set_winsize(VALUE io, VALUE size) #if defined _WIN32 HANDLE wh; int newrow, newcol; + BOOL ret; #endif VALUE row, col, xpixel, ypixel; const VALUE *sz; @@ -568,17 +569,21 @@ console_set_winsize(VALUE io, VALUE size) if (!GetConsoleScreenBufferInfo(wh, &ws)) { rb_syserr_fail(LAST_ERROR, "GetConsoleScreenBufferInfo"); } - if ((ws.dwSize.X < newcol && (ws.dwSize.X = newcol, 1)) || - (ws.dwSize.Y < newrow && (ws.dwSize.Y = newrow, 1))) { - if (!SetConsoleScreenBufferSize(wh, ws.dwSize)) { - rb_syserr_fail(LAST_ERROR, "SetConsoleScreenBufferInfo"); - } - } + ws.dwSize.X = newcol; + ret = SetConsoleScreenBufferSize(wh, ws.dwSize); ws.srWindow.Left = 0; ws.srWindow.Top = 0; - ws.srWindow.Right = newcol; - ws.srWindow.Bottom = newrow; - if (!SetConsoleWindowInfo(wh, FALSE, &ws.srWindow)) { + ws.srWindow.Right = newcol-1; + ws.srWindow.Bottom = newrow-1; + if (!SetConsoleWindowInfo(wh, TRUE, &ws.srWindow)) { + rb_syserr_fail(LAST_ERROR, "SetConsoleWindowInfo"); + } + /* retry when shrinking buffer after shrunk window */ + if (!ret && !SetConsoleScreenBufferSize(wh, ws.dwSize)) { + rb_syserr_fail(LAST_ERROR, "SetConsoleScreenBufferInfo"); + } + /* remove scrollbar if possible */ + if (!SetConsoleWindowInfo(wh, TRUE, &ws.srWindow)) { rb_syserr_fail(LAST_ERROR, "SetConsoleWindowInfo"); } #endif diff --git a/test/io/console/test_io_console.rb b/test/io/console/test_io_console.rb index d65a8e79ca..a3814e739a 100644 --- a/test/io/console/test_io_console.rb +++ b/test/io/console/test_io_console.rb @@ -317,6 +317,11 @@ defined?(IO.console) and TestIO_Console.class_eval do def test_set_winsize_console s = IO.console.winsize assert_nothing_raised(TypeError) {IO.console.winsize = s} + bug = '[ruby-core:82741] [Bug #13888]' + IO.console.winsize = [s[0], s[1]+1] + assert_equal([s[0], s[1]+1], IO.console.winsize, bug) + IO.console.winsize = s + assert_equal(s, IO.console.winsize, bug) end def test_close