Class jm_Ansi

java.lang.Object
com.jackmeng.ansicolors.jm_Ansi

public final class jm_Ansi extends Object

jm_Ansi - ANSI Coloring

Copyright (C) Jack Meng 2023

ANSI provides most consoles with a coloring format that you can use to build colorful CLI Applications or just to be more pretty with DEBUG messages. This simple library helps you to create cascading calls for creating concise and readable ANSI display codes. You don't have to mess with making your own and potentially making it overburdened and verbose to use!

Use this project in your code for displaying colorful console messages! All you have to is call make(String) and then you have a whole bunch of formatting options to utilize :)

You can also use functions similar to StringBuilder's class but however with a very limited subset of the methods available to you. It is limited because in order to create a cascading call pattern, certain functions that must return a type other than the cascading type will destroy cascading. However, some of them can be accessed like String.charAt(int) by calling the jm_Ansi._ansi.content() method which will return the String representation of the content for you to mess with. Other than that, all other methods will strictly return the current _ansi instance to continue the cascading pattern.

Simple usage guide

Everything is based off of the concept of cascading. You might have seen this with the class StringBuilder which you can constantly call methods like StringBuilder.append(String) and StringBuilder.insert(int, String) in a long chain like
newStringBuilder("Hello").append("World").append("!"). This is the exact concept used with jm_Ansi!
To start your cascade, you first must acquire an instance from jm_Ansi which you can do via two methods:
  • make(String) - Supplies the payload (AKA the string you want to format first). This introduces the pattern of apply first, format second: make(payload).format().format()...toString()
  • make() - Intended for the programmer to supply the payload later via a method like jm_Ansi._ansi.toString(String). This introduces the pattern of format first, apply second: make().format().format()...toString(payload)
Whichever one you use does not effect anything as both are interchangeable. It all comes down to personal preferences on how you like to order your code :).
To print the message to say System.out, you can just put it like so:
System.out.print(make().format().format().format() as the inbuilt jm_Ansi._ansi.toString() takes care of how to handle printing. However, this provides an automatic RESET character which makes sure the formatted content is only applied to that portion of the text. If you want to leave out this automatic RESET character, don't use these methods: Instead you should use jm_Ansi._ansi.render()

Formatting

Formatting is the way to modify the content which is given by jm_Ansi._ansi.content(). To modify text you use cascading calls within the jm_Ansi._ansi class. Some of these methods include: You also notice that certain colored functions like jm_Ansi._ansi.magenta_fg() or jm_Ansi._ansi.yellow_bg() has a suffix like _fg and _bg. These are naming schemes and are followed by for all colors:
  • _fg or color with no suffix - FOREGROUND coloring
  • _bg - BACKGROUND coloring
[!] You should always keep in mind that different platforms support different formatting looks. Sometimes they don't support formatting at all!

Example Usages

  • Printing RED Spectrum (RGB)

     for (int i = 0; i <= 255; i++)
       System.out.print(jm_Ansi.make(" ").rgb_bg(i, 0, 0));
     

  • Printing a Bolded Warning Text

     System.out.println(
         jm_Ansi.make("[!]").yellow_bg().black_bg().blink_fast() + " " + jm_Ansi.make("FAILED").bold().yellow_fg());
     

Version:
1.0
Author:
Jack Meng
See Also:
  • Method Details

    • _version

      public static long _version()
      The current version
      Returns:
      A long in the format of YYYYMMDDVV. Where YYYY -> Four digit year, MM -> 2 digit month number, DD -> 2 digit day number, VV -> for version numbering like 1.1 would be "11"
    • use_ansi

      public static void use_ansi(boolean e)
      Whether to use ANSI or not
      Parameters:
      e - true = on, false = off
    • use_ansi

      public static boolean use_ansi()
      The current state of using ANSI or not
      Returns:
      To use ansi or not
    • reset

      public static String reset()
      Hard coded RESET the renders
      Returns:
      "\033[0;m"
    • make

      public static jm_Ansi._ansi make(String content)
      Use this to grab an ANSI create instance. Primarily used if you want to submit your payload before formatting such that the semantics are like so: "make(content).color1().bold1()...toString()"

      Which one you use is up to personal preferences, there are no differences except for looks (or is it?). Contrary to make()

      Returns:
      An ANSI create instance
    • make

      public static jm_Ansi._ansi make()
      Use this to grab an ANSI create instance. Primarily used if you want to submit your payload AFTER formatting such that the semantics are like so: "make().color1().bold1()...toString(content)"

      Which one you use is up to personal preferences, there are no differences except for looks (or is it?). Contrary to make(String)

      Returns:
      An ANSI create instance