Debian Codegraph @

src/com/google/javascript/jscomp/AbstractMessageFormatter.java

raw symbols SCIP
1 /*
2 * Copyright 2007 The Closure Compiler Authors.
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package com.google.javascript.jscomp;
17
18 import com.google.common.collect.ImmutableSet;
19 import com.google.javascript.jscomp.CheckLevel;
20
21 import java.util.Set;
22
23 /**
24 * Abstract message formatter providing default behavior for implementations
25 * of {@link MessageFormatter} needing a {@link SourceExcerptProvider}.
26 *
27 */
28 public abstract class AbstractMessageFormatter implements MessageFormatter {
29 private final SourceExcerptProvider source;
30 private boolean colorize;
31
32 public AbstractMessageFormatter(SourceExcerptProvider source) {
33 this.source = source;
34 }
35
36 public void setColorize(boolean colorize) {
37 this.colorize = colorize;
38 }
39
40 /**
41 * Get the source excerpt provider.
42 */
43 protected final SourceExcerptProvider getSource() {
44 return source;
45 }
46
47 private static final Set<String> SUPPORTED_COLOR_TERMINALS =
48 ImmutableSet.of("xterm",
49 "xterm-color",
50 "xterm-256color",
51 "screen-bce");
52
53 static boolean termSupportsColor(String term) {
54 return SUPPORTED_COLOR_TERMINALS.contains(term);
55 }
56
57 private static enum Color {
58 ERROR("\033[31m"),
59 WARNING("\033[35m"),
60 RESET("\033[39m");
61
62 private final String controlCharacter;
63
64 Color(String controlCharacter) {
65 this.controlCharacter = controlCharacter;
66 }
67
68 public String getControlCharacter() {
69 return controlCharacter;
70 }
71 }
72
73 String getLevelName(CheckLevel level) {
74 switch (level) {
75 case ERROR: return maybeColorize("ERROR", Color.ERROR);
76 case WARNING: return maybeColorize("WARNING", Color.WARNING);
77 default: return level.toString();
78 }
79 }
80
81 private String maybeColorize(String text, Color color) {
82 if (!colorize) return text;
83
84 return color.getControlCharacter() +
85 text + Color.RESET.getControlCharacter();
86 }
87 }