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.TextAttributesKey;
22 import com.intellij.openapi.editor.markup.EffectType;
23 import com.intellij.openapi.editor.markup.TextAttributes;
24 import com.intellij.openapi.util.Key;
25 import com.intellij.openapi.util.text.StringUtil;
26 import org.jetbrains.annotations.NonNls;
27 import org.jetbrains.annotations.NotNull;
34 public class ColoredOutputTypeRegistry {
35 public static ColoredOutputTypeRegistry getInstance() {
36 return ServiceManager.getService(ColoredOutputTypeRegistry.class);
39 private static final TextAttributesKey[] myAnsiColorKeys = new TextAttributesKey[]{
40 ConsoleHighlighter.BLACK,
41 ConsoleHighlighter.RED,
42 ConsoleHighlighter.GREEN,
43 ConsoleHighlighter.YELLOW,
44 ConsoleHighlighter.BLUE,
45 ConsoleHighlighter.MAGENTA,
46 ConsoleHighlighter.CYAN,
47 ConsoleHighlighter.GRAY,
49 ConsoleHighlighter.DARKGRAY,
50 ConsoleHighlighter.RED_BRIGHT,
51 ConsoleHighlighter.GREEN_BRIGHT,
52 ConsoleHighlighter.YELLOW_BRIGHT,
53 ConsoleHighlighter.BLUE_BRIGHT,
54 ConsoleHighlighter.MAGENTA_BRIGHT,
55 ConsoleHighlighter.CYAN_BRIGHT,
56 ConsoleHighlighter.WHITE,
61 0 Cancel all attributes except foreground/background color
67 8 Concealed (don't display characters)
68 30 Make foreground (the characters) black
69 31 Make foreground red
70 32 Make foreground green
71 33 Make foreground yellow
72 34 Make foreground blue
73 35 Make foreground magenta
74 36 Make foreground cyan
75 37 Make foreground white
77 40 Make background (around the characters) black
78 41 Make background red
79 42 Make background green
80 43 Make background yellow
81 44 Make background blue
82 45 Make background magenta
83 46 Make background cyan
84 47 Make background white (you may need 0 instead, or in addition)
86 see full doc at http://en.wikipedia.org/wiki/ANSI_escape_code
90 public Key getOutputKey(@NonNls String attribute) {
91 final String completeAttribute = attribute;
92 if (attribute.startsWith("\u001B[")) {
93 attribute = attribute.substring(2);
95 else attribute = StringUtil.trimStart(attribute, "[");
96 attribute = StringUtil.trimEnd(attribute, "m");
97 if (attribute.equals("0")) {
98 return ProcessOutputTypes.STDOUT;
100 TextAttributes attrs = new TextAttributes();
101 final String[] strings = attribute.split(";");
102 for (String string : strings) {
105 value = Integer.parseInt(string);
107 catch (NumberFormatException e) {
111 attrs.setFontType(Font.BOLD);
113 else if (value == 4) {
114 attrs.setEffectType(EffectType.LINE_UNDERSCORE);
116 else if (value == 22) {
117 attrs.setFontType(Font.PLAIN);
119 else if (value == 24) { //not underlined
120 attrs.setEffectType(null);
122 else if (value >= 30 && value <= 37) {
123 attrs.setForegroundColor(getAnsiColor(value - 30));
125 else if (value == 38) {
126 //TODO: 256 colors foreground
128 else if (value == 39) {
129 attrs.setForegroundColor(getColorByKey(ConsoleViewContentType.NORMAL_OUTPUT_KEY));
131 else if (value >= 40 && value <= 47) {
132 attrs.setBackgroundColor(getAnsiColor(value - 40));
134 else if (value == 48) {
135 //TODO: 256 colors background
137 else if (value == 49) {
138 attrs.setBackgroundColor(getColorByKey(ConsoleViewContentType.NORMAL_OUTPUT_KEY));
140 else if (value >= 90 && value <= 97) {
141 attrs.setForegroundColor(
142 getAnsiColor(value - 82));
144 else if (value >= 100 && value <= 107) {
145 attrs.setBackgroundColor(
146 getAnsiColor(value - 92));
149 if (attrs.getEffectType() == EffectType.LINE_UNDERSCORE) {
150 attrs.setEffectColor(attrs.getForegroundColor());
152 Key newKey = new Key(completeAttribute);
153 ConsoleViewContentType contentType = new ConsoleViewContentType(completeAttribute, attrs);
154 ConsoleViewContentType.registerNewConsoleViewType(newKey, contentType);
158 private static Color getAnsiColor(final int value) {
159 return getColorByKey(getAnsiColorKey(value));
162 private static Color getColorByKey(TextAttributesKey colorKey) {
163 return EditorColorsManager.getInstance().getGlobalScheme().getAttributes(colorKey).getForegroundColor();
166 public static TextAttributesKey getAnsiColorKey(int value) {
168 return ConsoleViewContentType.NORMAL_OUTPUT_KEY;
170 return myAnsiColorKeys[value];