[Swift-commit] r6832 - in trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi: . tui

hategan at ci.uchicago.edu hategan at ci.uchicago.edu
Mon Aug 12 18:48:18 CDT 2013


Author: hategan
Date: 2013-08-12 18:48:18 -0500 (Mon, 12 Aug 2013)
New Revision: 6832

Modified:
   trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/AbstractANSIDisplay.java
   trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/LocalANSIDisplay.java
   trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/tui/ANSIContext.java
Log:
made detection of escape sequence support in terminal actually work

Modified: trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/AbstractANSIDisplay.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/AbstractANSIDisplay.java	2013-08-12 21:33:18 UTC (rev 6831)
+++ trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/AbstractANSIDisplay.java	2013-08-12 23:48:18 UTC (rev 6832)
@@ -80,6 +80,7 @@
             }));
             Screen screen = new Screen(context);
             if (!screen.init()) {
+                this.cleanup();
                 System.err
                     .println("Your terminal does not support ANSI escape codes");
             }

Modified: trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/LocalANSIDisplay.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/LocalANSIDisplay.java	2013-08-12 21:33:18 UTC (rev 6831)
+++ trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/LocalANSIDisplay.java	2013-08-12 23:48:18 UTC (rev 6832)
@@ -54,10 +54,12 @@
     protected void cleanup() throws IOException {
         m.remove(this);
         super.cleanup();
-        getContext().bgColor(ANSI.BLACK);
-        getContext().fgColor(ANSI.WHITE);
-        getContext().clear();
-        getContext().sync();
+        if (getContext().isInitialized()) {
+            getContext().bgColor(ANSI.BLACK);
+            getContext().fgColor(ANSI.WHITE);
+            getContext().clear();
+            getContext().sync();
+        }
         System.setOut(sout);
         System.setErr(serr);
     }

Modified: trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/tui/ANSIContext.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/tui/ANSIContext.java	2013-08-12 21:33:18 UTC (rev 6831)
+++ trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/tui/ANSIContext.java	2013-08-12 23:48:18 UTC (rev 6832)
@@ -44,7 +44,7 @@
     private ScreenBuffer buf;
     private boolean doubleBuffered;
     private int lock;
-    private boolean done, unicode;
+    private boolean done, unicode, initialized;
 
     private Terminal terminal;
     private boolean redraw;
@@ -151,6 +151,9 @@
         terminal = Terminal.setupTerminal();
         try {
             terminal.initializeTerminal();
+            if (!this.vt100CodesSupported()) {
+                return false;
+            }
             os.write(ANSI.cursorVisible(false));
             os.flush();
             Runtime.getRuntime().addShutdownHook(new Thread() {
@@ -158,13 +161,21 @@
                     exit();
                 }
             });
+            initialized = true;
+            return true;
         }
         catch (Exception e) {
             e.printStackTrace();
+            return false;
         }
-        return terminal.isANSISupported();
     }
     
+    public boolean isInitialized() {
+        return initialized;
+    }
+
+
+
     private boolean querySizeWorks = true, alternate = false;
 
     public int[] querySize() throws IOException {
@@ -208,6 +219,14 @@
             return null;
         }
     }
+    
+    public boolean vt100CodesSupported() throws IOException {
+        os.write(ANSI.AESC + "c");
+        os.flush();
+        String reply = readReply(100);
+        logger.debug("Terminal status code: " + reply);
+        return reply.length() > 0;
+    }
 
     protected void expect(char c) throws IOException {
         if (is.read() != c) {
@@ -220,6 +239,23 @@
             expect(what.charAt(i));
         }
     }
+    
+    protected String readReply(int wait) throws IOException {
+        StringBuilder sb = new StringBuilder();
+        while (wait > 0 && is.available() == 0) {
+            try {
+                Thread.sleep(1);
+                wait--;
+            }
+            catch (InterruptedException e) {
+                throw new IOException("Interrupted");
+            }
+        }
+        while (is.available() > 0) {
+            sb.append((char) is.read());
+        }
+        return sb.toString();
+    }
 
     protected void expect(String what, int wait) throws IOException {
         while (wait > 0 && is.available() == 0) {




More information about the Swift-commit mailing list