import

ANSI color and style library

This is a simple library for applying styles to terminal code using ANSI escape sequences.

This library can be used simply by importing it.

import color

Applying sets of styles

A set of styles can be enabled by passing them as arguments to the style function. All styles will be disabled with the unstyle function.

import color using style unstyle BOLD ITALIC

style $BOLD
echo this text will be bold
unstyle

echo "This $(style $BOLD $ITALIC)word$(unstyle) will be bold"

A style can be applied to a single string using span.

import color using style unstyle span BOLD ITALIC

style $BOLD
echo this text will be bold
unstyle

echo "This $(span $BOLD $ITALIC "word") will be bold"

Font weight

The font weight can be made heavier using the $BOLD style and lighter using the $FAINT style. $DIM is an alias for $FAINT.

The $MEDIUM style reset the font weight.

Font alternatives

Underlines

Alternative fonts

Note: This is not well supported.

The font function can be used to access one of the 10 alternative fonts. The default font is font 0.

echo "The word $(span $(font 2) "this") will be different"

Overlines

Note: This is not well supported.

Crossed-out text

Note: This is not well supported.

Blinking text

Hiding text

Frames

Note: This is not well supported.

Color inversion

Color styles

Simple colors

Many common 4-bit colors are defined with styles of the form <COLOR>_FG to set the foreground color and <COLOR>_BG to set the background color.

The available colors are:

Greys

Styles that use one of 24 values of grey can be generated with the grey_fg and grey_bg functions, setting the foreground and background colors respectively.

echo $(span $(grey_fg 2) "dark grey")
echo $(span $(grey_fg 22) "light grey")

8-bit color

Styles that use 8-bit color values can be created with the rgb8_fg and rgb8_bg functions, setting the foreground and background colors respectively.

Each of these functions takes 3 arguments, each in the range [0..6) for the channels red, green, and blue.

echo $(span $(rgb8_fg 3 1 5) "purple")

24-bit color

Styles that use 24-bit color values can be created with the rgb24_fg and rgb24_bg functions, setting the foreground and background colors respectively.

Each of these functions takes 3 arguments, each in the range [0..256) for the channels red, green, and blue.

echo $(span $(rgb24_fg 160 80 192) "purple")