Package Torello.Java

Class Shell.C

  • Enclosing class:
    Shell

    public static class Shell.C
    extends java.lang.Object
    Shell.C (UNIX Color-Codes) Documentation.

    This class is particularly usable if you are logged into the Google Cloud Server "Cloud Shell" interface for writing your Java Code. The instance of Bourne Again Shell that is provided allows these colors to display quite well. Generally these escape color codes ought to be usable by any UNIX-related terminal output. Only the oldest terminals still use only green.

    IMPORTANT: These colors to do not easily translate on a Windows, DOS or Apple Computer Terminal Output Display. If you load this Java-HTML package on a terminal or device that is not a UNIX system, this class will automatically default every one of these character strings to the empty, zero-length, string. This is to facilitate / guarantee that people who write UNIX-specific code will have their terminal-output displayed properly when their write-once, run-anywhere Java methods are run on non-UNIX platforms.


    Question from StackOverflow.com

    How to print color in console using System.out.println?

    How can I print color in console? I want to show data in colors when the processor sends data and in different colors when it receives data



    If your terminal supports it, you can use ANSI escape codes to use color in your output. It generally works for Unix shell prompts; however, it doesn't work for Windows Command Prompt (Although, it does work for Cygwin). For example, you could define constants like these for the colors. Then, you could reference those as necessary. For example, using the above constants, you could make the following red text output on supported terminals:

    Example:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    import Torello.Java.Shell.C;
    
    // On a UNIX Terminal, this will print in color=red.
    // On a non-UNIX terminal, these fields will evaluate to an empty-string, and the color-codes will be ignored.
    // NOTE: Macintosh and MS-DOS do not read UNIX Color-Codes.
    System.out.println(C.RED + "This text is red!" + C.RESET);
    
    // Also, if you wish to change the background color of the text, the following will work:
    System.out.println(C.GREEN_BKGND + "This text has a green background but default text!" + C.RESET);
    System.out.println(C.RED + "This text has red text but a default background!" + C.RESET);
    System.out.println(C.GREEN_BKGND + C.RED + "This text has a green background and red text!" + C.RESET);
    



    • Field Detail

      • RESET

        public static final java.lang.String RESET
        Code:
        Exact Field Declaration Expression:
        1
        public static final String RESET            = unix ? "\u001B[0m"  : "";
        
      • BLACK

        public static final java.lang.String BLACK
        Code:
        Exact Field Declaration Expression:
        1
        public static final String BLACK            = unix ? "\u001B[30m" : "";
        
      • RED

        public static final java.lang.String RED
        Code:
        Exact Field Declaration Expression:
        1
        public static final String RED              = unix ? "\u001B[31m" : "";
        
      • GREEN

        public static final java.lang.String GREEN
        Code:
        Exact Field Declaration Expression:
        1
        public static final String GREEN            = unix ? "\u001B[32m" : "";
        
      • YELLOW

        public static final java.lang.String YELLOW
        Code:
        Exact Field Declaration Expression:
        1
        public static final String YELLOW           = unix ? "\u001B[33m" : "";
        
      • BLUE

        public static final java.lang.String BLUE
        Code:
        Exact Field Declaration Expression:
        1
        public static final String BLUE             = unix ? "\u001B[34m" : "";
        
      • PURPLE

        public static final java.lang.String PURPLE
        Code:
        Exact Field Declaration Expression:
        1
        public static final String PURPLE           = unix ? "\u001B[35m" : "";
        
      • CYAN

        public static final java.lang.String CYAN
        Code:
        Exact Field Declaration Expression:
        1
        public static final String CYAN             = unix ? "\u001B[36m" : "";
        
      • WHITE

        public static final java.lang.String WHITE
        Code:
        Exact Field Declaration Expression:
        1
        public static final String WHITE            = unix ? "\u001B[37m" : "";
        
      • BLACK_BKGND

        public static final java.lang.String BLACK_BKGND
        Code:
        Exact Field Declaration Expression:
        1
        public static final String BLACK_BKGND      = unix ? "\u001B[40m" : "";
        
      • RED_BKGND

        public static final java.lang.String RED_BKGND
        Code:
        Exact Field Declaration Expression:
        1
        public static final String RED_BKGND        = unix ? "\u001B[41m" : "";
        
      • GREEN_BKGND

        public static final java.lang.String GREEN_BKGND
        Code:
        Exact Field Declaration Expression:
        1
        public static final String GREEN_BKGND      = unix ? "\u001B[42m" : "";
        
      • YELLOW_BKGND

        public static final java.lang.String YELLOW_BKGND
        Code:
        Exact Field Declaration Expression:
        1
        public static final String YELLOW_BKGND     = unix ? "\u001B[43m" : "";
        
      • BLUE_BKGND

        public static final java.lang.String BLUE_BKGND
        Code:
        Exact Field Declaration Expression:
        1
        public static final String BLUE_BKGND       = unix ? "\u001B[44m" : "";
        
      • PURPLE_BKGND

        public static final java.lang.String PURPLE_BKGND
        Code:
        Exact Field Declaration Expression:
        1
        public static final String PURPLE_BKGND     = unix ? "\u001B[45m" : "";
        
      • CYAN_BKGND

        public static final java.lang.String CYAN_BKGND
        Code:
        Exact Field Declaration Expression:
        1
        public static final String CYAN_BKGND       = unix ? "\u001B[46m" : "";
        
      • WHITE_BKGND

        public static final java.lang.String WHITE_BKGND
        Code:
        Exact Field Declaration Expression:
        1
        public static final String WHITE_BKGND      = unix ? "\u001B[47m" : "";
        
      • BBLACK

        public static final java.lang.String BBLACK
        Code:
        Exact Field Declaration Expression:
        1
        public static final String BBLACK           = unix ? "\u001B[90m" : "";
        
      • BRED

        public static final java.lang.String BRED
        Code:
        Exact Field Declaration Expression:
        1
        public static final String BRED             = unix ? "\u001B[91m" : "";
        
      • BGREEN

        public static final java.lang.String BGREEN
        Code:
        Exact Field Declaration Expression:
        1
        public static final String BGREEN           = unix ? "\u001B[92m" : "";
        
      • BYELLOW

        public static final java.lang.String BYELLOW
        Code:
        Exact Field Declaration Expression:
        1
        public static final String BYELLOW          = unix ? "\u001B[93m" : "";
        
      • BBLUE

        public static final java.lang.String BBLUE
        Code:
        Exact Field Declaration Expression:
        1
        public static final String BBLUE            = unix ? "\u001B[94m" : "";
        
      • BPURPLE

        public static final java.lang.String BPURPLE
        Code:
        Exact Field Declaration Expression:
        1
        public static final String BPURPLE          = unix ? "\u001B[95m" : "";
        
      • BCYAN

        public static final java.lang.String BCYAN
        Code:
        Exact Field Declaration Expression:
        1
        public static final String BCYAN            = unix ? "\u001B[96m" : "";
        
      • BWHITE

        public static final java.lang.String BWHITE
        Code:
        Exact Field Declaration Expression:
        1
        public static final String BWHITE           = unix ? "\u001B[97m" : "";
        
      • BBLACK_BKGND

        public static final java.lang.String BBLACK_BKGND
        Code:
        Exact Field Declaration Expression:
        1
        public static final String BBLACK_BKGND     = unix ? "\u001B[100m" : "";
        
      • BRED_BKGND

        public static final java.lang.String BRED_BKGND
        Code:
        Exact Field Declaration Expression:
        1
        public static final String BRED_BKGND       = unix ? "\u001B[101m" : "";
        
      • BGREEN_BKGND

        public static final java.lang.String BGREEN_BKGND
        Code:
        Exact Field Declaration Expression:
        1
        public static final String BGREEN_BKGND     = unix ? "\u001B[102m" : "";
        
      • BYELLOW_BKGND

        public static final java.lang.String BYELLOW_BKGND
        Code:
        Exact Field Declaration Expression:
        1
        public static final String BYELLOW_BKGND    = unix ? "\u001B[103m" : "";
        
      • BBLUE_BKGND

        public static final java.lang.String BBLUE_BKGND
        Code:
        Exact Field Declaration Expression:
        1
        public static final String BBLUE_BKGND      = unix ? "\u001B[104m" : "";
        
      • BPURPLE_BKGND

        public static final java.lang.String BPURPLE_BKGND
        Code:
        Exact Field Declaration Expression:
        1
        public static final String BPURPLE_BKGND    = unix ? "\u001B[105m" : "";
        
      • BCYAN_BKGND

        public static final java.lang.String BCYAN_BKGND
        Code:
        Exact Field Declaration Expression:
        1
        public static final String BCYAN_BKGND      = unix ? "\u001B[106m" : "";
        
      • BWHITE_BKGND

        public static final java.lang.String BWHITE_BKGND
        Code:
        Exact Field Declaration Expression:
        1
        public static final String BWHITE_BKGND     = unix ? "\u001B[107m" : "";
        
    • Method Detail

      • toHTML

        public static java.lang.String toHTML​(java.lang.String text,
                                              boolean preFormat,
                                              boolean escapeHTMLElements,
                                              boolean useCSSClasses)
        Converts the instances of these escape-sequences that are found inside of Java Strings that were generated using these ANSI UNIX color escape sequences, and produces a valid HTML string that contains HTML <SPAN STYLE="color-information"> replacements!

        NOTE: Any new-line characters sequences such as '\n' '\r\n' will be replaces with HTML <BR /> elements.
        Parameters:
        text - This should be any string, usually one that is saved from a 'StorageWriter', although any text that includes these UNIX Color Escape Codes is fine.
        preFormat - When this parameter receives FALSE, everywhere in the input text-string that a CRLF (new-line) occurs, that newline will be replaced by an HTML <BR /> element, in addition to the original newline.
        escapeHTMLElements - Whenever HTML is sent to the input-parameter 'text' - if the intention is to render the HTML using the browser, this parameter should be FALSE. If it is intended to allow the UI to "show the HTML" like it were text-to-be-viewed, then each and every greater-than-symbol '>' and also every less-than-symbol '<' will be escaped. This is done to prevent the browser from trying to parse the text as HTML.
        useCSSClasses - When this parameter receives TRUE, all returned <SPAN STYLE=...> elements shall be converted to using a simplified CSS Class Name. The CLASS definitions for the returned String can be retrieved by simply calling the method: getCSSDefinitions().

        IMPORTANT: If this parameter does receive a TRUE value, it is imperitive to include the CSS STYLE definitions that are returned by the above mentioned method, or else the colors shall not be visible.
        Returns:
        Every UNIX-ANSI color escape-sequence that is found/identified in this text will be replaced with an HTML <SPAN STYLE="color: a-color; background: a-background-color"> element.
        Code:
        Exact Method Body:
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
         // If the input text, itself, has HTML elements, then those have to be "escaped" to
         // properly render.  If the intention was to have them rendered has HTML Elements
         // (not text), then this boolean should be false.
         if (escapeHTMLElements) text = text.replace("<", "&lt;").replace(">", "&gt;");
        
         // With "Pre-Formatted Text" - there is no need to add "<BR />" where line-breaks
         // occur CRLF will automatically be inserted courtesy of the browser
         if (! preFormat)        text = text.replaceAll("\n|\r\n|\r", "<BR />\n");
        
         return StrReplace.r(
             text, charCodesArr, useCSSClasses 
                 ? htmlSpansCSSClasses 
                 : htmlSpansStyleAttributes
         );
        
      • getCSSDefinitions

        public static java.lang.String getCSSDefinitions()
        If the 'useCSSDefinitions' option is selected with the toHTML(java.lang.String,boolean,boolean,boolean) method, then the String returned from this method shall provide the CSS Style definitions needed to use the colors provided by toHTML(...)
        Returns:
        This shall visit the internal data-files for this JAR distribution, and return a list of CSS Style definitions that will colorize the HTML produced by an invocation of toHTML().
        Code:
        Exact Method Body:
        1
         return LFEC.readObjectFromFile_JAR(Torello.Data.DataFileLoader.class, CSS_DEFINITIONS_FILE, true, String.class);