Support TTerminalWindow resizing via ptypipe
[fanfix.git] / src / jexer / tterminal / ECMA48.java
index b3cb0ac556aded9743240714e087d738988d638c..5a30d37454af62d5f4c519e31e47924b2dd31593 100644 (file)
@@ -3,7 +3,7 @@
  *
  * The MIT License (MIT)
  *
- * Copyright (C) 2016 Kevin Lamonte
+ * Copyright (C) 2017 Kevin Lamonte
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -28,6 +28,7 @@
  */
 package jexer.tterminal;
 
+import java.io.BufferedOutputStream;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.IOException;
@@ -51,7 +52,7 @@ import jexer.io.TimeoutInputStream;
 import static jexer.TKeypress.*;
 
 /**
- * This implements a complex ANSI ECMA-48/ISO 6429/ANSI X3.64 type consoles,
+ * This implements a complex ECMA-48/ISO 6429/ANSI X3.64 type console,
  * including a scrollback buffer.
  *
  * <p>
@@ -203,7 +204,7 @@ public class ECMA48 implements Runnable {
      *
      * @param str string to send
      */
-    private void writeRemote(final String str) {
+    public void writeRemote(final String str) {
         if (stopReaderThread) {
             // Reader hit EOF, bail out now.
             close();
@@ -220,6 +221,7 @@ public class ECMA48 implements Runnable {
                 return;
             }
             try {
+                outputStream.flush();
                 for (int i = 0; i < str.length(); i++) {
                     outputStream.write(str.charAt(i));
                 }
@@ -234,6 +236,7 @@ public class ECMA48 implements Runnable {
                 return;
             }
             try {
+                output.flush();
                 output.write(str);
                 output.flush();
             } catch (IOException e) {
@@ -301,6 +304,21 @@ public class ECMA48 implements Runnable {
         }
     }
 
+    /**
+     * The enclosing listening object.
+     */
+    private DisplayListener listener;
+
+    /**
+     * Set a listening object.
+     *
+     * @param listener the object that will have displayChanged() called
+     * after bytes are received from the remote terminal
+     */
+    public void setListener(final DisplayListener listener) {
+        this.listener = listener;
+    }
+
     /**
      * When true, the reader thread is expected to exit.
      */
@@ -522,6 +540,22 @@ public class ECMA48 implements Runnable {
         return width;
     }
 
+    /**
+     * Set the display width.
+     *
+     * @param width the new width
+     */
+    public final void setWidth(final int width) {
+        this.width = width;
+        rightMargin = width - 1;
+        if (currentState.cursorX >= width) {
+            currentState.cursorX = width - 1;
+        }
+        if (savedState.cursorX >= width) {
+            savedState.cursorX = width - 1;
+        }
+    }
+
     /**
      * Physical display height.  We start at 80x24, but the user can resize
      * us bigger/smaller.
@@ -537,6 +571,33 @@ public class ECMA48 implements Runnable {
         return height;
     }
 
+    /**
+     * Set the display height.
+     *
+     * @param height the new height
+     */
+    public final void setHeight(final int height) {
+        this.height = height;
+        scrollRegionBottom = height - 1;
+        if (scrollRegionTop >= scrollRegionBottom) {
+            scrollRegionTop = 0;
+        }
+        if (currentState.cursorY >= height) {
+            currentState.cursorY = height - 1;
+        }
+        if (savedState.cursorY >= height) {
+            savedState.cursorY = height - 1;
+        }
+        while (display.size() < height) {
+            DisplayLine line = new DisplayLine(currentState.attr);
+            line.setReverseColor(reverseVideo);
+            display.add(line);
+        }
+        while (display.size() > height) {
+            scrollback.add(display.remove(0));
+        }
+    }
+
     /**
      * Top margin of the scrolling region.
      */
@@ -905,11 +966,12 @@ public class ECMA48 implements Runnable {
         }
         if (type == DeviceType.XTERM) {
             this.input    = new InputStreamReader(this.inputStream, "UTF-8");
-            this.output   = new OutputStreamWriter(outputStream, "UTF-8");
+            this.output   = new OutputStreamWriter(new
+                BufferedOutputStream(outputStream), "UTF-8");
             this.outputStream = null;
         } else {
             this.output       = null;
-            this.outputStream = outputStream;
+            this.outputStream = new BufferedOutputStream(outputStream);
         }
 
         reset();
@@ -1796,20 +1858,9 @@ public class ECMA48 implements Runnable {
         if (keypress.equalsWithoutModifiers(kbPgUp)) {
             switch (type) {
             case XTERM:
-                switch (arrowKeyMode) {
-                case ANSI:
-                    return xtermBuildKeySequence("\033[", '5', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                case VT52:
-                    return xtermBuildKeySequence("\033", '5', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                case VT100:
-                    return xtermBuildKeySequence("\033O", '5', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                }
+                return xtermBuildKeySequence("\033[", '5', '~',
+                    keypress.isCtrl(), keypress.isAlt(),
+                    keypress.isShift());
             default:
                 return "\033[5~";
             }
@@ -1818,20 +1869,9 @@ public class ECMA48 implements Runnable {
         if (keypress.equalsWithoutModifiers(kbPgDn)) {
             switch (type) {
             case XTERM:
-                switch (arrowKeyMode) {
-                case ANSI:
-                    return xtermBuildKeySequence("\033[", '6', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                case VT52:
-                    return xtermBuildKeySequence("\033", '6', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                case VT100:
-                    return xtermBuildKeySequence("\033O", '6', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                }
+                return xtermBuildKeySequence("\033[", '6', '~',
+                    keypress.isCtrl(), keypress.isAlt(),
+                    keypress.isShift());
             default:
                 return "\033[6~";
             }
@@ -1840,20 +1880,9 @@ public class ECMA48 implements Runnable {
         if (keypress.equalsWithoutModifiers(kbIns)) {
             switch (type) {
             case XTERM:
-                switch (arrowKeyMode) {
-                case ANSI:
-                    return xtermBuildKeySequence("\033[", '2', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                case VT52:
-                    return xtermBuildKeySequence("\033", '2', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                case VT100:
-                    return xtermBuildKeySequence("\033O", '2', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                }
+                return xtermBuildKeySequence("\033[", '2', '~',
+                    keypress.isCtrl(), keypress.isAlt(),
+                    keypress.isShift());
             default:
                 return "\033[2~";
             }
@@ -1862,20 +1891,9 @@ public class ECMA48 implements Runnable {
         if (keypress.equalsWithoutModifiers(kbDel)) {
             switch (type) {
             case XTERM:
-                switch (arrowKeyMode) {
-                case ANSI:
-                    return xtermBuildKeySequence("\033[", '3', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                case VT52:
-                    return xtermBuildKeySequence("\033", '3', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                case VT100:
-                    return xtermBuildKeySequence("\033O", '3', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                }
+                return xtermBuildKeySequence("\033[", '3', '~',
+                    keypress.isCtrl(), keypress.isAlt(),
+                    keypress.isShift());
             default:
                 // Delete sends real delete for VTxxx
                 return "\177";
@@ -6066,6 +6084,10 @@ public class ECMA48 implements Runnable {
                             consume((char)ch);
                         }
                     }
+                    // Permit my enclosing UI to know that I updated.
+                    if (listener != null) {
+                        listener.displayChanged();
+                    }
                 }
                 // System.err.println("end while loop"); System.err.flush();
             } catch (IOException e) {