From 1340425797564bc2bfa9c534e135d75547bdf206 Mon Sep 17 00:00:00 2001 From: Marco Maccaferri Date: Thu, 29 Oct 2020 19:50:09 +0100 Subject: [PATCH] Fixed cursor position and report sequences --- .../com/maccasoft/tools/TerminalTest.java | 34 ++++++++++++++++++- src/com/maccasoft/tools/Terminal.java | 8 ++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src-tests/com/maccasoft/tools/TerminalTest.java b/src-tests/com/maccasoft/tools/TerminalTest.java index a39be7e..30558d6 100644 --- a/src-tests/com/maccasoft/tools/TerminalTest.java +++ b/src-tests/com/maccasoft/tools/TerminalTest.java @@ -10,17 +10,30 @@ package com.maccasoft.tools; +import java.io.ByteArrayOutputStream; + import org.eclipse.swt.widgets.Shell; public class TerminalTest extends DatabindingTestCase { Shell shell; Terminal term; + ByteArrayOutputStream out; @Override protected void setUp() throws Exception { shell = createShell(); - term = new Terminal(shell); + + out = new ByteArrayOutputStream(); + + term = new Terminal(shell) { + + @Override + protected void writeByte(byte b) { + out.write(b); + } + + }; } @Override @@ -206,4 +219,23 @@ public class TerminalTest extends DatabindingTestCase { assertEquals(Terminal.CURSOR_ON | Terminal.CURSOR_FLASH | Terminal.CURSOR_ULINE, term.cursor); } + + public void testCursorReport() throws Exception { + out = new ByteArrayOutputStream(); + term.print("\033[1;1f\033[6n"); + assertEquals("\033[1;1R", out.toString()); + + out = new ByteArrayOutputStream(); + term.print("\033[10;1f\033[6n"); + assertEquals("\033[10;1R", out.toString()); + + out = new ByteArrayOutputStream(); + term.print("\033[1;10f\033[6n"); + assertEquals("\033[1;10R", out.toString()); + } + + public void testCursorPositionBounds() throws Exception { + term.print("\033[100;100f\033[6n"); + assertEquals("\033[25;80R", out.toString()); + } } diff --git a/src/com/maccasoft/tools/Terminal.java b/src/com/maccasoft/tools/Terminal.java index d2e3073..cf3dbfb 100644 --- a/src/com/maccasoft/tools/Terminal.java +++ b/src/com/maccasoft/tools/Terminal.java @@ -559,7 +559,13 @@ public class Terminal { } else if (argc >= 2) { cy = (args[0] > 0 ? (args[0] - 1) : 0) * font.getHeight(); + if (cy > (bounds.height - font.getHeight())) { + cy = (bounds.height / font.getHeight() - 1) * font.getHeight(); + } cx = (args[1] > 0 ? (args[1] - 1) : 0) * font.getWidth(); + if (cx > (bounds.width - font.getWidth())) { + cx = (bounds.width / font.getWidth() - 1) * font.getWidth(); + } } break; case 'J': { @@ -649,7 +655,7 @@ public class Terminal { writeByte((byte) 'n'); } else if (args[0] == 6) { - byte[] b = String.format("%c[%d;%dR", 0x1B, cy, cx).getBytes(); + byte[] b = String.format("%c[%d;%dR", 0x1B, (cy / font.getHeight()) + 1, (cx / font.getWidth()) + 1).getBytes(); for (int i = 0; i < b.length; i++) { writeByte(b[i]); }