Formating Text For Terminals.
Ansi Fmt gives bare-bone wrapper around Ansi Terminal Escape Codes.
Copy the files from the include
directory and place them as is.
The library provides two Macros
- z__ansi_fmt() Formats Text => Sets Color, Bold, Italic etc.
- z__ansi_scr() Handles Terminal Screen => Cursor Movement, Clearing Screen etc.
Takes a set of comma seperated Commands
in and Generates that String Literal of required ansi escape codes that formats text.
printf(
z__ansi_fmt((bold)) //Sets one Bold Text
"Text Here "
z__ansi_fmt((clrgb_fg, 254, 134, 0), (underline)) // Sets Foreground Color And Enable Underlines
"Another Text"
z__ansi_fmt((reset)) //Resets Changes, back to plain mode
);
The Arguments are expressed similar to Lisp's S-expression only difference being we use comma seperation instead of white-space.
These are the viable commands to be used in z__ansi_fmt
:
Turn On Bold Mode
Turn On Underline Mode
Turn On Dim Mode
Turn On Italic Mode
Turn On Reverse Mode
Turn On Blink Mode
Turn On Invisible Mode
Turn On Strike-through Mode.
Take in Literal Tokens
Disable an <option>
specified.
- <option>: Can be
bold
|italic
|dim
|reverse
|underline
|blink
|strike
|invisible
Set an Color out of Ansi specified 8bit colors
-
<foreground>: [0-255], Set the foreground color. Has to be literal number or a macro defined number. Variables will not work. But
%d
like formats can be used. -
<background>: [0-255], Set the background color. Has to be literal number or a macro defined number. Variables will not work. But
%d
like formats can be used.
Set True Colors
- <fg-red>: [0-255], Specify Foreground Red Variant
- <fg-green>: [0-255], Specify Foreground Green Variant
- <fg-blue>: [0-255], Specify Foreground Blue Variant
- <bg-red>: [0-255], Specify Background Red Variant
- <bg-green>: [0-255], Specify Background Green Variant
- <bg-blue>: [0-255], Specify Background Blue Variant
Only Set the Foreground Color in rgb.
- <red>: [0-255], Specify Red Variant
- <green>: [0-255], Specify Green Variant
- <blue>: [0-255], Specify Blue Variant
Only Set the Background Color in rgb.
- <red>: [0-255], Specify Red Variant
- <green>: [0-255], Specify Green Variant
- <blue>: [0-255], Specify Blue Variant
Set Back To Original/Default Text.
Same as (plain)
Takes a set of comma seperated Commands
in and Generates that String Literal of required ansi escape codes that formats Screen and Handles Cursor.
printf(
/* Clears Screen and Set the cursor at the Top-left corner*/
z__ansi_scr((clear), (jump))
);
Notation is similar to that of z__ansi_fmt
.
Toggle cursor; Show/Hides Cursor.
Clears Screen
Clears Line
Set High Mode
Set Low Mode
Toggle Alternate Buffer
Set Cursor At the Origin
Set Cursor to a specified location
- <row> [number], Specify row
- <column> [number], Specify column
Moves cursor up # lines
Moves cursor down # lines
Moves cursor left # columns
Moves cursor right # columns
Moves cursor down # lines. At the beginning
Moves cursor down # lines. At the beginning
Moves cursor to column #
Save cursor position
Restores the cursor to the last saved position
Save screen
Restore screen
Clears from cursor until end of screen
Clears from cursor until start of screen
Clears from cursor until end of line
Clears from cursor until start of screen
Changes the screen width or type to the mode specified by <value>
.
<value> |
Description |
---|---|
0 |
40 x 25 monochrome (text) |
1 |
40 x 25 color (text) |
2 |
80 x 25 monochrome (text) |
3 |
80 x 25 color (text) |
4 |
320 x 200 4-color (graphics) |
5 |
320 x 200 monochrome (graphics) |
6 |
640 x 200 monochrome (graphics) |
7 |
Enables line wrapping |
13 |
320 x 200 color (graphics) |
14 |
640 x 200 color (16-color graphics) |
15 |
640 x 350 monochrome (2-color graphics) |
16 |
640 x 350 color (16-color graphics) |
17 |
640 x 480 monochrome (2-color graphics) |
18 |
640 x 480 color (16-color graphics) |
19 |
320 x 200 color (256-color graphics) |
#include <stdio.h>
#include <stdlib.h>
#include "include/ansifmt.h"
int main()
{
fputs(z__ansi_scr(
(clear), // Clears Screen
(jump)), // Set Cursor At Top-left corner
stdout);
int number = random() &0xFFFF;
printf(z__ansi_fmt(
(cl256, 23, 254), // Set 8bit color
(bold)) // Turn on bold text
"Hello, World!\n"
z__ansi_fmt((no, bold)) // Disable Bold Text, color config is still intact.
"Number Generated => %d\n"
z__ansi_fmt(
(clrgb, 156, 0, 33, 234, 134, 0), //Set true color on (rgb)
(underline)) // Underlines Text
"Goodbye, World!"
z__ansi_fmt((reset)) "\n",
number);
}