Enum TerminalColor

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<TerminalColor>

    public enum TerminalColor
    extends java.lang.Enum<TerminalColor>
    ANSI escape sequences for colors, to style text in terminal environments.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String ANSI_RESET
      Reset all attributes.
      private static java.lang.String ESCAPE  
      private int fgCode  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private TerminalColor​(int fgCode)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String apply​(java.lang.String text, boolean bright, boolean background, boolean bold)
      Style the given string with this color.
      private int getCode​(boolean bright, boolean background)  
      private java.lang.String getEscape​(boolean bright, boolean background, boolean bold)  
      static java.lang.String style​(java.lang.String text, TerminalColor foreground, TerminalColor background, boolean brightFg, boolean brightBg, boolean boldText)
      Style both foreground and background.
      static TerminalColor valueOf​(java.lang.String name)
      Returns the enum constant of this type with the specified name.
      static TerminalColor[] values()
      Returns an array containing the constants of this enum type, in the order they are declared.
      • Methods inherited from class java.lang.Enum

        clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • ANSI_RESET

        public static final java.lang.String ANSI_RESET
        Reset all attributes.
        See Also:
        Constant Field Values
      • fgCode

        private final int fgCode
    • Constructor Detail

      • TerminalColor

        private TerminalColor​(int fgCode)
    • Method Detail

      • values

        public static TerminalColor[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (TerminalColor c : TerminalColor.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static TerminalColor valueOf​(java.lang.String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
        Parameters:
        name - the name of the enum constant to be returned.
        Returns:
        the enum constant with the specified name
        Throws:
        java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
        java.lang.NullPointerException - if the argument is null
      • getEscape

        private java.lang.String getEscape​(boolean bright,
                                           boolean background,
                                           boolean bold)
      • getCode

        private int getCode​(boolean bright,
                            boolean background)
      • apply

        public java.lang.String apply​(java.lang.String text,
                                      boolean bright,
                                      boolean background,
                                      boolean bold)
        Style the given string with this color. All text attributes are reset at the end.
        Parameters:
        text - Text to color
        bright - Whether to use a bright version of this color
        background - Whether to set the color on the text background rather than on the color
        bold - Whether to set the text as bold
        Returns:
        A string surrounded by escape sequences
      • style

        public static java.lang.String style​(java.lang.String text,
                                             TerminalColor foreground,
                                             TerminalColor background,
                                             boolean brightFg,
                                             boolean brightBg,
                                             boolean boldText)
        Style both foreground and background. Returns a new string containing the parameter, which uses this ANSI code at the beginning, and resets all text attributes at the end.
        Parameters:
        text - Text to color
        foreground - Text color
        background - Background color
        brightFg - Whether text color is bright
        brightBg - Whether background color is bright
        boldText - Whether to set the text as bold
        Returns:
        A string surrounded by escape sequences