001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.shell.utils;
020
021import java.util.HashMap;
022import java.util.Map;
023import java.util.regex.Matcher;
024import java.util.regex.Pattern;
025
026import jline.ANSIBuffer;
027
028/**
029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
030 */
031public class ANSICodes {
032
033    protected static final Pattern TPL = Pattern.compile("\\{([A-Za-z0-9]+)\\}");
034
035    public static final int OFF = 0;
036
037    public static final int BOLD = 1;
038
039    public static final int UNDERSCORE = 4;
040
041    public static final int BLINK = 5;
042
043    public static final int REVERSE = 7;
044
045    public static final int CONCEALED = 8;
046
047    public static final int FG_BLACK = 30;
048
049    public static final int FG_RED = 31;
050
051    public static final int FG_GREEN = 32;
052
053    public static final int FG_YELLOW = 33;
054
055    public static final int FG_BLUE = 34;
056
057    public static final int FG_MAGENTA = 35;
058
059    public static final int FG_CYAN = 36;
060
061    public static final int FG_WHITE = 37;
062
063    public static final int BG_BLACK = 40;
064
065    public static final int BG_RED = 41;
066
067    public static final int BG_GREEN = 42;
068
069    public static final int BG_YELLOW = 43;
070
071    public static final int BG_BLUE = 44;
072
073    public static final int BG_MAGENTA = 45;
074
075    public static final int BG_CYAN = 46;
076
077    public static final int BG_WHITE = 47;
078
079    protected static Map<String, Integer> map = new HashMap<String, Integer>();
080
081    static {
082        map.put("off", OFF);
083        map.put("header", BOLD);
084        map.put("bold", BOLD);
085        map.put("underscore", UNDERSCORE);
086        map.put("blink", BLINK);
087        map.put("reverse", REVERSE);
088        map.put("concealed", CONCEALED);
089        map.put("black", FG_BLACK);
090        map.put("red", FG_RED);
091        map.put("green", FG_GREEN);
092        map.put("yellow", FG_YELLOW);
093        map.put("blue", FG_BLUE);
094        map.put("magenta", FG_MAGENTA);
095        map.put("cyan", FG_CYAN);
096        map.put("white", FG_WHITE);
097        map.put("bg.black", BG_BLACK);
098        map.put("bg.red", BG_RED);
099        map.put("bg.green", BG_GREEN);
100        map.put("bg.yellow", BG_YELLOW);
101        map.put("bg.blue", BG_BLUE);
102        map.put("bg.magenta", BG_MAGENTA);
103        map.put("bg.cyan", BG_CYAN);
104        map.put("bg.white", BG_WHITE);
105    }
106
107    public static int getCode(String key) {
108        Integer code = map.get(key.toLowerCase());
109        if (code == null) {
110            try {
111                return Integer.parseInt(key);
112            } catch (NumberFormatException e) {
113                return -1;
114            }
115        } else {
116            return code.intValue();
117        }
118    }
119
120    public static void append(ANSIBuffer buf, String text, String codeKey, boolean wiki) {
121        int code = getCode(codeKey);
122        if (code > -1) {
123            if (wiki && code == BOLD) {
124                buf.append("*" + text + "*");
125            } else {
126                buf.attrib(text, code);
127            }
128        } else if (wiki) {
129            buf.append("{" + codeKey + "}" + text + "{" + codeKey + "}");
130        } else {
131            buf.append(text);
132        }
133    }
134
135    public static void appendTemplate(ANSIBuffer buf, String content, boolean wiki) {
136        Matcher m = TPL.matcher(content);
137        int s = 0;
138        while (m.find(s)) {
139            String token = m.group(1);
140            String key = '{' + token + '}';
141            int i = content.indexOf(key, m.end());
142            if (i == -1) {
143                buf.append(content.substring(s, m.end()));
144                s = m.end();
145            } else {
146                buf.append(content.substring(s, m.start()));
147                String text = content.substring(m.end(), i);
148                ANSICodes.append(buf, text, token, wiki);
149                s = i + key.length();
150            }
151        }
152        buf.append(content.substring(s, content.length()));
153    }
154
155}