| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 25 |
|
| 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 |
|
| 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 |
} |