Class jm_Ansi
jm_Ansi - ANSI Coloring
Copyright (C) Jack Meng 2023ANSI 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 classStringBuilder
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 likejm_Ansi._ansi.toString(String)
. This introduces the pattern of format first, apply second:make().format().format()...toString(payload)
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 byjm_Ansi._ansi.content()
. To modify text you use cascading
calls within the jm_Ansi._ansi
class. Some of these methods include:
-
jm_Ansi._ansi.red()
- Makes the FOREGROUND of the content RED -
jm_Ansi._ansi.green()
- Makes the FOREGROUND of the content GREEN -
jm_Ansi._ansi.blue()
- Makes the FOREGROUND of the content BLUE -
jm_Ansi._ansi.bold()
- Makes the content appear BOLDED (high intensity)
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
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:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
This class is where all of the formatting happens. -
Method Summary
Modifier and TypeMethodDescriptionstatic long
_version()
The current versionstatic jm_Ansi._ansi
make()
Use this to grab an ANSI create instance.static jm_Ansi._ansi
Use this to grab an ANSI create instance.static String
reset()
Hard coded RESET the rendersstatic boolean
use_ansi()
The current state of using ANSI or notstatic void
use_ansi
(boolean e) Whether to use ANSI or not
-
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
Hard coded RESET the renders- Returns:
- "\033[0;m"
-
make
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
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
-