import org.tizen.common.util.MapUtil;
public class AnsicodeAdapter {
- public static final char ESCAPE = '\033'; // ANSI Escape Character that starts commands
-
- public static final Color BLACK = new Color(getDisplay(), 0, 0, 0); //Display.getCurrent().getSystemColor(SWT.COLOR_BLACK);
- public static final Color RED = new Color(getDisplay(), 255, 0, 0); //Display.getCurrent().getSystemColor(SWT.COLOR_RED);
- public static final Color GREEN = new Color(getDisplay(), 0, 255, 0); //Display.getCurrent().getSystemColor(SWT.COLOR_GREEN);
- public static final Color YELLOW = new Color(getDisplay(), 255, 255, 0); //Display.getCurrent().getSystemColor(SWT.COLOR_YELLOW);
- public static final Color BLUE = new Color(getDisplay(), 0,0,255); //Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);
- public static final Color MAGENTA = new Color(getDisplay(), 255, 0, 255); //Display.getCurrent().getSystemColor(SWT.COLOR_MAGENTA);
- public static final Color CYAN = new Color(getDisplay(), 0,255, 255); //Display.getCurrent().getSystemColor(SWT.COLOR_CYAN);
- public static final Color WHITE = new Color(getDisplay(), 255, 255, 255); //Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
-
- public static final Color INTENSE_BLACK = new Color(getDisplay(), 0, 0, 0); //Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GRAY);
- public static final Color INTENSE_RED = new Color(getDisplay(), 139, 0, 0); //Display.getCurrent().getSystemColor(SWT.COLOR_DARK_RED);
- public static final Color INTENSE_GREEN = new Color(getDisplay(), 0, 100, 0);//Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GREEN);
- public static final Color INTENSE_YELLOW = new Color(getDisplay(), 250,250,210);//Display.getCurrent().getSystemColor(SWT.COLOR_DARK_YELLOW);
- public static final Color INTENSE_BLUE = new Color(getDisplay(), 0,0,139);//Display.getCurrent().getSystemColor(SWT.COLOR_DARK_BLUE);
- public static final Color INTENSE_MAGENTA = new Color(getDisplay(), 139,0,139);//Display.getCurrent().getSystemColor(SWT.COLOR_DARK_MAGENTA);
- public static final Color INTENSE_CYAN = new Color(getDisplay(), 0, 139, 139);//Display.getCurrent().getSystemColor(SWT.COLOR_DARK_CYAN);
- public static final Color INTENSE_WHITE = new Color(getDisplay(), 245, 245, 245);//Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
+ public static final char ESCAPE = '\033'; // ANSI Escape Character that starts commands
+
+ public static final Color BLACK = new Color(getDisplay(), 0, 0, 0); //Display.getCurrent().getSystemColor(SWT.COLOR_BLACK);
+ public static final Color RED = new Color(getDisplay(), 255, 0, 0); //Display.getCurrent().getSystemColor(SWT.COLOR_RED);
+ public static final Color GREEN = new Color(getDisplay(), 0, 255, 0); //Display.getCurrent().getSystemColor(SWT.COLOR_GREEN);
+ public static final Color YELLOW = new Color(getDisplay(), 255, 255, 0); //Display.getCurrent().getSystemColor(SWT.COLOR_YELLOW);
+ public static final Color BLUE = new Color(getDisplay(), 0,0,255); //Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);
+ public static final Color MAGENTA = new Color(getDisplay(), 255, 0, 255); //Display.getCurrent().getSystemColor(SWT.COLOR_MAGENTA);
+ public static final Color CYAN = new Color(getDisplay(), 0,255, 255); //Display.getCurrent().getSystemColor(SWT.COLOR_CYAN);
+ public static final Color WHITE = new Color(getDisplay(), 255, 255, 255); //Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
+
+ public static final Color INTENSE_BLACK = new Color(getDisplay(), 0, 0, 0); //Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GRAY);
+ public static final Color INTENSE_RED = new Color(getDisplay(), 139, 0, 0); //Display.getCurrent().getSystemColor(SWT.COLOR_DARK_RED);
+ public static final Color INTENSE_GREEN = new Color(getDisplay(), 0, 100, 0);//Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GREEN);
+ public static final Color INTENSE_YELLOW = new Color(getDisplay(), 250,250,210);//Display.getCurrent().getSystemColor(SWT.COLOR_DARK_YELLOW);
+ public static final Color INTENSE_BLUE = new Color(getDisplay(), 0,0,139);//Display.getCurrent().getSystemColor(SWT.COLOR_DARK_BLUE);
+ public static final Color INTENSE_MAGENTA = new Color(getDisplay(), 139,0,139);//Display.getCurrent().getSystemColor(SWT.COLOR_DARK_MAGENTA);
+ public static final Color INTENSE_CYAN = new Color(getDisplay(), 0, 139, 139);//Display.getCurrent().getSystemColor(SWT.COLOR_DARK_CYAN);
+ public static final Color INTENSE_WHITE = new Color(getDisplay(), 245, 245, 245);//Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
public static final Map<Integer, Color[]> CODE2COLOR = Collections.unmodifiableMap( MapUtil.<Integer, Color[]>asMap( new Object[][] {
new Object[] { 30, new Color[] { INTENSE_BLACK, BLACK } },
new Object[] { 47, new Color[] { INTENSE_WHITE, WHITE } },
} ) );
- private static int[] commands = {'m','n'}; //SGR , DSR
-
- private static List<TextStyle> parseAnsiString(String ansiString) {
- char command = 0;
- List<TextStyle> styles = new ArrayList<TextStyle>();
- int arrayIndex = 0;
- StringBuilder sb = new StringBuilder();
-
- for (int i=0; i<ansiString.length(); i++) {
- if (ansiString.charAt(i) == ESCAPE) {
- int min = ansiString.length() - 1;
- int startCommand = 0;
-
- for (int j=0 ; j<commands.length ; j++) {
- startCommand = ansiString.indexOf(commands[j], i);
- if (min > startCommand && startCommand > -1) {
- min = startCommand;
- }
- }
- command=ansiString.charAt(min);
- startCommand=min;
-
- if ( startCommand < ansiString.length()) {
-
- String colorCodes = ansiString.substring(i+2,startCommand);
- String codes[] = colorCodes.split(";");
- Color[] colors = getColorAttribute(codes);
- int styleStart = startCommand+1;
- int styleEnd = 0;
- int endEscape = ansiString.indexOf(ESCAPE,styleStart);
- if (endEscape < 0 ) {
- styleEnd = ansiString.length();
- i = ansiString.length();
- } else {
- int endCommand = ansiString.indexOf(command,endEscape);
- if (endCommand < 0 ) {
- styleEnd = ansiString.length();
- i = ansiString.length();
- } else {
- styleEnd = endEscape;
- i = endCommand;
- }
- }
- String styleString = null;
- styleString = ansiString.substring(styleStart, styleEnd);
-
- TextStyle styleTs = new TextStyle(colors[0],colors[1],styleString);
-
- if (sb.length() > 0) {
- TextStyle defaultTs = new TextStyle(BLACK,WHITE, sb.toString());
- styles.add(arrayIndex++,defaultTs);
- sb.setLength(0);
- }
- styles.add(arrayIndex++,styleTs);
+ private static int[] commands = {'m','n'}; //SGR, DSR
+
+ private static List<TextStyle> parseAnsiString(String ansiString) {
+ char command = 0;
+ List<TextStyle> styles = new ArrayList<TextStyle>();
+ int arrayIndex = 0;
+ StringBuilder sb = new StringBuilder();
+
+ for (int i=0; i<ansiString.length(); i++) {
+ if (ansiString.charAt(i) == ESCAPE) {
+ int min = ansiString.length() - 1;
+ int startCommand = 0;
+
+ for (int j=0 ; j<commands.length ; j++) {
+ startCommand = ansiString.indexOf(commands[j], i);
+ if (min > startCommand && startCommand > -1) {
+ min = startCommand;
+ }
+ }
+ command=ansiString.charAt(min);
+ startCommand=min;
+
+ if ( startCommand < ansiString.length()) {
+ String colorCodes = ansiString.substring(i+2,startCommand);
+ String codes[] = colorCodes.split(";");
+ Color[] colors = getColorAttribute(codes);
+ int styleStart = startCommand+1;
+ int styleEnd = 0;
+ int endEscape = ansiString.indexOf(ESCAPE,styleStart);
+ if (endEscape < 0 ) {
+ styleEnd = ansiString.length();
+ i = ansiString.length();
+ } else {
+ int endCommand = ansiString.indexOf(command,endEscape);
+ if (endCommand < 0 ) {
+ styleEnd = ansiString.length();
+ i = ansiString.length();
+ } else {
+ styleEnd = endEscape;
+ i = endCommand;
+ }
+ }
+ String styleString = null;
+ styleString = ansiString.substring(styleStart, styleEnd);
+
+ TextStyle styleTs = new TextStyle(colors[0],colors[1],styleString);
- }
- }else { //nothing found!
-
- sb.append(ansiString.charAt(i));
- }
- }
- if (sb.length() > 0) {
- TextStyle defaultTs = new TextStyle(BLACK,WHITE, sb.toString());
- styles.add(arrayIndex,defaultTs);
- }
- return styles;
- }
- private static Color[] getColorAttribute(String codes[]) {
- boolean brighter = false;
- Color[] color = {BLACK,WHITE}; //foreground&background
-
+ if (sb.length() > 0) {
+ TextStyle defaultTs = new TextStyle(BLACK,WHITE, sb.toString());
+ styles.add(arrayIndex++,defaultTs);
+ sb.setLength(0);
+ }
+ styles.add(arrayIndex++,styleTs);
+ }
+ } else { //nothing found!
+ sb.append(ansiString.charAt(i));
+ }
+ }
+ if (sb.length() > 0) {
+ TextStyle defaultTs = new TextStyle(BLACK, WHITE, sb.toString());
+ styles.add(arrayIndex,defaultTs);
+ }
+ return styles;
+ }
+ private static Color[] getColorAttribute(String codes[]) {
+ boolean brighter = false;
+ Color[] color = {BLACK,WHITE}; //foreground&background
+
for (int j = 0; j < codes.length; j++) {
-
if (codes[j].length() > 0 && codes[j].matches("[\\d]*")) {
int code = Integer.parseInt(codes[j]);
if (code == 0)
else if (code >= 30 && code <= 39) {
if (code == 39)
{
- color[0] = BLACK; //default foreground color
+ color[0] = BLACK; //default foreground color
}
else if (code >= 30 && code <= 37) {
- color[0] = getColorFromANSICode(code, brighter);
+ color[0] = getColorFromANSICode(code, brighter);
brighter = false;
}
- } else if (code >= 40 && code <= 49) {
+ }
+ else if (code >= 40 && code <= 49) {
if (code == 49)
{
- color[1] = WHITE; //default background color
+ color[1] = WHITE; //default background color
}
else if (code >= 40 && code <= 47)
{
- color[1] = getColorFromANSICode(code, brighter);
+ color[1] = getColorFromANSICode(code, brighter);
}
}
}
}
return color;
- }
-
-
+ }
+
+ private static Color getColorFromANSICode(int code, boolean brighter) {
+ final Color[] colorSet = CODE2COLOR.get( code );
+ if ( null == colorSet )
+ {
+ return null;
+ }
+ return colorSet[brighter?0:1];
+ }
+
+ public static String getStripAnsiString(String newLineStr) {
+ List<TextStyle> styles = parseAnsiString(newLineStr);
+ if (styles != null) {
+ StringBuilder sb = new StringBuilder();
+ for (int i=0 ; i<styles.size(); i++) {
+ sb.append(styles.get(i).getStripString());
+ }
+ return sb.toString();
+ } else {
+ return newLineStr;
+ }
+ }
- private static Color getColorFromANSICode(int code, boolean brighter) {
- final Color[] colorSet = CODE2COLOR.get( code );
- if ( null == colorSet )
- {
- return null;
- }
- return colorSet[brighter?0:1];
+ public static List<TextStyle> getStringStyles(String newLineStr) {
+ List<TextStyle> styles = parseAnsiString(newLineStr);
+ return styles;
}
- public static String getStripAnsiString(String newLineStr) {
- List<TextStyle> styles = parseAnsiString(newLineStr);
- if (styles != null) {
- StringBuilder sb = new StringBuilder();
- for (int i=0 ; i<styles.size(); i++) {
- sb.append(styles.get(i).getStripString());
- }
- return sb.toString();
- }else {
- return newLineStr;
- }
- }
- public static List<TextStyle> getStringStyles(String newLineStr) {
- List<TextStyle> styles = parseAnsiString(newLineStr);
- return styles;
- }
}