Color
Color
This module exposes functionality to format Str
values with ansi escape
codes using Select Graphic Rendition (SGR)
to format text on the terminal. For usage, check the examples!
Further reading:
- https://graphcomp.com/info/specs/ansi_col.html
- https://github.com/termstandard/colors
- https://www.hackitu.de/termcolor256/
- https://chrisyeh96.github.io/2020/03/28/terminal-colors.html
Color
Type that represents a color code for formatting.
Use ansi
, color256bit
or rgb
to construct it.
DisplayAttribute : [ Reset, Bold, Dim, Italic, Underscore, Blink, Reverse, Hidden ]
Display attributes for SGR.
When using formatWith
you should use the lowercase variants exported
by this module (see the documentation for formatWith
).
Please note that Italic
is not officially specified for SGR but many
terminals support it. Bold
is sometimes interpreted as "bright".
Most terminals however format the text as bold.
AnsiColor : [ Black, Blue, Cyan, Green, Magenta, Red, White, Yellow ]
SGR colors.
These colors are most likely to work with most terminals. Use ansi
to
turn them into a Color
or use the already exported constants provided
by this module.
formatWith : List [ Display DisplayAttribute, Foreground Color, Background Color ], Str -> Str
Wraps the string argument with a SGR that reflects the attributes and colors in the input list and a reset for SGR after it. When printing the resulting string to the terminal, it will be styled acoordingly assuming the terminal supports the chosen display attributes and colors.
Usage
You are meant to import this function and your desired attributes by
exposing
them, so the syntax is less cluttered.
import color.Color exposing [formatWith, bold, italic, underscore, rgb, blue, whiteBg, foreground] main = line1 = formatWith [bold, blue, whiteBg] "foobar" Stdout.line! "My first text: $(line1)" line2 = formatWith [italic, underscore, foreground (rgb 25 233 143)] "barfoo" Stdout.line! "My second text: $(line2)"
Assuming that at most a single Foreground
and Background
color is present
in the list, the order of elements is not relevant. If multiple Foreground
and/or Background
values are present, it's undefined which and if colors
will be chosen.
selectGraphicRendition
Generate a single SGR with the attributes given in a record of optional values. The type of this function is:
selectGraphicRendition : { attrs ? List DisplayAttribute, bgColor ? Color, fgColor ? Color } -> Str
resetGraphicRendition : Str
String to reset the SGR.
This is equivalent to selectGraphicRendition {attrs: [Reset]}
.
ansi : AnsiColor -> Color
Turns an AnsiColor
into a Color
. It's most likely that your targeted
terminals support this color type.
color256bit : U8 -> Color
Turns a byte into a 256 bit color. Please note that this color type might
not be supported by your targeted terminals. If you don't need fine-grained
control of colors you should stick with AnsiColor
values.
rgb : U8, U8, U8 -> Color
Turns three bytes into a Truecolor RGB color. Please note that this color
type might not be supported by your targeted terminals. If you don't need
fine-grained control of colors you should stick with AnsiColor
values.
rgbHex : Str -> Color
Interprets a hexadecimal color as a Truecolor RGB color. If the supplied string fails to parse, the color will not influence formatting.
Format
#rrggbb
or #rgb
The string must contain 3 bytes in hexadecimal format (00-FF).
Alternatively, the string can only consist of three characters (0-F) that
describe the individual bytes to be made up of the specified character
twice, meaning that F
turns into FF
and 5
turns into 55
.
Letters can be uppercase or lowercase. The string can be prefixed with an
optional #
.
Examples
"#000"
results in black color.
"#1E90FF"
is an RGB value of (30, 144, 255).
foreground : Color -> [Foreground Color]
Marks a Color
to be used for the foreground (the text color).
background : Color -> [Background Color]
Marks a Color
to be used for the background.