The MemoANSI format is a string-based syntax designed for styling text with various colors, backgrounds, and boldness attributes. This format is particularly useful for adding visual emphasis to log messages or console output.
A MemoANSI Expression begins with the @
symbol, followed by a sequence of codes that specify the text's style, and ends with {text}
, where text
is the styled content.
@<color><bold><background>{text}
- Start The color expression must start with
@
- Color Code: The first character after
@
specifies the foreground color of the text. - Boldness Flag (optional): An exclamation mark (
!
) after the color code makes the text bold. - Background Color Code (optional): The next character specifies the background color of the text.
- Text: Enclosed within
{}
brackets, this is the content to be styled.
graph LR
Start["@"] --> |"Normal Colors"| Normal["k,r,g,y,b,m,c,w"]
Start --> |"Bright Colors"| Bright["K,R,G,Y,B,M,C,W"]
Normal --> |"Bold"| Bold["!"]
Bright --> |"Bold"| Bold
Normal --> |"Skip Bold"| BG
Bright --> |"Skip Bold"| BG
Bold --> |"Background"| BG["k,r,g,y,b,m,c,w<br/>K,R,G,Y,B,M,C,W"]
Bold --> |"No Background"| Text
BG --> Text["{text}"]
BG --> Text
@r{red color}
: Text in red.@R{bright red color}
: Text in bright red.@r!{red bold color}
: Bold text in red.@rW{red on white background}
: Text in red on a white background.@r!W{red bold on white background}
: Bold text in red on a white background.@*{Rainbow text}
: Rainbow-styled text (special case).
Code | Color | Bright Variant Code | Bright Color |
---|---|---|---|
k | Black | K | Bright Black (Gray) |
r | Red | R | Bright Red |
g | Green | G | Bright Green |
y | Yellow | Y | Bright Yellow |
b | Blue | B | Bright Blue |
m | Magenta | M | Bright Magenta |
c | Cyan | C | Bright Cyan |
w | White | W | Bright White |
The same codes used for foreground colors can also be used for background colors when placed after the boldness flag (if any).
Below are some examples demonstrating how to use the MemoANSI format in a logging library:
slog.Info("Example of @r{red color}")
slog.Info("Example of @R{bright red color}")
slog.Info("Example of @r!{red bold color}")
slog.Info("Example of @rW{red on white background}")
slog.Info("Example of @r!W{red bold on white background}")
slog.Info("Example of @*{Rainbow text}")
The @*{text}
expression applies a "rainbow" effect to the text. Each character is styled with a different color from the spectrum. This effect does not support boldness or background colors.
- Every MemoANSI Expression must begin with
@
. - After the
@
symbol, there must be a valid color code. - The boldness flag (
!
) is optional and can only appear after the color code. - The last optional character before the
{
must be a valid color code if specifying a background color. - The opening
{
and closing}
must encapsulate the text to style.