You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by gn...@apache.org on 2017/03/21 13:17:26 UTC

svn commit: r1787964 - in /felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline: Builtin.java Highlighter.java Posix.java

Author: gnodet
Date: Tue Mar 21 13:17:25 2017
New Revision: 1787964

URL: http://svn.apache.org/viewvc?rev=1787964&view=rev
Log:
[FELIX-5594][gogo][jline] Improve color support for ls/grep and syntax highlighting

Modified:
    felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Builtin.java
    felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Highlighter.java
    felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Posix.java

Modified: felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Builtin.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Builtin.java?rev=1787964&r1=1787963&r2=1787964&view=diff
==============================================================================
--- felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Builtin.java (original)
+++ felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Builtin.java Tue Mar 21 13:17:25 2017
@@ -722,7 +722,7 @@ public class Builtin {
             type = "";
             suffix = "";
         }
-        String col = Posix.getColorMap(session, "LS").get(type);
+        String col = Posix.getLsColorMap(session).get(type);
         if (col != null && !col.isEmpty()) {
             return "\033[" + col + "m" + path.getFileName().toString() + "\033[m" + suffix;
         } else {

Modified: felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Highlighter.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Highlighter.java?rev=1787964&r1=1787963&r2=1787964&view=diff
==============================================================================
--- felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Highlighter.java (original)
+++ felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Highlighter.java Tue Mar 21 13:17:25 2017
@@ -20,6 +20,7 @@ package org.apache.felix.gogo.jline;
 
 import java.util.Arrays;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import org.apache.felix.gogo.runtime.CommandSessionImpl;
@@ -35,11 +36,12 @@ import org.jline.reader.LineReader.Regio
 import org.jline.reader.impl.DefaultHighlighter;
 import org.jline.utils.AttributedString;
 import org.jline.utils.AttributedStringBuilder;
-import org.jline.utils.AttributedStyle;
 import org.jline.utils.WCWidth;
 
 public class Highlighter extends DefaultHighlighter {
 
+    public static final String DEFAULT_HIGHLIGHTER_COLORS = "rs=35:st=32:nu=32:co=32:va=36:vn=36:fu=94:bf=91:re=90";
+
     private final CommandSession session;
 
     public Highlighter(CommandSession session) {
@@ -63,6 +65,8 @@ public class Highlighter extends Default
                 }
             }
 
+            Map<String, String> colors = Posix.getColorMap(session, "HIGHLIGHTER", DEFAULT_HIGHLIGHTER_COLORS);
+
             int underlineStart = -1;
             int underlineEnd = -1;
             int negativeStart = -1;
@@ -170,32 +174,7 @@ public class Highlighter extends Default
                 }
                 if (types[i] != prevType) {
                     prevType = types[i];
-                    switch (prevType) {
-                        case Reserved:
-                            sb.style(sb.style().foreground(AttributedStyle.MAGENTA));
-                            break;
-                        case String:
-                        case Number:
-                        case Constant:
-                            sb.style(sb.style().foreground(AttributedStyle.GREEN));
-                            break;
-                        case Variable:
-                        case VariableName:
-                            sb.style(sb.style().foreground(AttributedStyle.CYAN));
-                            break;
-                        case Function:
-                            sb.style(sb.style().foreground(AttributedStyle.BLUE + AttributedStyle.BRIGHT));
-                            break;
-                        case BadFunction:
-                            sb.style(sb.style().foreground(AttributedStyle.RED + AttributedStyle.BRIGHT));
-                            break;
-                        case Repair:
-                            sb.style(sb.style().foreground(AttributedStyle.BLACK + AttributedStyle.BRIGHT));
-                            break;
-                        default:
-                            sb.style(sb.style().foregroundDefault());
-                            break;
-                    }
+                    applyStyle(sb, colors, prevType);
                 }
                 char c = repaired.charAt(i);
                 if (c == '\t' || c == '\n') {
@@ -225,18 +204,32 @@ public class Highlighter extends Default
         }
     }
 
+    private void applyStyle(AttributedStringBuilder sb, Map<String, String> colors, Type type) {
+        String col = colors.get(type.color);
+        if (col != null && !col.isEmpty()) {
+            sb.appendAnsi("\033[" + col + "m");
+        } else {
+            sb.style(sb.style().foregroundDefault());
+        }
+    }
+
     enum Type {
-        Reserved,
-        String,
-        Number,
-        Variable,
-        VariableName,
-        Function,
-        BadFunction,
-        Value,
-        Constant,
-        Unknown,
-        Repair
+        Reserved("rs"),
+        String("st"),
+        Number("nu"),
+        Variable("va"),
+        VariableName("vn"),
+        Function("fu"),
+        BadFunction("bf"),
+        Constant("co"),
+        Unknown("un"),
+        Repair("re");
+
+        private final String color;
+
+        Type(String color) {
+            this.color = color;
+        }
     }
 
 }

Modified: felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Posix.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Posix.java?rev=1787964&r1=1787963&r2=1787964&view=diff
==============================================================================
--- felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Posix.java (original)
+++ felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Posix.java Tue Mar 21 13:17:25 2017
@@ -1006,6 +1006,7 @@ public class Posix {
                 "  -? --help                show help",
                 "  -1                       list one entry per line",
                 "  -C                       multi-column output",
+                "     --color=WHEN          colorize the output, may be `always', `never' or `auto'",
                 "  -a                       list entries starting with .",
                 "  -F                       append file type indicators",
                 "  -m                       comma separated",
@@ -1019,7 +1020,28 @@ public class Posix {
                 "  -h                       print sizes in human readable form"
         };
         Options opt = parseOptions(session, usage, argv);
-        Map<String, String> colors = getColorMap(session, "LS");
+        String color = opt.isSet("color") ? opt.get("color") : "auto";
+        boolean colored;
+        switch (color) {
+            case "always":
+            case "yes":
+            case "force":
+                colored = true;
+                break;
+            case "never":
+            case "no":
+            case "none":
+                colored = false;
+                break;
+            case "auto":
+            case "tty":
+            case "if-tty":
+                colored = process.isTty(1);
+                break;
+            default:
+                throw new IllegalArgumentException("invalid argument ‘" + color + "’ for ‘--color’");
+        }
+        Map<String, String> colors = colored ? getLsColorMap(session) : Collections.emptyMap();
 
         class PathEntry implements Comparable<PathEntry> {
             final Path abs;
@@ -1096,7 +1118,7 @@ public class Posix {
                 }
                 String col = colors.get(type);
                 boolean addSuffix = opt.isSet("F");
-                if (col != null && !col.isEmpty() && process.isTty(1)) { // TODO: ability to force colors if piped
+                if (col != null && !col.isEmpty()) {
                     return "\033[" + col + "m" + path.toString() + "\033[m" + (addSuffix ? suffix : "") + link;
                 } else {
                     return path.toString() + (addSuffix ? suffix : "") + link;
@@ -1489,6 +1511,26 @@ public class Posix {
         boolean lineNumber = opt.isSet("line-number");
         boolean count = opt.isSet("count");
         String color = opt.isSet("color") ? opt.get("color") : "auto";
+        boolean colored;
+        switch (color) {
+            case "always":
+            case "yes":
+            case "force":
+                colored = true;
+                break;
+            case "never":
+            case "no":
+            case "none":
+                colored = false;
+                break;
+            case "auto":
+            case "tty":
+            case "if-tty":
+                colored = process.isTty(1);
+                break;
+            default:
+                throw new IllegalArgumentException("invalid argument ‘" + color + "’ for ‘--color’");
+        }
 
         List<Source> sources = new ArrayList<>();
         if (opt.args().isEmpty()) {
@@ -1515,7 +1557,9 @@ public class Posix {
                     }
                     if (p.matcher(line).matches() ^ invertMatch) {
                         AttributedStringBuilder sbl = new AttributedStringBuilder();
-                        sbl.style(AttributedStyle.DEFAULT.foreground(AttributedStyle.BLACK + AttributedStyle.BRIGHT));
+                        if (colored) {
+                            sbl.style(AttributedStyle.DEFAULT.foreground(AttributedStyle.BLACK + AttributedStyle.BRIGHT));
+                        }
                         if (!count && sources.size() > 1) {
                             sbl.append(source.getName());
                             sbl.append(":");
@@ -1523,11 +1567,13 @@ public class Posix {
                         if (!count && lineNumber) {
                             sbl.append(String.format("%6d  ", lineno));
                         }
-                        sbl.style(AttributedStyle.DEFAULT);
+                        if (colored) {
+                            sbl.style(AttributedStyle.DEFAULT);
+                        }
                         Matcher matcher2 = p2.matcher(line);
                         AttributedString aLine = AttributedString.fromAnsi(line);
                         AttributedStyle style = AttributedStyle.DEFAULT;
-                        if (!invertMatch && !color.equalsIgnoreCase("never")) {
+                        if (!invertMatch && colored) {
                             style = style.bold().foreground(AttributedStyle.RED);
                         }
                         int cur = 0;
@@ -1933,15 +1979,15 @@ public class Posix {
         return perms;
     }
 
-    public static Map<String, String> getColorMap(CommandSession session, String name) {
+    public static Map<String, String> getLsColorMap(CommandSession session) {
+        return getColorMap(session, "LS", DEFAULT_LS_COLORS);
+    }
+
+    public static Map<String, String> getColorMap(CommandSession session, String name, String def) {
         Object obj = session.get(name + "_COLORS");
         String str = obj != null ? obj.toString() : null;
         if (str == null || !str.matches("[a-z]{2}=[0-9]+(;[0-9]+)*(:[a-z]{2}=[0-9]+(;[0-9]+)*)*")) {
-            if ("LS".equals(name)) {
-                str = DEFAULT_LS_COLORS;
-            } else {
-                str = "";
-            }
+            str = def;
         }
         return Arrays.stream(str.split(":"))
                 .collect(Collectors.toMap(s -> s.substring(0, s.indexOf('=')),