|
1 package org.vaadin.console.ansi; |
|
2 |
|
3 import java.util.HashMap; |
|
4 import java.util.Map; |
|
5 import java.util.regex.Matcher; |
|
6 import java.util.regex.Pattern; |
|
7 |
|
8 /** |
|
9 * Default converter for numeric-only ANSI codes. |
|
10 * |
|
11 * @author indvdum |
|
12 * 31.05.2011 13:55:47 |
|
13 * |
|
14 */ |
|
15 public class DefaultANSICodeConverter implements ANSICodeConverter { |
|
16 |
|
17 protected static final Map<Integer, String> ANSI_CSS = new HashMap<Integer, String>(); |
|
18 static{ |
|
19 ANSI_CSS.put(0, ""); |
|
20 ANSI_CSS.put(1, "term-font-weight-bolder"); |
|
21 ANSI_CSS.put(2, "term-font-weight-lighter"); |
|
22 ANSI_CSS.put(3, "term-font-style-italic"); |
|
23 ANSI_CSS.put(4, "term-text-decoration-underline"); |
|
24 ANSI_CSS.put(5, "term-text-decoration-blink"); |
|
25 ANSI_CSS.put(6, "term-text-decoration-blink"); |
|
26 |
|
27 ANSI_CSS.put(9, "term-text-decoration-line-through"); |
|
28 |
|
29 ANSI_CSS.put(22, "term-font-weight-normal"); |
|
30 |
|
31 ANSI_CSS.put(24, "term-text-decoration-none"); |
|
32 ANSI_CSS.put(25, "term-text-decoration-none"); |
|
33 |
|
34 ANSI_CSS.put(29, "term-text-decoration-none"); |
|
35 |
|
36 ANSI_CSS.put(30, "term-color-black"); |
|
37 ANSI_CSS.put(31, "term-color-red"); |
|
38 ANSI_CSS.put(32, "term-color-green"); |
|
39 ANSI_CSS.put(33, "term-color-yellow"); |
|
40 ANSI_CSS.put(34, "term-color-blue"); |
|
41 ANSI_CSS.put(35, "term-color-purple"); |
|
42 ANSI_CSS.put(36, "term-color-teal"); |
|
43 ANSI_CSS.put(37, "term-color-silver"); |
|
44 |
|
45 ANSI_CSS.put(40, "term-background-color-black"); |
|
46 ANSI_CSS.put(41, "term-background-color-red"); |
|
47 ANSI_CSS.put(42, "term-background-color-green"); |
|
48 ANSI_CSS.put(43, "term-background-color-yellow"); |
|
49 ANSI_CSS.put(44, "term-background-color-blue"); |
|
50 ANSI_CSS.put(45, "term-background-color-purple"); |
|
51 ANSI_CSS.put(46, "term-background-color-teal"); |
|
52 ANSI_CSS.put(47, "term-background-color-silver"); |
|
53 |
|
54 ANSI_CSS.put(53, "term-text-decoration-overline"); |
|
55 ANSI_CSS.put(55, "term-text-decoration-none"); |
|
56 } |
|
57 |
|
58 @Override |
|
59 public String convertANSIToCSS(String ansiEscapeSequence) { |
|
60 StringBuilder cssClasses = new StringBuilder(""); |
|
61 Matcher matcher = Pattern.compile(ANSI_PATTERN).matcher(ansiEscapeSequence); |
|
62 if(!matcher.matches()) |
|
63 return cssClasses.toString(); |
|
64 String codes[] = matcher.group(2).split(";"); |
|
65 for(int i = 0; i < codes.length; i++){ |
|
66 if(!codes[i].matches("[0-9]+")) // processing only numeric codes |
|
67 continue; |
|
68 int nCode = Integer.valueOf(codes[i]); |
|
69 if(nCode == 38){ // Set xterm-256 text color, nothing to do |
|
70 i+=2; |
|
71 continue; |
|
72 } |
|
73 if(nCode == 48){ // Set xterm-256 background color, nothing to do |
|
74 i+=2; |
|
75 continue; |
|
76 } |
|
77 String cssClass = getCSSClass(nCode); |
|
78 if(cssClass == null) |
|
79 continue; |
|
80 cssClass = cssClass.trim(); |
|
81 if(cssClass.length() > 0) |
|
82 cssClasses.append(cssClass).append(" "); |
|
83 } |
|
84 return cssClasses.toString().trim(); |
|
85 } |
|
86 |
|
87 /** |
|
88 * Converting one numeric ANSI code to one or many CSS-classes. |
|
89 * |
|
90 * @param ansiCode ANSI code |
|
91 * @return CSS-class(es). |
|
92 */ |
|
93 protected String getCSSClass(int ansiCode) { |
|
94 return ANSI_CSS.get(ansiCode); |
|
95 } |
|
96 |
|
97 /** |
|
98 * Converting one string ANSI code to one or many CSS-classes. |
|
99 * |
|
100 * @param ansiCode ANSI code |
|
101 * @return CSS-class(es). |
|
102 */ |
|
103 protected String getCSSClass(String ansiCode) { |
|
104 return ""; |
|
105 } |
|
106 |
|
107 } |