From f67c0738671b50c47e31c049959f77bcdc7a6eb6 Mon Sep 17 00:00:00 2001 From: nathan Date: Sun, 29 May 2016 21:42:02 +0200 Subject: [PATCH] Fixed scrolling in windows so that the screen is clipped (as per the specification) --- .../console/output/WindowsAnsiOutputStream.java | 17 ++++++++++------- src/dorkbox/console/util/windows/Kernel32.java | 5 +++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/dorkbox/console/output/WindowsAnsiOutputStream.java b/src/dorkbox/console/output/WindowsAnsiOutputStream.java index 81aeb65..03e2592 100644 --- a/src/dorkbox/console/output/WindowsAnsiOutputStream.java +++ b/src/dorkbox/console/output/WindowsAnsiOutputStream.java @@ -93,6 +93,9 @@ public final class WindowsAnsiOutputStream extends AnsiOutputStream { private volatile short savedX = (short) -1; private volatile short savedY = (short) -1; + // reused vars + private IntByReference written = new IntByReference(); + public WindowsAnsiOutputStream(final OutputStream os, int fileHandle) throws IOException { super(os); @@ -166,7 +169,7 @@ public final class WindowsAnsiOutputStream extends AnsiOutputStream { protected void processEraseScreen(final int eraseOption) throws IOException { getConsoleInfo(); - int[] written = new int[1]; + switch (eraseOption) { case ERASE_ALL: COORD topLeft = new COORD(); @@ -198,7 +201,7 @@ public final class WindowsAnsiOutputStream extends AnsiOutputStream { protected void processEraseLine(final int eraseOption) throws IOException { getConsoleInfo(); - int[] written = new int[1]; + switch (eraseOption) { case ERASE_ALL: COORD currentRow = info.cursorPosition.asValue(); @@ -407,18 +410,18 @@ public final class WindowsAnsiOutputStream extends AnsiOutputStream { SMALL_RECT.ByReference scrollRect = new SMALL_RECT.ByReference(); COORD.ByValue coordDest = new COORD.ByValue(); - // the content that will be scrolled - scrollRect.top = (short) (0); - scrollRect.bottom = Short.MAX_VALUE; + // the content that will be scrolled (just what is visible in the window) + scrollRect.top = (short) (info.cursorPosition.y - info.window.height()); + scrollRect.bottom = (short) (info.cursorPosition.y); scrollRect.left = (short) 0; scrollRect.right = (short) (info.size.x - 1); // The destination for the scroll rectangle is xxx row up/down. coordDest.x = (short) 0; - coordDest.y = (short) (-rowsToScroll); + coordDest.y = (short) (scrollRect.top - rowsToScroll); // fill the space with whatever color was already there with spaces - IntByReference attribs = new IntByReference(); + IntByReference attribs = written; attribs.setValue(info.attributes); // The clipping rectangle is the same as the scrolling rectangle, so we pass NULL diff --git a/src/dorkbox/console/util/windows/Kernel32.java b/src/dorkbox/console/util/windows/Kernel32.java index 789ffc0..fb9040b 100644 --- a/src/dorkbox/console/util/windows/Kernel32.java +++ b/src/dorkbox/console/util/windows/Kernel32.java @@ -113,13 +113,13 @@ public class Kernel32 { * https://msdn.microsoft.com/en-us/library/ms682662%28VS.85%29.aspx */ public static native - int FillConsoleOutputAttribute(HANDLE consoleOutput, short attribute, int length, COORD.ByValue writeCoord, int[] numberOfAttrsWritten); + int FillConsoleOutputAttribute(HANDLE consoleOutput, short attribute, int length, COORD.ByValue writeCoord, IntByReference numberOfAttrsWritten); /** * https://msdn.microsoft.com/en-us/library/ms682663%28VS.85%29.aspx */ public static native - int FillConsoleOutputCharacterW(HANDLE consoleOutput, char character, int length, COORD.ByValue writeCoord, int[] numberOfCharsWritten); + int FillConsoleOutputCharacterW(HANDLE consoleOutput, char character, int length, COORD.ByValue writeCoord, IntByReference numberOfCharsWritten); @@ -135,6 +135,7 @@ public class Kernel32 { public static native int SetConsoleMode(HANDLE handle, int mode); + /** * https://msdn.microsoft.com/en-us/library/ms684961(v=VS.85).aspx */