Jexer image protocol performance
authorKevin Lamonte <kevin.lamonte@gmail.com>
Sun, 3 Nov 2019 00:58:10 +0000 (19:58 -0500)
committerKevin Lamonte <kevin.lamonte@gmail.com>
Sun, 3 Nov 2019 00:58:10 +0000 (19:58 -0500)
examples/imgls [new file with mode: 0755]
src/jexer/tterminal/ECMA48.java

diff --git a/examples/imgls b/examples/imgls
new file mode 100755 (executable)
index 0000000..99bbb9c
--- /dev/null
@@ -0,0 +1,64 @@
+#!/bin/bash
+
+# This is a modified version of the 'imgls' from iTerm2 located at
+# https://iterm2.com/utilities/imgls, modified to emit images with the
+# Jexer image protocol.
+
+# tmux requires unrecognized OSC sequences to be wrapped with DCS tmux;
+# <sequence> ST, and for all ESCs in <sequence> to be replaced with ESC ESC. It
+# only accepts ESC backslash for ST.
+function print_osc() {
+    if [ x"$TERM" = "xscreen" ] ; then
+        printf "\033Ptmux;\033\033]"
+    else
+        printf "\033]"
+    fi
+}
+
+function check_dependency() {
+  if ! (builtin command -V "$1" > /dev/null 2>& 1); then
+    echo "imgcat: missing dependency: can't find $1" 1>& 2
+    exit 1
+  fi
+}
+
+# More of the tmux workaround described above.
+function print_st() {
+    if [ x"$TERM" = "xscreen" ] ; then
+        printf "\a\033\\"
+    else
+        printf "\a"
+    fi
+}
+
+function list_file() {
+  fn=$1
+  test -f "$fn" || return 0
+
+  if [ "${fn: -4}" == ".png" ]; then
+      print_osc
+      printf '444;1;1;'
+      base64 < "$fn"
+      print_st
+  elif [ "${fn: -4}" == ".jpg" ]; then
+      print_osc
+      printf '444;2;1;'
+      base64 < "$fn"
+      print_st
+  fi
+}
+
+check_dependency base64
+
+if [ $# -eq 0 ]; then
+  for fn in *
+  do
+     list_file "$fn"
+  done < <(ls -ls)
+else
+  for fn in "$@"
+  do
+     list_file "$fn"
+  done
+fi
+
index 8127458dc18e1ab8e319e88a43e786429005aaac..12e7a2bc3638b22d2aa19651661a2e899d167afb 100644 (file)
@@ -4778,13 +4778,22 @@ public class ECMA48 implements Runnable {
     private void oscPut(final char xtermChar) {
         // System.err.println("oscPut: " + xtermChar);
 
+        boolean oscEnd = false;
+
+        if (xtermChar == 0x07) {
+            oscEnd = true;
+        }
+        if ((xtermChar == '\\')
+            && (collectBuffer.charAt(collectBuffer.length() - 1) == '\033')
+        ) {
+            oscEnd = true;
+        }
+
         // Collect first
         collectBuffer.append(xtermChar);
 
         // Xterm cases...
-        if ((xtermChar == 0x07)
-            || (collectBuffer.toString().endsWith("\033\\"))
-        ) {
+        if (oscEnd) {
             String args = null;
             if (xtermChar == 0x07) {
                 args = collectBuffer.substring(0, collectBuffer.length() - 1);
@@ -4867,11 +4876,19 @@ public class ECMA48 implements Runnable {
     private void pmPut(final char pmChar) {
         // System.err.println("pmPut: " + pmChar);
 
+        boolean pmEnd = false;
+
+        if ((pmChar == '\\')
+            && (collectBuffer.charAt(collectBuffer.length() - 1) == '\033')
+        ) {
+            pmEnd = true;
+        }
+
         // Collect first
         collectBuffer.append(pmChar);
 
         // Xterm cases...
-        if (collectBuffer.toString().endsWith("\033\\")) {
+        if (pmEnd) {
             String arg = null;
             arg = collectBuffer.substring(0, collectBuffer.length() - 2);