| 1 | |
package org.jbehave.core.reporters; |
| 2 | |
|
| 3 | |
import java.util.HashMap; |
| 4 | |
import java.util.Map; |
| 5 | |
import java.util.Properties; |
| 6 | |
import org.jbehave.core.configuration.Keywords; |
| 7 | |
|
| 8 | |
import static org.jbehave.core.reporters.ANSIConsoleOutput.SGRCode.BLUE; |
| 9 | |
import static org.jbehave.core.reporters.ANSIConsoleOutput.SGRCode.BOLD; |
| 10 | |
import static org.jbehave.core.reporters.ANSIConsoleOutput.SGRCode.GREEN; |
| 11 | |
import static org.jbehave.core.reporters.ANSIConsoleOutput.SGRCode.MAGENTA; |
| 12 | |
import static org.jbehave.core.reporters.ANSIConsoleOutput.SGRCode.RED; |
| 13 | |
import static org.jbehave.core.reporters.ANSIConsoleOutput.SGRCode.RESET; |
| 14 | |
import static org.jbehave.core.reporters.ANSIConsoleOutput.SGRCode.YELLOW; |
| 15 | |
import static org.jbehave.core.steps.StepCreator.PARAMETER_VALUE_END; |
| 16 | |
import static org.jbehave.core.steps.StepCreator.PARAMETER_VALUE_START; |
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
public class ANSIConsoleOutput extends ConsoleOutput { |
| 24 | |
|
| 25 | |
private static final char ESCAPE_CHARACTER = (char) 27; |
| 26 | |
private static final String SGR_CONTROL = "m"; |
| 27 | |
private static final String CODE_SEPARATOR = ";"; |
| 28 | |
|
| 29 | 7 | @SuppressWarnings("serial") |
| 30 | 7 | private Map<String, SGRCode> codes = new HashMap<String, SGRCode>() { |
| 31 | |
{ |
| 32 | 7 | put("successful", GREEN); |
| 33 | 7 | put("pending", YELLOW); |
| 34 | 7 | put("pendingMethod", YELLOW); |
| 35 | 7 | put("notPerformed", MAGENTA); |
| 36 | 7 | put("ignorable", BLUE); |
| 37 | 7 | put("failed", RED); |
| 38 | 7 | put("cancelled", RED); |
| 39 | 7 | put("restarted", MAGENTA); |
| 40 | 7 | } |
| 41 | |
}; |
| 42 | |
|
| 43 | |
public ANSIConsoleOutput() { |
| 44 | 7 | super(); |
| 45 | 7 | } |
| 46 | |
|
| 47 | |
public ANSIConsoleOutput(Keywords keywords) { |
| 48 | 0 | super(keywords); |
| 49 | 0 | } |
| 50 | |
|
| 51 | |
public ANSIConsoleOutput(Properties outputPatterns, Keywords keywords, boolean reportFailureTrace) { |
| 52 | 0 | super(outputPatterns, keywords, reportFailureTrace); |
| 53 | 0 | } |
| 54 | |
|
| 55 | |
@Override |
| 56 | |
protected String format(String eventKey, String defaultPattern, Object... args) { |
| 57 | 8 | final String formatted = super.format(eventKey, defaultPattern, args); |
| 58 | |
|
| 59 | 8 | if (codes.containsKey(eventKey)) { |
| 60 | 7 | SGRCode code = codes.get(eventKey); |
| 61 | 7 | return escapeCodeFor(code) + boldifyParams(formatted, code) + escapeCodeFor(RESET); |
| 62 | |
} |
| 63 | |
|
| 64 | 1 | return formatted; |
| 65 | |
} |
| 66 | |
|
| 67 | |
private String boldifyParams(String formatted, SGRCode currentColor) { |
| 68 | 7 | final String valueStart = lookupPattern(PARAMETER_VALUE_START, PARAMETER_VALUE_START); |
| 69 | 7 | final String valueEnd = lookupPattern(PARAMETER_VALUE_END, PARAMETER_VALUE_END); |
| 70 | 7 | return formatted |
| 71 | |
.replaceAll(valueStart, escapeCodeFor(BOLD, currentColor)) |
| 72 | |
.replaceAll(valueEnd, escapeCodeFor(RESET, currentColor)); |
| 73 | |
} |
| 74 | |
|
| 75 | |
private String escapeCodeFor(SGRCode code) { |
| 76 | 14 | return controlSequenceInitiator(code + SGR_CONTROL); |
| 77 | |
} |
| 78 | |
|
| 79 | |
private String escapeCodeFor(SGRCode first, SGRCode second) { |
| 80 | 14 | return controlSequenceInitiator(first + CODE_SEPARATOR + second + SGR_CONTROL); |
| 81 | |
} |
| 82 | |
|
| 83 | |
private String controlSequenceInitiator(String code) { |
| 84 | 28 | return ESCAPE_CHARACTER + "[" + code; |
| 85 | |
} |
| 86 | |
|
| 87 | |
public void assignCodeToEvent(String eventKey, SGRCode code) { |
| 88 | 1 | codes.put(eventKey, code); |
| 89 | 1 | } |
| 90 | |
|
| 91 | 1 | public static enum SGRCode { |
| 92 | 1 | RESET(0), |
| 93 | 1 | BOLD(1), |
| 94 | 1 | DARK(2), |
| 95 | 1 | ITALIC(3), |
| 96 | 1 | UNDERLINE(4), |
| 97 | 1 | BLINK(5), |
| 98 | 1 | RAPID_BLINK(6), |
| 99 | 1 | NEGATIVE(7), |
| 100 | 1 | CONCEALED(8), |
| 101 | 1 | STRIKETHROUGH(9), |
| 102 | 1 | BLACK(30), |
| 103 | 1 | RED(31), |
| 104 | 1 | GREEN(32), |
| 105 | 1 | YELLOW(33), |
| 106 | 1 | BLUE(34), |
| 107 | 1 | MAGENTA(35), |
| 108 | 1 | CYAN(36), |
| 109 | 1 | WHITE(37), |
| 110 | 1 | ON_BLACK(40), |
| 111 | 1 | ON_RED(41), |
| 112 | 1 | ON_GREEN(42), |
| 113 | 1 | ON_YELLOW(43), |
| 114 | 1 | ON_BLUE(44), |
| 115 | 1 | ON_MAGENTA(45), |
| 116 | 1 | ON_CYAN(46), |
| 117 | 1 | ON_WHITE(47); |
| 118 | |
|
| 119 | |
private final int code; |
| 120 | |
|
| 121 | 26 | SGRCode(int code) { |
| 122 | 26 | this.code = code; |
| 123 | 26 | } |
| 124 | |
|
| 125 | |
@Override |
| 126 | |
public String toString() { |
| 127 | 42 | return Integer.toString(code); |
| 128 | |
} |
| 129 | |
} |
| 130 | |
|
| 131 | |
} |