2 * Copyright 2000-2015 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.execution.process;
18 import com.intellij.execution.ui.ConsoleViewContentType;
19 import com.intellij.openapi.components.ServiceManager;
20 import com.intellij.openapi.editor.colors.EditorColorsManager;
21 import com.intellij.openapi.editor.colors.EditorColorsScheme;
22 import com.intellij.openapi.editor.colors.TextAttributesKey;
23 import com.intellij.openapi.editor.markup.EffectType;
24 import com.intellij.openapi.editor.markup.TextAttributes;
25 import com.intellij.openapi.util.Key;
26 import com.intellij.openapi.util.text.StringUtil;
27 import org.jetbrains.annotations.NonNls;
28 import org.jetbrains.annotations.NotNull;
35 public class ColoredOutputTypeRegistry {
36 public static ColoredOutputTypeRegistry getInstance() {
37 return ServiceManager.getService(ColoredOutputTypeRegistry.class);
40 private static final TextAttributesKey[] myAnsiColorKeys = new TextAttributesKey[]{
41 ConsoleHighlighter.BLACK,
42 ConsoleHighlighter.RED,
43 ConsoleHighlighter.GREEN,
44 ConsoleHighlighter.YELLOW,
45 ConsoleHighlighter.BLUE,
46 ConsoleHighlighter.MAGENTA,
47 ConsoleHighlighter.CYAN,
48 ConsoleHighlighter.GRAY,
50 ConsoleHighlighter.DARKGRAY,
51 ConsoleHighlighter.RED_BRIGHT,
52 ConsoleHighlighter.GREEN_BRIGHT,
53 ConsoleHighlighter.YELLOW_BRIGHT,
54 ConsoleHighlighter.BLUE_BRIGHT,
55 ConsoleHighlighter.MAGENTA_BRIGHT,
56 ConsoleHighlighter.CYAN_BRIGHT,
57 ConsoleHighlighter.WHITE,
62 0 Cancel all attributes except foreground/background color
68 8 Concealed (don't display characters)
69 30 Make foreground (the characters) black
70 31 Make foreground red
71 32 Make foreground green
72 33 Make foreground yellow
73 34 Make foreground blue
74 35 Make foreground magenta
75 36 Make foreground cyan
76 37 Make foreground white
78 40 Make background (around the characters) black
79 41 Make background red
80 42 Make background green
81 43 Make background yellow
82 44 Make background blue
83 45 Make background magenta
84 46 Make background cyan
85 47 Make background white (you may need 0 instead, or in addition)
87 see full doc at http://en.wikipedia.org/wiki/ANSI_escape_code
91 public Key getOutputKey(@NonNls String attribute) {
92 final String completeAttribute = attribute;
93 if (attribute.startsWith("\u001B[")) {
94 attribute = attribute.substring(2);
97 attribute = StringUtil.trimStart(attribute, "[");
99 attribute = StringUtil.trimEnd(attribute, "m");
100 if (attribute.equals("0")) {
101 return ProcessOutputTypes.STDOUT;
103 boolean inverse = false;
104 TextAttributes attrs = new TextAttributes();
105 final String[] strings = attribute.split(";");
106 for (String string : strings) {
109 value = Integer.parseInt(string);
111 catch (NumberFormatException e) {
115 attrs.setFontType(Font.BOLD);
117 else if (value == 4) {
118 attrs.setEffectType(EffectType.LINE_UNDERSCORE);
120 else if (value == 7) {
123 else if (value == 22) {
124 attrs.setFontType(Font.PLAIN);
126 else if (value == 24) { //not underlined
127 attrs.setEffectType(null);
129 else if (value >= 30 && value <= 37) {
130 attrs.setForegroundColor(getAnsiColor(value - 30));
132 else if (value == 38) {
133 //TODO: 256 colors foreground
135 else if (value == 39) {
136 attrs.setForegroundColor(getDefaultForegroundColor());
138 else if (value >= 40 && value <= 47) {
139 attrs.setBackgroundColor(getAnsiColor(value - 40));
141 else if (value == 48) {
142 //TODO: 256 colors background
144 else if (value == 49) {
145 attrs.setBackgroundColor(getDefaultBackgroundColor());
147 else if (value >= 90 && value <= 97) {
148 attrs.setForegroundColor(
149 getAnsiColor(value - 82));
151 else if (value >= 100 && value <= 107) {
152 attrs.setBackgroundColor(
153 getAnsiColor(value - 92));
156 if (attrs.getEffectType() == EffectType.LINE_UNDERSCORE) {
157 Color foregroundColor = attrs.getForegroundColor();
158 if (foregroundColor == null) {
159 foregroundColor = getDefaultForegroundColor();
161 attrs.setEffectColor(foregroundColor);
164 Color foregroundColor = attrs.getForegroundColor();
165 if (foregroundColor == null) {
166 foregroundColor = getDefaultForegroundColor();
168 Color backgroundColor = attrs.getBackgroundColor();
169 if (backgroundColor == null) {
170 backgroundColor = getDefaultBackgroundColor();
172 attrs.setForegroundColor(backgroundColor);
173 attrs.setEffectColor(backgroundColor);
174 attrs.setBackgroundColor(foregroundColor);
176 Key newKey = new Key(completeAttribute);
177 ConsoleViewContentType contentType = new ConsoleViewContentType(completeAttribute, attrs);
178 ConsoleViewContentType.registerNewConsoleViewType(newKey, contentType);
182 private static Color getAnsiColor(final int value) {
183 return getColorByKey(getAnsiColorKey(value));
186 private static Color getColorByKey(TextAttributesKey colorKey) {
187 return EditorColorsManager.getInstance().getGlobalScheme().getAttributes(colorKey).getForegroundColor();
191 private static Color getDefaultForegroundColor() {
192 EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
193 TextAttributes attr = scheme.getAttributes(ConsoleViewContentType.NORMAL_OUTPUT_KEY);
194 Color color = attr != null ? attr.getForegroundColor() : null;
196 color = scheme.getDefaultForeground();
202 private static Color getDefaultBackgroundColor() {
203 EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
204 Color color = scheme.getColor(ConsoleViewContentType.CONSOLE_BACKGROUND_KEY);
206 color = scheme.getDefaultBackground();
211 public static TextAttributesKey getAnsiColorKey(int value) {
213 return ConsoleViewContentType.NORMAL_OUTPUT_KEY;
215 return myAnsiColorKeys[value];