src/main/java/org/vaadin/console/Console.java
branchconsoleWithANSI
changeset 15 ea825ba6336b
parent 14 f6b22b2ab1b9
     1.1 --- a/src/main/java/org/vaadin/console/Console.java	Mon May 23 13:13:51 2011 +0400
     1.2 +++ b/src/main/java/org/vaadin/console/Console.java	Tue May 31 15:52:03 2011 +0400
     1.3 @@ -12,7 +12,10 @@
     1.4  import java.util.List;
     1.5  import java.util.Map;
     1.6  import java.util.Set;
     1.7 +import java.util.regex.Pattern;
     1.8  
     1.9 +import org.vaadin.console.ansi.ANSICodeConverter;
    1.10 +import org.vaadin.console.ansi.DefaultANSICodeConverter;
    1.11  import org.vaadin.rpc.ServerSideHandler;
    1.12  import org.vaadin.rpc.ServerSideProxy;
    1.13  import org.vaadin.rpc.client.Method;
    1.14 @@ -33,6 +36,8 @@
    1.15  
    1.16      private static final long serialVersionUID = 590258219352859644L;
    1.17      private Handler handler;
    1.18 +    private ANSICodeConverter ansiToCSSconverter;
    1.19 +    private boolean isConvertANSIToCSS = false;
    1.20      private final HashMap<String, Command> commands = new HashMap<String, Command>();
    1.21      private final Config config = new Config();
    1.22  
    1.23 @@ -288,6 +293,7 @@
    1.24      public Console() {
    1.25          setImmediate(true);
    1.26          setHandler(new DefaultConsoleHandler());
    1.27 +        setANSIToCSSConverter(new DefaultANSICodeConverter());
    1.28          registerCallbacks();
    1.29      }
    1.30  
    1.31 @@ -522,7 +528,11 @@
    1.32      }
    1.33  
    1.34      public void print(final String output) {
    1.35 -        client.call("print", output);
    1.36 +    	if(isConvertANSIToCSS){
    1.37 +    		client.call("print", "");
    1.38 +    		appendWithProcessingANSICodes(output);
    1.39 +    	} else
    1.40 +    		client.call("print", output);
    1.41      }
    1.42  
    1.43  	/**
    1.44 @@ -601,7 +611,11 @@
    1.45      }
    1.46  
    1.47      public void println(final String string) {
    1.48 -        client.call("println", string);
    1.49 +    	if(isConvertANSIToCSS){
    1.50 +    		client.call("print", "");
    1.51 +    		appendWithProcessingANSICodes(string + "\n");
    1.52 +    	} else
    1.53 +    		client.call("println", string);
    1.54      }
    1.55  
    1.56  	/**
    1.57 @@ -619,9 +633,38 @@
    1.58       * @return this Console object
    1.59       */
    1.60      public Console append(final String string) {
    1.61 -    	client.call("append", string);
    1.62 +    	if(isConvertANSIToCSS)
    1.63 +    		appendWithProcessingANSICodes(string);
    1.64 +    	else
    1.65 +    		client.call("append", string);
    1.66      	return this;
    1.67      }
    1.68 +    
    1.69 +	private void appendWithProcessingANSICodes(String sOutput) {
    1.70 +		String splitted[] = sOutput.split(ANSICodeConverter.ANSI_PATTERN);
    1.71 +		String notPrintedYet = new String(sOutput);
    1.72 +		for(int i = 0; i < splitted.length; i++){
    1.73 +			String nextStr = splitted[i];
    1.74 +			if(i == 0 && nextStr.length() == 0)
    1.75 +				continue;
    1.76 +			String cssClasses = "";
    1.77 +			Pattern firstAnsi = Pattern.compile("^(" + ANSICodeConverter.ANSI_PATTERN + ")+\\Q" + nextStr + "\\E.*", Pattern.DOTALL);
    1.78 +			if(firstAnsi.matcher(notPrintedYet).matches()){
    1.79 +				while(firstAnsi.matcher(notPrintedYet).matches()){									
    1.80 +					String ansi = notPrintedYet.replaceAll("\\Q" + notPrintedYet.replaceAll("^(" + ANSICodeConverter.ANSI_PATTERN + "){1}", "") + "\\E", "");
    1.81 +					cssClasses += ansiToCSSconverter.convertANSIToCSS(ansi) + " ";
    1.82 +					notPrintedYet = notPrintedYet.replaceAll("^(" + ANSICodeConverter.ANSI_PATTERN + "){1}", "");
    1.83 +				}
    1.84 +				notPrintedYet = notPrintedYet.replaceAll("^\\Q" + nextStr + "\\E", "");
    1.85 +			} else
    1.86 +				notPrintedYet = notPrintedYet.replaceFirst("\\Q" + nextStr + "\\E", "");
    1.87 +			cssClasses = cssClasses.trim();
    1.88 +			if(cssClasses.length() > 0)
    1.89 +				client.call("append", nextStr, cssClasses);
    1.90 +			else
    1.91 +				client.call("append", nextStr);
    1.92 +		}
    1.93 +    }
    1.94  
    1.95  	/**
    1.96  	 * Append text with predefined in theme CSS class.
    1.97 @@ -810,7 +853,33 @@
    1.98          this.handler = handler != null ? handler : new DefaultConsoleHandler();
    1.99      }
   1.100  
   1.101 -    /**
   1.102 +    public ANSICodeConverter getANSIToCSSConverter() {
   1.103 +		return ansiToCSSconverter;
   1.104 +	}
   1.105 +
   1.106 +	public void setANSIToCSSConverter(ANSICodeConverter converter) {
   1.107 +		this.ansiToCSSconverter = converter != null? converter : new DefaultANSICodeConverter();
   1.108 +	}
   1.109 +
   1.110 +	/**
   1.111 +	 * Converting raw output with ANSI escape sequences to output with CSS-classes.
   1.112 +	 *
   1.113 +	 * @return
   1.114 +	 */
   1.115 +	public boolean isConvertANSIToCSS() {
   1.116 +		return isConvertANSIToCSS;
   1.117 +	}
   1.118 +
   1.119 +	/**
   1.120 +	 * Converting raw output with ANSI escape sequences to output with CSS-classes.
   1.121 +	 *
   1.122 +	 * @param isConvertANSIToCSS
   1.123 +	 */
   1.124 +	public void setConvertANSIToCSS(boolean isConvertANSIToCSS) {
   1.125 +		this.isConvertANSIToCSS = isConvertANSIToCSS;
   1.126 +	}
   1.127 +
   1.128 +	/**
   1.129       * Get map of available commands in this Console.
   1.130       *
   1.131       * @return