4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
7 * Yoonki Park <yoonki.park@samsung.com>
8 * Kangho Kim <kh5325.kim@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
26 package org.tizen.common.ui.view.console;
28 import static org.tizen.common.util.SWTUtil.getDisplay;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
35 import org.eclipse.swt.graphics.Color;
36 import org.tizen.common.util.MapUtil;
38 public class AnsicodeAdapter {
39 public static final char ESCAPE = '\033'; // ANSI Escape Character that starts commands
41 public static final Color BLACK = new Color(getDisplay(), 0, 0, 0); //Display.getCurrent().getSystemColor(SWT.COLOR_BLACK);
42 public static final Color RED = new Color(getDisplay(), 255, 0, 0); //Display.getCurrent().getSystemColor(SWT.COLOR_RED);
43 public static final Color GREEN = new Color(getDisplay(), 0, 255, 0); //Display.getCurrent().getSystemColor(SWT.COLOR_GREEN);
44 public static final Color YELLOW = new Color(getDisplay(), 255, 255, 0); //Display.getCurrent().getSystemColor(SWT.COLOR_YELLOW);
45 public static final Color BLUE = new Color(getDisplay(), 0,0,255); //Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);
46 public static final Color MAGENTA = new Color(getDisplay(), 255, 0, 255); //Display.getCurrent().getSystemColor(SWT.COLOR_MAGENTA);
47 public static final Color CYAN = new Color(getDisplay(), 0,255, 255); //Display.getCurrent().getSystemColor(SWT.COLOR_CYAN);
48 public static final Color WHITE = new Color(getDisplay(), 255, 255, 255); //Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
50 public static final Color INTENSE_BLACK = new Color(getDisplay(), 0, 0, 0); //Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GRAY);
51 public static final Color INTENSE_RED = new Color(getDisplay(), 139, 0, 0); //Display.getCurrent().getSystemColor(SWT.COLOR_DARK_RED);
52 public static final Color INTENSE_GREEN = new Color(getDisplay(), 0, 100, 0);//Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GREEN);
53 public static final Color INTENSE_YELLOW = new Color(getDisplay(), 250,250,210);//Display.getCurrent().getSystemColor(SWT.COLOR_DARK_YELLOW);
54 public static final Color INTENSE_BLUE = new Color(getDisplay(), 0,0,139);//Display.getCurrent().getSystemColor(SWT.COLOR_DARK_BLUE);
55 public static final Color INTENSE_MAGENTA = new Color(getDisplay(), 139,0,139);//Display.getCurrent().getSystemColor(SWT.COLOR_DARK_MAGENTA);
56 public static final Color INTENSE_CYAN = new Color(getDisplay(), 0, 139, 139);//Display.getCurrent().getSystemColor(SWT.COLOR_DARK_CYAN);
57 public static final Color INTENSE_WHITE = new Color(getDisplay(), 245, 245, 245);//Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
59 public static final Map<Integer, Color[]> CODE2COLOR = Collections.unmodifiableMap( MapUtil.<Integer, Color[]>asMap( new Object[][] {
60 new Object[] { 30, new Color[] { INTENSE_BLACK, BLACK } },
61 new Object[] { 31, new Color[] { INTENSE_RED, RED } },
62 new Object[] { 32, new Color[] { INTENSE_GREEN, GREEN } },
63 new Object[] { 33, new Color[] { INTENSE_YELLOW, YELLOW } },
64 new Object[] { 34, new Color[] { INTENSE_BLUE, BLUE } },
65 new Object[] { 35, new Color[] { INTENSE_MAGENTA, MAGENTA } },
66 new Object[] { 36, new Color[] { INTENSE_CYAN, CYAN } },
67 new Object[] { 37, new Color[] { INTENSE_WHITE, WHITE } },
68 new Object[] { 40, new Color[] { INTENSE_BLACK, BLACK } },
69 new Object[] { 41, new Color[] { INTENSE_RED, RED } },
70 new Object[] { 42, new Color[] { INTENSE_GREEN, GREEN } },
71 new Object[] { 43, new Color[] { INTENSE_YELLOW, YELLOW } },
72 new Object[] { 44, new Color[] { INTENSE_BLUE, BLUE } },
73 new Object[] { 45, new Color[] { INTENSE_MAGENTA, MAGENTA } },
74 new Object[] { 46, new Color[] { INTENSE_CYAN, CYAN } },
75 new Object[] { 47, new Color[] { INTENSE_WHITE, WHITE } },
78 private static int[] commands = {'m','n'}; //SGR , DSR
80 private static List<TextStyle> parseAnsiString(String ansiString) {
82 List<TextStyle> styles = new ArrayList<TextStyle>();
84 StringBuilder sb = new StringBuilder();
86 for (int i=0; i<ansiString.length(); i++) {
87 if (ansiString.charAt(i) == ESCAPE) {
88 int min = ansiString.length() ;
91 for (int j=0 ; j<commands.length ; j++) {
92 startCommand = ansiString.indexOf(commands[j], i);
93 if (min > startCommand && startCommand > -1) {
97 command=ansiString.charAt(min);
100 if ( startCommand < ansiString.length()) {
102 String colorCodes = ansiString.substring(i+2,startCommand);
103 String codes[] = colorCodes.split(";");
104 Color[] colors = getColorAttribute(codes);
105 int styleStart = startCommand+1;
107 int endEscape = ansiString.indexOf(ESCAPE,styleStart);
108 if (endEscape < 0 ) {
109 styleEnd = ansiString.length();
110 i = ansiString.length();
112 int endCommand = ansiString.indexOf(command,endEscape);
113 if (endCommand < 0 ) {
114 styleEnd = ansiString.length();
115 i = ansiString.length();
117 styleEnd = endEscape;
121 String styleString = null;
122 styleString = ansiString.substring(styleStart, styleEnd);
124 TextStyle styleTs = new TextStyle(colors[0],colors[1],styleString);
126 if (sb.length() > 0) {
127 TextStyle defaultTs = new TextStyle(BLACK,WHITE, sb.toString());
128 styles.add(arrayIndex++,defaultTs);
131 styles.add(arrayIndex++,styleTs);
134 }else { //nothing found!
136 sb.append(ansiString.charAt(i));
139 if (sb.length() > 0) {
140 TextStyle defaultTs = new TextStyle(BLACK,WHITE, sb.toString());
141 styles.add(arrayIndex,defaultTs);
145 private static Color[] getColorAttribute(String codes[]) {
146 boolean brighter = false;
147 Color[] color = {BLACK,WHITE}; //foreground&background
149 for (int j = 0; j < codes.length; j++) {
151 if (codes[j].length() > 0 && codes[j].matches("[\\d]*")) {
152 int code = Integer.parseInt(codes[j]);
157 else if (code >= 30 && code <= 39) {
159 color[0] = BLACK; //default foreground color
160 else if (code >= 30 && code <= 37) {
161 color[0] = getColorFromANSICode(code, brighter);
164 } else if (code >= 40 && code <= 49) {
166 color[1] = WHITE; //default background color
167 else if (code >= 40 && code <= 47)
168 color[1] = getColorFromANSICode(code, brighter);
177 private static Color getColorFromANSICode(int code, boolean brighter) {
178 final Color[] colorSet = CODE2COLOR.get( code );
179 if ( null == colorSet )
183 return colorSet[brighter?0:1];
185 public static String getStripAnsiString(String newLineStr) {
186 List<TextStyle> styles = parseAnsiString(newLineStr);
187 if (styles != null) {
188 StringBuilder sb = new StringBuilder();
189 for (int i=0 ; i<styles.size(); i++) {
190 sb.append(styles.get(i).getStripString());
192 return sb.toString();
197 public static List<TextStyle> getStringStyles(String newLineStr) {
198 List<TextStyle> styles = parseAnsiString(newLineStr);
199 if (styles != null) {