diff --git a/source/birchwood/formatting.d b/source/birchwood/formatting.d index 975da7f..2fbbc39 100644 --- a/source/birchwood/formatting.d +++ b/source/birchwood/formatting.d @@ -2,12 +2,16 @@ module birchwood.formatting import std.string; +// Reset character; resets all formatting +enum reset = '\x0F'; + // Toggle characters enum bold = '\x02'; enum italic = '\x1D'; enum underline = '\x1F'; enum strikethrough = '\x1E'; enum monospace = '\x11'; +enum reverse_colors = '\x16'; // NOT UNIVERSALLY SUPPORTED // Color characters enum ascii_color_code = '\x03'; @@ -34,15 +38,23 @@ enum simple_colors: string { DEFAULT = "99" // NOT UNIVERSALLY SUPPORTED } -// Generates a string that changes the foreground color -string set_foreground(string color) { - char[1] control_char; +// 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 +char generate_color_control_char(string color) { if (color.length == 6) { - control_char[0] = hex_color_code; + return hex_color_code; } else if (color.length == 2) { - control_char[0] = ascii_color_code; + return ascii_color_code; } else { 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; -} \ No newline at end of file +} + +// 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";} \ No newline at end of file diff --git a/source/birchwood/package.d b/source/birchwood/package.d index d9018f1..b98ed1d 100644 --- a/source/birchwood/package.d +++ b/source/birchwood/package.d @@ -2,4 +2,5 @@ module birchwood; public import birchwood.client; public import birchwood.config; -public import birchwood.protocol; \ No newline at end of file +public import birchwood.protocol; +public import birchwood.formatting \ No newline at end of file