1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/main/java/org/vaadin/console/ansi/DefaultANSICodeConverter.java Tue May 31 15:52:03 2011 +0400
1.3 @@ -0,0 +1,107 @@
1.4 +package org.vaadin.console.ansi;
1.5 +
1.6 +import java.util.HashMap;
1.7 +import java.util.Map;
1.8 +import java.util.regex.Matcher;
1.9 +import java.util.regex.Pattern;
1.10 +
1.11 +/**
1.12 + * Default converter for numeric-only ANSI codes.
1.13 + *
1.14 + * @author indvdum
1.15 + * 31.05.2011 13:55:47
1.16 + *
1.17 + */
1.18 +public class DefaultANSICodeConverter implements ANSICodeConverter {
1.19 +
1.20 + protected static final Map<Integer, String> ANSI_CSS = new HashMap<Integer, String>();
1.21 + static{
1.22 + ANSI_CSS.put(0, "");
1.23 + ANSI_CSS.put(1, "term-font-weight-bolder");
1.24 + ANSI_CSS.put(2, "term-font-weight-lighter");
1.25 + ANSI_CSS.put(3, "term-font-style-italic");
1.26 + ANSI_CSS.put(4, "term-text-decoration-underline");
1.27 + ANSI_CSS.put(5, "term-text-decoration-blink");
1.28 + ANSI_CSS.put(6, "term-text-decoration-blink");
1.29 +
1.30 + ANSI_CSS.put(9, "term-text-decoration-line-through");
1.31 +
1.32 + ANSI_CSS.put(22, "term-font-weight-normal");
1.33 +
1.34 + ANSI_CSS.put(24, "term-text-decoration-none");
1.35 + ANSI_CSS.put(25, "term-text-decoration-none");
1.36 +
1.37 + ANSI_CSS.put(29, "term-text-decoration-none");
1.38 +
1.39 + ANSI_CSS.put(30, "term-color-black");
1.40 + ANSI_CSS.put(31, "term-color-red");
1.41 + ANSI_CSS.put(32, "term-color-green");
1.42 + ANSI_CSS.put(33, "term-color-yellow");
1.43 + ANSI_CSS.put(34, "term-color-blue");
1.44 + ANSI_CSS.put(35, "term-color-purple");
1.45 + ANSI_CSS.put(36, "term-color-teal");
1.46 + ANSI_CSS.put(37, "term-color-silver");
1.47 +
1.48 + ANSI_CSS.put(40, "term-background-color-black");
1.49 + ANSI_CSS.put(41, "term-background-color-red");
1.50 + ANSI_CSS.put(42, "term-background-color-green");
1.51 + ANSI_CSS.put(43, "term-background-color-yellow");
1.52 + ANSI_CSS.put(44, "term-background-color-blue");
1.53 + ANSI_CSS.put(45, "term-background-color-purple");
1.54 + ANSI_CSS.put(46, "term-background-color-teal");
1.55 + ANSI_CSS.put(47, "term-background-color-silver");
1.56 +
1.57 + ANSI_CSS.put(53, "term-text-decoration-overline");
1.58 + ANSI_CSS.put(55, "term-text-decoration-none");
1.59 + }
1.60 +
1.61 + @Override
1.62 + public String convertANSIToCSS(String ansiEscapeSequence) {
1.63 + StringBuilder cssClasses = new StringBuilder("");
1.64 + Matcher matcher = Pattern.compile(ANSI_PATTERN).matcher(ansiEscapeSequence);
1.65 + if(!matcher.matches())
1.66 + return cssClasses.toString();
1.67 + String codes[] = matcher.group(2).split(";");
1.68 + for(int i = 0; i < codes.length; i++){
1.69 + if(!codes[i].matches("[0-9]+")) // processing only numeric codes
1.70 + continue;
1.71 + int nCode = Integer.valueOf(codes[i]);
1.72 + if(nCode == 38){ // Set xterm-256 text color, nothing to do
1.73 + i+=2;
1.74 + continue;
1.75 + }
1.76 + if(nCode == 48){ // Set xterm-256 background color, nothing to do
1.77 + i+=2;
1.78 + continue;
1.79 + }
1.80 + String cssClass = getCSSClass(nCode);
1.81 + if(cssClass == null)
1.82 + continue;
1.83 + cssClass = cssClass.trim();
1.84 + if(cssClass.length() > 0)
1.85 + cssClasses.append(cssClass).append(" ");
1.86 + }
1.87 + return cssClasses.toString().trim();
1.88 + }
1.89 +
1.90 + /**
1.91 + * Converting one numeric ANSI code to one or many CSS-classes.
1.92 + *
1.93 + * @param ansiCode ANSI code
1.94 + * @return CSS-class(es).
1.95 + */
1.96 + protected String getCSSClass(int ansiCode) {
1.97 + return ANSI_CSS.get(ansiCode);
1.98 + }
1.99 +
1.100 + /**
1.101 + * Converting one string ANSI code to one or many CSS-classes.
1.102 + *
1.103 + * @param ansiCode ANSI code
1.104 + * @return CSS-class(es).
1.105 + */
1.106 + protected String getCSSClass(String ansiCode) {
1.107 + return "";
1.108 + }
1.109 +
1.110 +}