10 import java.util.HashMap; |
10 import java.util.HashMap; |
11 import java.util.HashSet; |
11 import java.util.HashSet; |
12 import java.util.List; |
12 import java.util.List; |
13 import java.util.Map; |
13 import java.util.Map; |
14 import java.util.Set; |
14 import java.util.Set; |
15 |
15 import java.util.regex.Pattern; |
|
16 |
|
17 import org.vaadin.console.ansi.ANSICodeConverter; |
|
18 import org.vaadin.console.ansi.DefaultANSICodeConverter; |
16 import org.vaadin.rpc.ServerSideHandler; |
19 import org.vaadin.rpc.ServerSideHandler; |
17 import org.vaadin.rpc.ServerSideProxy; |
20 import org.vaadin.rpc.ServerSideProxy; |
18 import org.vaadin.rpc.client.Method; |
21 import org.vaadin.rpc.client.Method; |
19 |
22 |
20 import com.vaadin.terminal.PaintException; |
23 import com.vaadin.terminal.PaintException; |
31 @com.vaadin.ui.ClientWidget(org.vaadin.console.client.ui.VTextConsole.class) |
34 @com.vaadin.ui.ClientWidget(org.vaadin.console.client.ui.VTextConsole.class) |
32 public class Console extends AbstractComponent implements Component.Focusable { |
35 public class Console extends AbstractComponent implements Component.Focusable { |
33 |
36 |
34 private static final long serialVersionUID = 590258219352859644L; |
37 private static final long serialVersionUID = 590258219352859644L; |
35 private Handler handler; |
38 private Handler handler; |
|
39 private ANSICodeConverter ansiToCSSconverter; |
|
40 private boolean isConvertANSIToCSS = false; |
36 private final HashMap<String, Command> commands = new HashMap<String, Command>(); |
41 private final HashMap<String, Command> commands = new HashMap<String, Command>(); |
37 private final Config config = new Config(); |
42 private final Config config = new Config(); |
38 |
43 |
39 private static final String DEFAULT_PS = "}> "; |
44 private static final String DEFAULT_PS = "}> "; |
40 private static final String DEFAULT_GREETING = "Console ready."; |
45 private static final String DEFAULT_GREETING = "Console ready."; |
520 } |
526 } |
521 return count; |
527 return count; |
522 } |
528 } |
523 |
529 |
524 public void print(final String output) { |
530 public void print(final String output) { |
525 client.call("print", output); |
531 if(isConvertANSIToCSS){ |
|
532 client.call("print", ""); |
|
533 appendWithProcessingANSICodes(output); |
|
534 } else |
|
535 client.call("print", output); |
526 } |
536 } |
527 |
537 |
528 /** |
538 /** |
529 * Print text with predefined in theme CSS class. |
539 * Print text with predefined in theme CSS class. |
530 * |
540 * |
599 public void prompt(final String initialInput) { |
609 public void prompt(final String initialInput) { |
600 client.call("prompt", initialInput); |
610 client.call("prompt", initialInput); |
601 } |
611 } |
602 |
612 |
603 public void println(final String string) { |
613 public void println(final String string) { |
604 client.call("println", string); |
614 if(isConvertANSIToCSS){ |
|
615 client.call("print", ""); |
|
616 appendWithProcessingANSICodes(string + "\n"); |
|
617 } else |
|
618 client.call("println", string); |
605 } |
619 } |
606 |
620 |
607 /** |
621 /** |
608 * Print text with predefined in theme CSS class. |
622 * Print text with predefined in theme CSS class. |
609 * |
623 * |
617 /** |
631 /** |
618 * @param string text to append to the last printed line |
632 * @param string text to append to the last printed line |
619 * @return this Console object |
633 * @return this Console object |
620 */ |
634 */ |
621 public Console append(final String string) { |
635 public Console append(final String string) { |
622 client.call("append", string); |
636 if(isConvertANSIToCSS) |
|
637 appendWithProcessingANSICodes(string); |
|
638 else |
|
639 client.call("append", string); |
623 return this; |
640 return this; |
|
641 } |
|
642 |
|
643 private void appendWithProcessingANSICodes(String sOutput) { |
|
644 String splitted[] = sOutput.split(ANSICodeConverter.ANSI_PATTERN); |
|
645 String notPrintedYet = new String(sOutput); |
|
646 for(int i = 0; i < splitted.length; i++){ |
|
647 String nextStr = splitted[i]; |
|
648 if(i == 0 && nextStr.length() == 0) |
|
649 continue; |
|
650 String cssClasses = ""; |
|
651 Pattern firstAnsi = Pattern.compile("^(" + ANSICodeConverter.ANSI_PATTERN + ")+\\Q" + nextStr + "\\E.*", Pattern.DOTALL); |
|
652 if(firstAnsi.matcher(notPrintedYet).matches()){ |
|
653 while(firstAnsi.matcher(notPrintedYet).matches()){ |
|
654 String ansi = notPrintedYet.replaceAll("\\Q" + notPrintedYet.replaceAll("^(" + ANSICodeConverter.ANSI_PATTERN + "){1}", "") + "\\E", ""); |
|
655 cssClasses += ansiToCSSconverter.convertANSIToCSS(ansi) + " "; |
|
656 notPrintedYet = notPrintedYet.replaceAll("^(" + ANSICodeConverter.ANSI_PATTERN + "){1}", ""); |
|
657 } |
|
658 notPrintedYet = notPrintedYet.replaceAll("^\\Q" + nextStr + "\\E", ""); |
|
659 } else |
|
660 notPrintedYet = notPrintedYet.replaceFirst("\\Q" + nextStr + "\\E", ""); |
|
661 cssClasses = cssClasses.trim(); |
|
662 if(cssClasses.length() > 0) |
|
663 client.call("append", nextStr, cssClasses); |
|
664 else |
|
665 client.call("append", nextStr); |
|
666 } |
624 } |
667 } |
625 |
668 |
626 /** |
669 /** |
627 * Append text with predefined in theme CSS class. |
670 * Append text with predefined in theme CSS class. |
628 * |
671 * |
808 */ |
851 */ |
809 public void setHandler(final Handler handler) { |
852 public void setHandler(final Handler handler) { |
810 this.handler = handler != null ? handler : new DefaultConsoleHandler(); |
853 this.handler = handler != null ? handler : new DefaultConsoleHandler(); |
811 } |
854 } |
812 |
855 |
813 /** |
856 public ANSICodeConverter getANSIToCSSConverter() { |
|
857 return ansiToCSSconverter; |
|
858 } |
|
859 |
|
860 public void setANSIToCSSConverter(ANSICodeConverter converter) { |
|
861 this.ansiToCSSconverter = converter != null? converter : new DefaultANSICodeConverter(); |
|
862 } |
|
863 |
|
864 /** |
|
865 * Converting raw output with ANSI escape sequences to output with CSS-classes. |
|
866 * |
|
867 * @return |
|
868 */ |
|
869 public boolean isConvertANSIToCSS() { |
|
870 return isConvertANSIToCSS; |
|
871 } |
|
872 |
|
873 /** |
|
874 * Converting raw output with ANSI escape sequences to output with CSS-classes. |
|
875 * |
|
876 * @param isConvertANSIToCSS |
|
877 */ |
|
878 public void setConvertANSIToCSS(boolean isConvertANSIToCSS) { |
|
879 this.isConvertANSIToCSS = isConvertANSIToCSS; |
|
880 } |
|
881 |
|
882 /** |
814 * Get map of available commands in this Console. |
883 * Get map of available commands in this Console. |
815 * |
884 * |
816 * @return |
885 * @return |
817 */ |
886 */ |
818 public Set<String> getCommands() { |
887 public Set<String> getCommands() { |