SGRCodes.java

  1. package org.jbehave.core.reporters;

  2. import static org.jbehave.core.reporters.SGRCodes.SGRCode.BLUE;
  3. import static org.jbehave.core.reporters.SGRCodes.SGRCode.BRIGHT_MAGENTA;
  4. import static org.jbehave.core.reporters.SGRCodes.SGRCode.GREEN;
  5. import static org.jbehave.core.reporters.SGRCodes.SGRCode.MAGENTA;
  6. import static org.jbehave.core.reporters.SGRCodes.SGRCode.RED;
  7. import static org.jbehave.core.reporters.SGRCodes.SGRCode.UNDERLINE;
  8. import static org.jbehave.core.reporters.SGRCodes.SGRCode.YELLOW;

  9. import java.util.HashMap;
  10. import java.util.Map;

  11. /**
  12.  * <p>
  13.  * Manages {@link SGRCode}s used by {@link ANSIConsoleOutput}
  14.  * </p>
  15.  */
  16. public class SGRCodes {

  17.     public enum SGRCode {
  18.         RESET(0),
  19.         BOLD(1),
  20.         FAINT(2),
  21.         ITALIC(3),
  22.         UNDERLINE(4),
  23.         SLOW_BLINK(5),
  24.         RAPID_BLINK(6),
  25.         NEGATIVE(7),
  26.         CONCEALED(8),
  27.         CROSSED_OUT(9),
  28.         BLACK(30),
  29.         RED(31),
  30.         GREEN(32),
  31.         YELLOW(33),
  32.         BLUE(34),
  33.         MAGENTA(35),
  34.         CYAN(36),
  35.         WHITE(37),
  36.         ON_BLACK(40),
  37.         ON_RED(41),
  38.         ON_GREEN(42),
  39.         ON_YELLOW(43),
  40.         ON_BLUE(44),
  41.         ON_MAGENTA(45),
  42.         ON_CYAN(46),
  43.         ON_WHITE(47),
  44.         BRIGHT_BLACK(90),
  45.         BRIGHT_RED(91),
  46.         BRIGHT_GREEN(92),
  47.         BRIGHT_YELLOW(93),
  48.         BRIGHT_BLUE(94),
  49.         BRIGHT_MAGENTA(95),
  50.         BRIGHT_CYAN(96),
  51.         BRIGHT_WHITE(97);

  52.         private final int code;

  53.         SGRCode(int code) {
  54.             this.code = code;
  55.         }

  56.         @Override
  57.         public String toString() {
  58.             return Integer.toString(code);
  59.         }
  60.     }

  61.     @SuppressWarnings("serial")
  62.     public static final Map<String, SGRCode> DEFAULT_CODES = new HashMap<String, SGRCode>() {
  63.         {
  64.             put("narrative", BLUE);
  65.             put("beforeScenario", BRIGHT_MAGENTA);
  66.             put("successful", GREEN);
  67.             put("pending", YELLOW);
  68.             put("pendingMethod", YELLOW);
  69.             put("notPerformed", MAGENTA);
  70.             put("comment", BLUE);
  71.             put("ignorable", BLUE);
  72.             put("failed", RED);
  73.             put("cancelled", RED);
  74.             put("restarted", MAGENTA);
  75.             put("highlight", UNDERLINE);
  76.         }
  77.     };

  78.     private final Map<String, SGRCode> codes;

  79.     public SGRCodes() {
  80.         this(DEFAULT_CODES);
  81.     }

  82.     public SGRCodes(Map<String, SGRCode> codes) {
  83.         this.codes = codes;
  84.     }

  85.     public void assignCode(String key, SGRCode code) {
  86.         codes.put(key, code);
  87.     }

  88.     public boolean hasCode(String key) {
  89.         return codes.containsKey(key);
  90.     }

  91.     public SGRCode getCode(String key) {
  92.         if (codes.containsKey(key)) {
  93.             return codes.get(key);
  94.         }

  95.         throw new RuntimeException("No code found for key " + key);
  96.     }

  97. }