diff -r f6b22b2ab1b9 -r ea825ba6336b src/main/java/org/vaadin/console/Console.java --- a/src/main/java/org/vaadin/console/Console.java Mon May 23 13:13:51 2011 +0400 +++ b/src/main/java/org/vaadin/console/Console.java Tue May 31 15:52:03 2011 +0400 @@ -12,7 +12,10 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.regex.Pattern; +import org.vaadin.console.ansi.ANSICodeConverter; +import org.vaadin.console.ansi.DefaultANSICodeConverter; import org.vaadin.rpc.ServerSideHandler; import org.vaadin.rpc.ServerSideProxy; import org.vaadin.rpc.client.Method; @@ -33,6 +36,8 @@ private static final long serialVersionUID = 590258219352859644L; private Handler handler; + private ANSICodeConverter ansiToCSSconverter; + private boolean isConvertANSIToCSS = false; private final HashMap commands = new HashMap(); private final Config config = new Config(); @@ -288,6 +293,7 @@ public Console() { setImmediate(true); setHandler(new DefaultConsoleHandler()); + setANSIToCSSConverter(new DefaultANSICodeConverter()); registerCallbacks(); } @@ -522,7 +528,11 @@ } public void print(final String output) { - client.call("print", output); + if(isConvertANSIToCSS){ + client.call("print", ""); + appendWithProcessingANSICodes(output); + } else + client.call("print", output); } /** @@ -601,7 +611,11 @@ } public void println(final String string) { - client.call("println", string); + if(isConvertANSIToCSS){ + client.call("print", ""); + appendWithProcessingANSICodes(string + "\n"); + } else + client.call("println", string); } /** @@ -619,9 +633,38 @@ * @return this Console object */ public Console append(final String string) { - client.call("append", string); + if(isConvertANSIToCSS) + appendWithProcessingANSICodes(string); + else + client.call("append", string); return this; } + + private void appendWithProcessingANSICodes(String sOutput) { + String splitted[] = sOutput.split(ANSICodeConverter.ANSI_PATTERN); + String notPrintedYet = new String(sOutput); + for(int i = 0; i < splitted.length; i++){ + String nextStr = splitted[i]; + if(i == 0 && nextStr.length() == 0) + continue; + String cssClasses = ""; + Pattern firstAnsi = Pattern.compile("^(" + ANSICodeConverter.ANSI_PATTERN + ")+\\Q" + nextStr + "\\E.*", Pattern.DOTALL); + if(firstAnsi.matcher(notPrintedYet).matches()){ + while(firstAnsi.matcher(notPrintedYet).matches()){ + String ansi = notPrintedYet.replaceAll("\\Q" + notPrintedYet.replaceAll("^(" + ANSICodeConverter.ANSI_PATTERN + "){1}", "") + "\\E", ""); + cssClasses += ansiToCSSconverter.convertANSIToCSS(ansi) + " "; + notPrintedYet = notPrintedYet.replaceAll("^(" + ANSICodeConverter.ANSI_PATTERN + "){1}", ""); + } + notPrintedYet = notPrintedYet.replaceAll("^\\Q" + nextStr + "\\E", ""); + } else + notPrintedYet = notPrintedYet.replaceFirst("\\Q" + nextStr + "\\E", ""); + cssClasses = cssClasses.trim(); + if(cssClasses.length() > 0) + client.call("append", nextStr, cssClasses); + else + client.call("append", nextStr); + } + } /** * Append text with predefined in theme CSS class. @@ -810,7 +853,33 @@ this.handler = handler != null ? handler : new DefaultConsoleHandler(); } - /** + public ANSICodeConverter getANSIToCSSConverter() { + return ansiToCSSconverter; + } + + public void setANSIToCSSConverter(ANSICodeConverter converter) { + this.ansiToCSSconverter = converter != null? converter : new DefaultANSICodeConverter(); + } + + /** + * Converting raw output with ANSI escape sequences to output with CSS-classes. + * + * @return + */ + public boolean isConvertANSIToCSS() { + return isConvertANSIToCSS; + } + + /** + * Converting raw output with ANSI escape sequences to output with CSS-classes. + * + * @param isConvertANSIToCSS + */ + public void setConvertANSIToCSS(boolean isConvertANSIToCSS) { + this.isConvertANSIToCSS = isConvertANSIToCSS; + } + + /** * Get map of available commands in this Console. * * @return