001/* 002 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * bstefanescu 016 */ 017package org.nuxeo.shell.utils; 018 019import java.util.HashMap; 020import java.util.Map; 021import java.util.regex.Matcher; 022import java.util.regex.Pattern; 023 024import jline.ANSIBuffer; 025 026/** 027 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 028 */ 029public class ANSICodes { 030 031 protected static final Pattern TPL = Pattern.compile("\\{([A-Za-z0-9]+)\\}"); 032 033 public static final int OFF = 0; 034 035 public static final int BOLD = 1; 036 037 public static final int UNDERSCORE = 4; 038 039 public static final int BLINK = 5; 040 041 public static final int REVERSE = 7; 042 043 public static final int CONCEALED = 8; 044 045 public static final int FG_BLACK = 30; 046 047 public static final int FG_RED = 31; 048 049 public static final int FG_GREEN = 32; 050 051 public static final int FG_YELLOW = 33; 052 053 public static final int FG_BLUE = 34; 054 055 public static final int FG_MAGENTA = 35; 056 057 public static final int FG_CYAN = 36; 058 059 public static final int FG_WHITE = 37; 060 061 public static final int BG_BLACK = 40; 062 063 public static final int BG_RED = 41; 064 065 public static final int BG_GREEN = 42; 066 067 public static final int BG_YELLOW = 43; 068 069 public static final int BG_BLUE = 44; 070 071 public static final int BG_MAGENTA = 45; 072 073 public static final int BG_CYAN = 46; 074 075 public static final int BG_WHITE = 47; 076 077 protected static Map<String, Integer> map = new HashMap<String, Integer>(); 078 079 static { 080 map.put("off", OFF); 081 map.put("header", BOLD); 082 map.put("bold", BOLD); 083 map.put("underscore", UNDERSCORE); 084 map.put("blink", BLINK); 085 map.put("reverse", REVERSE); 086 map.put("concealed", CONCEALED); 087 map.put("black", FG_BLACK); 088 map.put("red", FG_RED); 089 map.put("green", FG_GREEN); 090 map.put("yellow", FG_YELLOW); 091 map.put("blue", FG_BLUE); 092 map.put("magenta", FG_MAGENTA); 093 map.put("cyan", FG_CYAN); 094 map.put("white", FG_WHITE); 095 map.put("bg.black", BG_BLACK); 096 map.put("bg.red", BG_RED); 097 map.put("bg.green", BG_GREEN); 098 map.put("bg.yellow", BG_YELLOW); 099 map.put("bg.blue", BG_BLUE); 100 map.put("bg.magenta", BG_MAGENTA); 101 map.put("bg.cyan", BG_CYAN); 102 map.put("bg.white", BG_WHITE); 103 } 104 105 public static int getCode(String key) { 106 Integer code = map.get(key.toLowerCase()); 107 if (code == null) { 108 try { 109 return Integer.parseInt(key); 110 } catch (NumberFormatException e) { 111 return -1; 112 } 113 } else { 114 return code.intValue(); 115 } 116 } 117 118 public static void append(ANSIBuffer buf, String text, String codeKey, boolean wiki) { 119 int code = getCode(codeKey); 120 if (code > -1) { 121 if (wiki && code == BOLD) { 122 buf.append("*" + text + "*"); 123 } else { 124 buf.attrib(text, code); 125 } 126 } else if (wiki) { 127 buf.append("{" + codeKey + "}" + text + "{" + codeKey + "}"); 128 } else { 129 buf.append(text); 130 } 131 } 132 133 public static void appendTemplate(ANSIBuffer buf, String content, boolean wiki) { 134 Matcher m = TPL.matcher(content); 135 int s = 0; 136 while (m.find(s)) { 137 String token = m.group(1); 138 String key = '{' + token + '}'; 139 int i = content.indexOf(key, m.end()); 140 if (i == -1) { 141 buf.append(content.substring(s, m.end())); 142 s = m.end(); 143 } else { 144 buf.append(content.substring(s, m.start())); 145 String text = content.substring(m.end(), i); 146 ANSICodes.append(buf, text, token, wiki); 147 s = i + key.length(); 148 } 149 } 150 buf.append(content.substring(s, content.length())); 151 } 152 153}