Populates the given buffer with the full ANSI sequence needed to enable the styling defined within this AnsiTextLite
Provides a range that returns, in this order: The start sequence (.toFullStartSequence); the output text (.text), and finally the end sequence (.toFullEndSequence).
Outputs in order: The start sequence (.toFullStartSequence), the output text (.text), and the end sequence (.toFullEndSequence) into the given sink.
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.
The sink-based version of toString, which doesn't allocate by itself unless the sink decides to allocate.
The maximum amount of chars required by the start sequence of an AnsiTextLite (toFullStartSequence).
The styling to apply to the text.
The text to output.
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 }
ansi for fluent creation of an AnsiTextLite.
This struct's unittest for an example of usage.
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.