View on GitHub

ansi_fmt

Formats Text Using Ansi Escape code with ease.

Ansi Fmt

Formating Text For Terminals.

About

Ansi Fmt gives bare-bone wrapper around Ansi Terminal Escape Codes.

Installation

Copy the files from the include directory and place them as is.

Usage

The library provides two Macros

z_ansi_fmt(<commands>...)

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.

<commands>

These are the viable commands to be used in z__ansi_fmt:

(bold)

Turn On Bold Mode

(underline)

Turn On Underline Mode

(dim)

Turn On Dim Mode

(italic)

Turn On Italic Mode

(reverse)

Turn On Reverse Mode

Turn On Blink Mode

(invisible)

Turn On Invisible Mode

(strike)

Turn On Strike-through Mode.

(lit)

Take in Literal Tokens

(no, <option>)

Disable an <option> specified.

(cl256, <foreground>, <background>)

Set an Color out of Ansi specified 8bit colors

(clrgb, <fg-red>, <fg-green>, <fg-blue>, <bg-red>, <bg-green>, <bg-blue>)

Set True Colors

(clrgb_fg, <red>, <green>, <blue>)

Only Set the Foreground Color in rgb.

(clrgb_bg, <red>, <green>, <blue>)

Only Set the Background Color in rgb.

(plain)

Set Back To Original/Default Text.

(reset)

Same as (plain)

z__ansi_scr(<commands>...)

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.

<commands>

(curs)

Toggle cursor; Show/Hides Cursor.

(clear)

Clears Screen

(clear_line)

Clears Line

(high)

Set High Mode

(low)

Set Low Mode

(alt_buff)

Toggle Alternate Buffer

(jump)

Set Cursor At the Origin

(jumpxy, <row>, <column>)

Set Cursor to a specified location

(up, <#>)

Moves cursor up # lines

(down, <#>)

Moves cursor down # lines

(left, <#>)

Moves cursor left # columns

(right, <#>)

Moves cursor right # columns

(up0, <#>)

Moves cursor down # lines. At the beginning

(down0, <#>)

Moves cursor down # lines. At the beginning

(column, <#>)

Moves cursor to column #

(save)

Save cursor position

(load)

Restores the cursor to the last saved position

(ssave)

Save screen

(sload)

Restore screen

(clr_toend)

Clears from cursor until end of screen

(clr_tostart)

Clears from cursor until start of screen

(clrln_toend)

Clears from cursor until end of line

(clrln_tostart)

Clears from cursor until start of screen

(mode_on, <value>) & (mode_off, <value>)

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)

Example

#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);

}

Output:
Outex