1
0
mirror of https://github.com/deavmi/birchwood synced 2024-09-20 15:22:53 +02:00

Edit colors and add more control codes

This commit is contained in:
supremestdoggo 2023-03-13 14:58:49 -04:00
parent dcf32b2a13
commit 464773daa8
2 changed files with 21 additions and 8 deletions

View File

@ -2,12 +2,16 @@ module birchwood.formatting
import std.string; import std.string;
// Reset character; resets all formatting
enum reset = '\x0F';
// Toggle characters // Toggle characters
enum bold = '\x02'; enum bold = '\x02';
enum italic = '\x1D'; enum italic = '\x1D';
enum underline = '\x1F'; enum underline = '\x1F';
enum strikethrough = '\x1E'; enum strikethrough = '\x1E';
enum monospace = '\x11'; enum monospace = '\x11';
enum reverse_colors = '\x16'; // NOT UNIVERSALLY SUPPORTED
// Color characters // Color characters
enum ascii_color_code = '\x03'; enum ascii_color_code = '\x03';
@ -34,15 +38,23 @@ enum simple_colors: string {
DEFAULT = "99" // NOT UNIVERSALLY SUPPORTED DEFAULT = "99" // NOT UNIVERSALLY SUPPORTED
} }
// Generates a string that changes the foreground color // Return the hex control character if color is a hexadecimal color code, the ASCII control character if color is two ASCII digits, and throw an exception if it's neither
string set_foreground(string color) { char generate_color_control_char(string color) {
char[1] control_char;
if (color.length == 6) { if (color.length == 6) {
control_char[0] = hex_color_code; return hex_color_code;
} else if (color.length == 2) { } else if (color.length == 2) {
control_char[0] = ascii_color_code; return ascii_color_code;
} else { } else {
throw new StringException("Invalid color code (must be either two ASCII digits or a hexadecimal code of the form RRGGBB)"); throw new StringException("Invalid color code (must be either two ASCII digits or a hexadecimal code of the form RRGGBB)");
} }
return control_char.idup ~ color;
} }
// Generate a string that sets the foreground color
string set_fg(string color) {return [generate_color_control_char(color)].idup ~ color;}
// Generate a string that sets the foreground and background color
string set_fg_bg(string color) {return [generate_color_control_char(color)].idup ~ color ~ "," ~ color;}
// Generate a string that resets the foreground and background colors
pragma(inline)
string reset_fg_bg() {return "\x03";}

View File

@ -3,3 +3,4 @@ module birchwood;
public import birchwood.client; public import birchwood.client;
public import birchwood.config; public import birchwood.config;
public import birchwood.protocol; public import birchwood.protocol;
public import birchwood.formatting