AnsiTextLite

A lightweight alternative to AnsiText which only supports a singular coloured string, at the cost of removing most of the other complexity & dynamic allocation needs of AnsiText.

If you only need to style your string in one certain way, or want to avoid AnsiText altogether, then this struct is the way to go.

Usage_(Manually): First, retrieve and the ANSI styling sequence via AnsiTextLite.toFullStartSequence and output it.

Second, output AnsiTextLite.text.

Finally, and optionally, retrieve the ANSI reset sequence via AnsiTextLite.toFullEndSequence and output it.

Usage_(Range): Call AnsiTextLite.toRange to get the range, please read its documentation as it is important (it'll return slices to stack-allocated memory).

Usage_(GC): If you're not compiling under -betterc, then AnsiTextLite.toString() will provide you with a GC-allocated string containing: the start ANSI sequence; the text to display; and the end ANSI sequence. i.e. A string that is just ready to be printed.

This struct also implements the sink-based version of toString, which means that when used directly with things like writeln, this struct is able to avoid allocations (unless the sink itself allocates). See the unittest for an example.

Members

Functions

bg
AnsiTextLite bg(AnsiColour colour)
bg
AnsiTextLite bg(Ansi4BitColour colour)
bg
AnsiTextLite bg(Ansi8BitColour colour)
bg
AnsiTextLite bg(AnsiRgbColour colour)
bg
AnsiColour bg()
fg
AnsiTextLite fg(AnsiColour colour)
fg
AnsiTextLite fg(Ansi4BitColour colour)
fg
AnsiTextLite fg(Ansi8BitColour colour)
fg
AnsiTextLite fg(AnsiRgbColour colour)
fg
AnsiColour fg()
style
AnsiTextLite style(AnsiStyle style)
style
AnsiStyle style()
toFullEndSequence
char[ANSI_COLOUR_RESET.length] toFullEndSequence()
toFullStartSequence
char[] toFullStartSequence(char[MAX_CHARS_NEEDED] buffer)

Populates the given buffer with the full ANSI sequence needed to enable the styling defined within this AnsiTextLite

toRange
auto toRange()

Provides a range that returns, in this order: The start sequence (.toFullStartSequence); the output text (.text), and finally the end sequence (.toFullEndSequence).

toSink
void toSink(Sink sink)

Outputs in order: The start sequence (.toFullStartSequence), the output text (.text), and the end sequence (.toFullEndSequence) into the given sink.

toString
string toString()

Notes: This struct implements the sink-based toString which performs no allocations, so the likes of std.stdio.writeln will automatically use the sink-based version if you pass this struct to it directly.

toString
void toString(void delegate(const(char)[]) sink)

The sink-based version of toString, which doesn't allocate by itself unless the sink decides to allocate.

Manifest constants

MAX_CHARS_NEEDED
enum MAX_CHARS_NEEDED;

The maximum amount of chars required by the start sequence of an AnsiTextLite (toFullStartSequence).

Variables

styleSet
AnsiStyleSet styleSet;

The styling to apply to the text.

text
const(char)[] text;

The text to output.

Examples

1 static if(!BetterC)
2 {
3     auto text = "Hello!".ansi
4                         .fg(Ansi4BitColour.green)
5                         .bg(AnsiRgbColour(128, 128, 128))
6                         .style(AnsiStyle.init.bold.underline);
7 
8     // Usage 1: Manually
9     import core.stdc.stdio : printf;
10     import std.stdio : writeln, write;
11     version(JANSI_TestOutput) // Just so test output isn't clogged. This still shows you how to use things though.
12     {
13         char[AnsiTextLite.MAX_CHARS_NEEDED + 1] startSequence; // + 1 for null terminator.
14         const sliceFromStartSequence = text.toFullStartSequence(startSequence[0..AnsiTextLite.MAX_CHARS_NEEDED]);
15         startSequence[sliceFromStartSequence.length] = '\0';
16 
17         char[200] textBuffer;
18         textBuffer[0..text.text.length] = text.text[];
19         textBuffer[text.text.length] = '\0';
20 
21         char[ANSI_COLOUR_RESET.length + 1] endSequence;
22         endSequence[0..ANSI_COLOUR_RESET.length] = text.toFullEndSequence()[];
23         endSequence[$-1] = '\0';
24 
25         printf("%s%s%s\n", startSequence.ptr, textBuffer.ptr, endSequence.ptr);
26     }
27 
28     // Usage 2: Range (RETURNS STACK MEMORY, DO NOT ALLOW SLICES TO OUTLIVE RANGE OBJECT WITHOUT EXPLICIT COPY)
29     version(JANSI_TestOutput)
30     {
31         // -betterC
32         foreach(slice; text.toRange)
33         {
34             char[200] buffer;
35             buffer[0..slice.length] = slice[];
36             buffer[slice.length] = '\0';
37             printf("%s", buffer.ptr);
38         }
39         printf("\n");
40 
41         // GC
42         foreach(slice; text.toRange)
43             write(slice);
44         writeln();
45     }
46 
47     // Usage 3: toString (Sink-based, so AnsiTextLite doesn't allocate, but writeln/the sink might)
48     version(JANSI_TestOutput)
49     {
50         writeln(text); // Calls the sink-based .toString();
51     }
52 
53     // Usage 4: toString (non-sink, non-betterc only)
54     version(JANSI_TestOutput)
55     {
56         writeln(text.toString());
57     }
58 
59     // Usage 5: toSink
60     version(JANSI_TestOutput)
61     {
62         struct CustomOutputRange
63         {
64             char[] output;
65             @safe
66             void put(const(char)[] slice) nothrow
67             {
68                 const start = output.length;
69                 output.length += slice.length;
70                 output[start..$] = slice[];
71             }
72         }
73 
74         CustomOutputRange sink;
75         ()@safe nothrow{ text.toSink(sink); }();
76 
77         writeln(sink.output);
78     }
79 }

See Also

ansi for fluent creation of an AnsiTextLite.

This struct's unittest for an example of usage.

Meta