felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ccust...@apache.org
Subject svn commit: r822113 - /felix/trunk/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/GrepAction.java
Date Tue, 06 Oct 2009 04:02:55 GMT
Author: ccustine
Date: Tue Oct  6 04:02:55 2009
New Revision: 822113

URL: http://svn.apache.org/viewvc?rev=822113&view=rev
Log:
FELIX-1706 - Highlight grep matches

Modified:
    felix/trunk/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/GrepAction.java

Modified: felix/trunk/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/GrepAction.java
URL: http://svn.apache.org/viewvc/felix/trunk/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/GrepAction.java?rev=822113&r1=822112&r2=822113&view=diff
==============================================================================
--- felix/trunk/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/GrepAction.java
(original)
+++ felix/trunk/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/GrepAction.java
Tue Oct  6 04:02:55 2009
@@ -21,12 +21,14 @@
 import java.io.IOException;
 import java.io.Reader;
 import java.io.InputStreamReader;
+import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import org.apache.felix.karaf.shell.console.OsgiCommandSupport;
 import org.apache.felix.gogo.commands.Argument;
 import org.apache.felix.gogo.commands.Option;
 import org.apache.felix.gogo.commands.Command;
+import org.fusesource.jansi.Ansi;
 
 
 @Command(scope = "shell", name="grep", description="Prints lines matching the given pattern")
@@ -69,16 +71,30 @@
             regexp = regexp.toUpperCase();
         }
         Pattern p = Pattern.compile(regexp);
+        Pattern p2 = Pattern.compile(regex);
         try {
             int lineno = 1;
             String line;
             Reader r = new InputStreamReader(System.in);
             while ((line = readLine(r)) != null) {
-                if (p.matcher(ignoreCase ? line.toUpperCase() : line).matches() ^ invertMatch)
{
+                String pattern = ignoreCase ? line.toUpperCase() : line;
+                if (p.matcher(pattern).matches() ^ invertMatch) {
+
                     if (lineNumber) {
                         System.out.print(String.format("%6d  ", lineno++));
                     }
-                    System.out.println(line);
+
+                    Matcher matcher2 = p2.matcher(pattern);
+                    StringBuffer sb = new StringBuffer();
+                    while (matcher2.find()) {
+                        matcher2.appendReplacement(sb, Ansi.ansi()
+                            .bg(Ansi.Color.YELLOW)
+                            .fg(Ansi.Color.BLACK)
+                            .a(matcher2.group())
+                            .reset().toString());
+                    }
+                    matcher2.appendTail(sb);
+                    System.out.println(sb.toString());
                     lineno++;
                 }
             }



Mime
View raw message