From 8fccde71e22ae6f04df9748d38a14144a8576783 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sat, 18 Mar 2023 13:00:03 +0200 Subject: [PATCH 01/20] Formatting - Added documentation for module --- source/birchwood/protocol/formatting.d | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/birchwood/protocol/formatting.d b/source/birchwood/protocol/formatting.d index a3fb7b7..2d948b7 100644 --- a/source/birchwood/protocol/formatting.d +++ b/source/birchwood/protocol/formatting.d @@ -1,3 +1,6 @@ +/** + * Message formatting utilities + */ module birchwood.protocol.formatting; import std.string; From 3dc85953275d089fdf9435612fddf648de42fd47 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sat, 18 Mar 2023 13:02:56 +0200 Subject: [PATCH 02/20] README - Added credits to supremestdoggo --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index e0aa557..8c1f313 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,11 @@ You can take a look at the `Client` API documentation on [DUB](https://birchwood More standards will be added within the next month or so, mostly relating to new response codes that just need to be added. +## Credits + +* [supremestdoggo](https://github.com/supremestdoggo) + * Adding IRC message formatting code (`267f8886150dbd40eb5c2fecb9d7e3a4c8dfd71f`) + ## License See [LICENSE](LICENSE). From 71d30039daf75d7a9846bdb424780f20b37004ea Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sat, 18 Mar 2023 13:05:57 +0200 Subject: [PATCH 03/20] Formatting - Changed name of enum `simpleColor` to `SimpleColor` to adhere to D naming conventions --- source/birchwood/protocol/formatting.d | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/birchwood/protocol/formatting.d b/source/birchwood/protocol/formatting.d index 2d948b7..0c82870 100644 --- a/source/birchwood/protocol/formatting.d +++ b/source/birchwood/protocol/formatting.d @@ -21,7 +21,7 @@ enum ascii_color_code = '\x03'; enum hex_color_code = '\x04'; // Simple color codes -enum simpleColor: string { +enum SimpleColor: string { WHITE = "00", BLACK = "01", BLUE = "02", @@ -84,13 +84,13 @@ string set_foreground_background(string fg, string bg) { // Generates a string that changes the foreground color (except enum) pragma(inline) -string set_foreground(simpleColor color) { +string set_foreground(SimpleColor color) { return ascii_color_code ~ color; } // Generate a string that sets the foreground and background color (except enum) pragma(inline) -string set_foreground_background(simpleColor fg, simpleColor bg) { +string set_foreground_background(SimpleColor fg, SimpleColor bg) { return ascii_color_code ~ fg ~ "," ~ bg; } From 208cfebf49f8e210a115bc320af11fd4d7663b2b Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sun, 19 Mar 2023 11:58:44 +0200 Subject: [PATCH 04/20] Formatting - Added TODO for import `std.string` - Added documentation to `SimpleColor` enum - Added documentation and code-styled `reset_fg_bg()`, `bold(string)`, `italics(string)`, `underline(string)`, `strikethrough(string)` and `monospace(string)` --- source/birchwood/protocol/formatting.d | 90 +++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 10 deletions(-) diff --git a/source/birchwood/protocol/formatting.d b/source/birchwood/protocol/formatting.d index 0c82870..a4c415a 100644 --- a/source/birchwood/protocol/formatting.d +++ b/source/birchwood/protocol/formatting.d @@ -3,6 +3,7 @@ */ module birchwood.protocol.formatting; +// FIXME: Remove below import and start using BirchwoodException import std.string; // Reset character; resets all formatting @@ -20,8 +21,10 @@ enum reverse_colors_code = '\x16'; // NOT UNIVERSALLY SUPPORTED enum ascii_color_code = '\x03'; enum hex_color_code = '\x04'; -// Simple color codes -enum SimpleColor: string { +/** + * Simple color codes + */ +public enum SimpleColor: string { WHITE = "00", BLACK = "01", BLUE = "02", @@ -94,22 +97,89 @@ string set_foreground_background(SimpleColor fg, SimpleColor bg) { return ascii_color_code ~ fg ~ "," ~ bg; } -// Generate a string that resets the foreground and background colors +/** + * Generate a string that resets the foreground + * and background colors + * + * Returns: The control string + */ pragma(inline) -string reset_fg_bg() {return [ascii_color_code].idup;} +string reset_fg_bg() +{ + return [ascii_color_code].idup; +} + + + // Format strings with functions -pragma(inline) -string bold(string text) {return bold_code~text~bold_code;} +/** + * Formats the provided text as bold + * + * Params: + * text = the text to bolden + * + * Returns: the boldened text + */ pragma(inline) -string italics(string text) {return italic_code~text~italic_code;} +string bold(string text) +{ + return bold_code~text~bold_code; +} +/** + * Formats the provided text in italics + * + * Params: + * text = the text to italicize + * + * Returns: the italicized text + */ pragma(inline) -string underline(string text) {return underline_code~text~underline_code;} +string italics(string text) +{ + return italic_code~text~italic_code; +} +/** + * Formats the text as underlined + * + * Params: + * text = the text to underline + * + * Returns: the underlined text + */ pragma(inline) -string strikethrough(string text) {return strikethrough_code~text~strikethrough_code;} +string underline(string text) +{ + return underline_code~text~underline_code; +} +/** + * Formats the text as strikethroughed + * + * Params: + * text = the text to strikethrough + * + * Returns: the strikethroughed text + */ pragma(inline) -string monospace(string text) {return monospace_code~text~monospace_code;} \ No newline at end of file +string strikethrough(string text) +{ + return strikethrough_code~text~strikethrough_code; +} + +/** + * Formats the text as monospaced + * + * Params: + * text = the text to monospace + * + * Returns: the monospaced text + */ +pragma(inline) +string monospace(string text) +{ + return monospace_code~text~monospace_code; +} \ No newline at end of file From 033ea850f365e2c1e72fb9e0289777d8fe777321 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sun, 19 Mar 2023 12:08:44 +0200 Subject: [PATCH 05/20] Formatting - Explicitly make the formatting functions `public` Client - Added unit tests for formatting functions --- source/birchwood/client/client.d | 15 +++++++++++++++ source/birchwood/protocol/formatting.d | 10 +++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/source/birchwood/client/client.d b/source/birchwood/client/client.d index 28b120a..96fd367 100644 --- a/source/birchwood/client/client.d +++ b/source/birchwood/client/client.d @@ -1098,6 +1098,21 @@ public class Client : Thread client.directMessage("(3) Message to myself (multi)", ["birchwood", "birchwood"]); + /** + * Test formatting of text + */ + import birchwood.protocol.formatting; + string formattedTextBold = bold("Hello in bold!"); + string formattedTextItalics = italics("Hello in italics!"); + string formattedTextUnderline = underline("Hello in underline!"); + string formattedTextMonospace = monospace("Hello in monospace!"); + string formattedTextStrikthrough = strikethrough("Hello in strikethrough!"); + client.channelMessage(formattedTextBold, "#birchwood"); + client.channelMessage(formattedTextItalics, "#birchwood"); + client.channelMessage(formattedTextUnderline, "#birchwood"); + client.channelMessage(formattedTextMonospace, "#birchwood"); + client.channelMessage(formattedTextStrikthrough, "#birchwood"); + /** diff --git a/source/birchwood/protocol/formatting.d b/source/birchwood/protocol/formatting.d index a4c415a..d1a3f27 100644 --- a/source/birchwood/protocol/formatting.d +++ b/source/birchwood/protocol/formatting.d @@ -123,7 +123,7 @@ string reset_fg_bg() * Returns: the boldened text */ pragma(inline) -string bold(string text) +public string bold(string text) { return bold_code~text~bold_code; } @@ -137,7 +137,7 @@ string bold(string text) * Returns: the italicized text */ pragma(inline) -string italics(string text) +public string italics(string text) { return italic_code~text~italic_code; } @@ -151,7 +151,7 @@ string italics(string text) * Returns: the underlined text */ pragma(inline) -string underline(string text) +public string underline(string text) { return underline_code~text~underline_code; } @@ -165,7 +165,7 @@ string underline(string text) * Returns: the strikethroughed text */ pragma(inline) -string strikethrough(string text) +public string strikethrough(string text) { return strikethrough_code~text~strikethrough_code; } @@ -179,7 +179,7 @@ string strikethrough(string text) * Returns: the monospaced text */ pragma(inline) -string monospace(string text) +public string monospace(string text) { return monospace_code~text~monospace_code; } \ No newline at end of file From 1ca052978b64b783f46f4b228d3dcd79a0ed7f85 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sun, 19 Mar 2023 12:13:54 +0200 Subject: [PATCH 06/20] CLient - Updated unit test to test combining of formatting --- source/birchwood/client/client.d | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/birchwood/client/client.d b/source/birchwood/client/client.d index 96fd367..2df9eca 100644 --- a/source/birchwood/client/client.d +++ b/source/birchwood/client/client.d @@ -1113,6 +1113,9 @@ public class Client : Thread client.channelMessage(formattedTextMonospace, "#birchwood"); client.channelMessage(formattedTextStrikthrough, "#birchwood"); + string combination = bold(italics("Italiano Boldino")); + client.channelMessage(combination, "#birchwood"); + /** From 51a04659c580e646287b771dc1b3f5dcd94475e3 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sun, 19 Mar 2023 12:19:32 +0200 Subject: [PATCH 07/20] Exceptions - Added new `ErrorType` enum member `INVALID_FORMATTING` Formatting - All formatting functions will now throw a `BirchwoodException` instead of a `StringException` on invalid formatting parameters --- source/birchwood/client/exceptions.d | 8 +++++++- source/birchwood/protocol/formatting.d | 11 +++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/source/birchwood/client/exceptions.d b/source/birchwood/client/exceptions.d index ae5a1a6..b3f1cd8 100644 --- a/source/birchwood/client/exceptions.d +++ b/source/birchwood/client/exceptions.d @@ -56,7 +56,13 @@ public enum ErrorType * If the final encoded IRC message * is too long to send to the server */ - COMMAND_TOO_LONG + COMMAND_TOO_LONG, + + /** + * If invalid parameters are passed + * to any of the text formatting functions + */ + INVALID_FORMATTING } /** diff --git a/source/birchwood/protocol/formatting.d b/source/birchwood/protocol/formatting.d index d1a3f27..d41c913 100644 --- a/source/birchwood/protocol/formatting.d +++ b/source/birchwood/protocol/formatting.d @@ -3,8 +3,7 @@ */ module birchwood.protocol.formatting; -// FIXME: Remove below import and start using BirchwoodException -import std.string; +import birchwood.client.exceptions; // Reset character; resets all formatting enum reset_code = '\x0F'; @@ -52,7 +51,7 @@ char generate_color_control_char(string color) { } else if (color.length == 2) { 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)"); + throw new BirchwoodException(ErrorType.INVALID_FORMATTING, "Invalid color code (must be either two ASCII digits or a hexadecimal code of the form RRGGBB)"); } } @@ -64,7 +63,7 @@ string set_foreground(string color) { } else if (color.length == 2) { control_char[0] = ascii_color_code; } else { - throw new StringException("Invalid color code (must be either two ASCII digits or a hexadecimal code of the form RRGGBB)"); + throw new BirchwoodException(ErrorType.INVALID_FORMATTING, "Invalid color code (must be either two ASCII digits or a hexadecimal code of the form RRGGBB)"); } return control_char.idup ~ color; } @@ -73,14 +72,14 @@ string set_foreground(string color) { string set_foreground_background(string fg, string bg) { char[1] control_char; if (fg.length != bg.length) { - throw new StringException("Invalid color code (cannot mix hex and ASCII)"); + throw new BirchwoodException(ErrorType.INVALID_FORMATTING, "Invalid color code (cannot mix hex and ASCII)"); } if (fg.length == 6) { control_char[0] = hex_color_code; } else if (fg.length == 2) { control_char[0] = ascii_color_code; } else { - throw new StringException("Invalid color code (must be either two ASCII digits or a hexadecimal code of the form RRGGBB)"); + throw new BirchwoodException(ErrorType.INVALID_FORMATTING, "Invalid color code (must be either two ASCII digits or a hexadecimal code of the form RRGGBB)"); } return control_char.idup ~ fg ~ "," ~ bg; } From 927d10abff8049a8f4e2501e602911007434a87b Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sun, 19 Mar 2023 12:34:20 +0200 Subject: [PATCH 08/20] Formatting - Added some TODOs - Code formatting - Added some documentation --- source/birchwood/protocol/formatting.d | 79 +++++++++++++++++++------- 1 file changed, 58 insertions(+), 21 deletions(-) diff --git a/source/birchwood/protocol/formatting.d b/source/birchwood/protocol/formatting.d index d41c913..f3704c1 100644 --- a/source/birchwood/protocol/formatting.d +++ b/source/birchwood/protocol/formatting.d @@ -23,7 +23,8 @@ enum hex_color_code = '\x04'; /** * Simple color codes */ -public enum SimpleColor: string { +public enum SimpleColor: string +{ WHITE = "00", BLACK = "01", BLUE = "02", @@ -45,54 +46,91 @@ public enum SimpleColor: string { // 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 // This function might be useless now that set_fg and set_fg_bg have been changed, but I'll keep it in case it's needed later. -char generate_color_control_char(string color) { - if (color.length == 6) { +char generate_color_control_char(string color) +{ + if (color.length == 6) + { return hex_color_code; - } else if (color.length == 2) { + } + else if (color.length == 2) + { return ascii_color_code; - } else { + } + else + { throw new BirchwoodException(ErrorType.INVALID_FORMATTING, "Invalid color code (must be either two ASCII digits or a hexadecimal code of the form RRGGBB)"); } } // Generates a string that changes the foreground color -string set_foreground(string color) { +string set_foreground(string color) +{ char[1] control_char; - if (color.length == 6) { + + if (color.length == 6) + { control_char[0] = hex_color_code; - } else if (color.length == 2) { + } + else if (color.length == 2) + { control_char[0] = ascii_color_code; - } else { + } + else + { throw new BirchwoodException(ErrorType.INVALID_FORMATTING, "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 and background color -string set_foreground_background(string fg, string bg) { + +// TODO: Investigate how we want to aloow people to use the below + +/** + * Generate a string that sets the foreground and background color + * + * Params: + * fg = foreground color in hex code or ASCII color code + * bg = background color + * + * Returns: the control sequence to set the style + */ +public string setForegroundBackground(string fg, string bg) +{ char[1] control_char; - if (fg.length != bg.length) { + + if (fg.length != bg.length) + { throw new BirchwoodException(ErrorType.INVALID_FORMATTING, "Invalid color code (cannot mix hex and ASCII)"); } - if (fg.length == 6) { + + if (fg.length == 6) + { control_char[0] = hex_color_code; - } else if (fg.length == 2) { + } + else if (fg.length == 2) + { control_char[0] = ascii_color_code; - } else { + } + else + { throw new BirchwoodException(ErrorType.INVALID_FORMATTING, "Invalid color code (must be either two ASCII digits or a hexadecimal code of the form RRGGBB)"); } + return control_char.idup ~ fg ~ "," ~ bg; } // Generates a string that changes the foreground color (except enum) pragma(inline) -string set_foreground(SimpleColor color) { +string set_foreground(SimpleColor color) +{ return ascii_color_code ~ color; } // Generate a string that sets the foreground and background color (except enum) pragma(inline) -string set_foreground_background(SimpleColor fg, SimpleColor bg) { +string set_foreground_background(SimpleColor fg, SimpleColor bg) +{ return ascii_color_code ~ fg ~ "," ~ bg; } @@ -108,10 +146,9 @@ string reset_fg_bg() return [ascii_color_code].idup; } - - - -// Format strings with functions +// TODO: consider removing praghma(inline), not a bad thing to have though +// TOOD: investigate idup, makes sense me thinks but take a look at +// Format strings with functions (TODO: remove comment) /** * Formats the provided text as bold From 0296cc333c56b98bcd62d1b7852b2a8eaf96c429 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sun, 19 Mar 2023 12:37:35 +0200 Subject: [PATCH 09/20] Formatting - Cleaned up code style --- source/birchwood/protocol/formatting.d | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/birchwood/protocol/formatting.d b/source/birchwood/protocol/formatting.d index f3704c1..1279c35 100644 --- a/source/birchwood/protocol/formatting.d +++ b/source/birchwood/protocol/formatting.d @@ -117,7 +117,7 @@ public string setForegroundBackground(string fg, string bg) throw new BirchwoodException(ErrorType.INVALID_FORMATTING, "Invalid color code (must be either two ASCII digits or a hexadecimal code of the form RRGGBB)"); } - return control_char.idup ~ fg ~ "," ~ bg; + return control_char.idup~fg~","~bg; } // Generates a string that changes the foreground color (except enum) @@ -131,7 +131,7 @@ string set_foreground(SimpleColor color) pragma(inline) string set_foreground_background(SimpleColor fg, SimpleColor bg) { - return ascii_color_code ~ fg ~ "," ~ bg; + return ascii_color_code~fg~","~ bg; } /** From 2f0d370c0bbcacf3652edea54bf56306b7a99280 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sun, 19 Mar 2023 16:06:44 +0200 Subject: [PATCH 10/20] bruh --- source/birchwood/protocol/formatting.d | 42 ++++++++++++++++++-------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/source/birchwood/protocol/formatting.d b/source/birchwood/protocol/formatting.d index 1279c35..469d414 100644 --- a/source/birchwood/protocol/formatting.d +++ b/source/birchwood/protocol/formatting.d @@ -67,11 +67,11 @@ string set_foreground(string color) { char[1] control_char; - if (color.length == 6) + if(color.length == 6) { control_char[0] = hex_color_code; } - else if (color.length == 2) + else if(color.length == 2) { control_char[0] = ascii_color_code; } @@ -80,7 +80,7 @@ string set_foreground(string color) throw new BirchwoodException(ErrorType.INVALID_FORMATTING, "Invalid color code (must be either two ASCII digits or a hexadecimal code of the form RRGGBB)"); } - return control_char.idup ~ color; + return control_char.idup~color; } @@ -99,16 +99,16 @@ public string setForegroundBackground(string fg, string bg) { char[1] control_char; - if (fg.length != bg.length) + if(fg.length != bg.length) { throw new BirchwoodException(ErrorType.INVALID_FORMATTING, "Invalid color code (cannot mix hex and ASCII)"); } - if (fg.length == 6) + if(fg.length == 6) { control_char[0] = hex_color_code; } - else if (fg.length == 2) + else if(fg.length == 2) { control_char[0] = ascii_color_code; } @@ -120,18 +120,34 @@ public string setForegroundBackground(string fg, string bg) return control_char.idup~fg~","~bg; } -// Generates a string that changes the foreground color (except enum) +/** + * Generates a string that changes the foreground color (except enum) + * + * Params: + * color = the foreground color + * + * Returns: the control sequence + */ pragma(inline) -string set_foreground(SimpleColor color) +public string setForeground(SimpleColor color) { - return ascii_color_code ~ color; + return ascii_color_code~color; } -// Generate a string that sets the foreground and background color (except enum) + +/** + * Generate a string that sets the foreground and background color (except enum) + * + * Params: + * fg = the foreground color + * bg = the background color + * + * Returns: thecolor control sequence + */ pragma(inline) -string set_foreground_background(SimpleColor fg, SimpleColor bg) +public string setForegroundBackground(SimpleColor fg, SimpleColor bg) { - return ascii_color_code~fg~","~ bg; + return ascii_color_code~fg~","~bg; } /** @@ -141,7 +157,7 @@ string set_foreground_background(SimpleColor fg, SimpleColor bg) * Returns: The control string */ pragma(inline) -string reset_fg_bg() +public string resetForegroundBackground() { return [ascii_color_code].idup; } From 853358a92ff52e986fa2e7959135da21db5364f5 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sun, 19 Mar 2023 16:07:34 +0200 Subject: [PATCH 11/20] d --- source/birchwood/client/exceptions.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/birchwood/client/exceptions.d b/source/birchwood/client/exceptions.d index 558aab6..f0ee49a 100644 --- a/source/birchwood/client/exceptions.d +++ b/source/birchwood/client/exceptions.d @@ -2,7 +2,7 @@ * Exception handling */ module birchwood.client.exceptions; - + import std.conv : to; /** From 25dfddba25ba61dff7e5f4fa5a12cd6b1cb45e76 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sun, 19 Mar 2023 16:07:39 +0200 Subject: [PATCH 12/20] d --- source/birchwood/client/exceptions.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/birchwood/client/exceptions.d b/source/birchwood/client/exceptions.d index f0ee49a..558aab6 100644 --- a/source/birchwood/client/exceptions.d +++ b/source/birchwood/client/exceptions.d @@ -2,7 +2,7 @@ * Exception handling */ module birchwood.client.exceptions; - + import std.conv : to; /** From 2e5ff166a6d4facf88ee01f63f0792b17ec1c86e Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sun, 19 Mar 2023 16:12:39 +0200 Subject: [PATCH 13/20] God I hate git, added all work I did back now --- source/birchwood/protocol/formatting.d | 70 +++++++++++++++----------- 1 file changed, 42 insertions(+), 28 deletions(-) diff --git a/source/birchwood/protocol/formatting.d b/source/birchwood/protocol/formatting.d index 469d414..4f282d5 100644 --- a/source/birchwood/protocol/formatting.d +++ b/source/birchwood/protocol/formatting.d @@ -5,20 +5,27 @@ module birchwood.protocol.formatting; import birchwood.client.exceptions; -// Reset character; resets all formatting -enum reset_code = '\x0F'; +/** + * Control codes + * + * TODO: Document each member + */ +public enum ControlCode: char +{ + Reset = '\x0F', -// Toggle characters -enum bold_code = '\x02'; -enum italic_code = '\x1D'; -enum underline_code = '\x1F'; -enum strikethrough_code = '\x1E'; -enum monospace_code = '\x11'; -enum reverse_colors_code = '\x16'; // NOT UNIVERSALLY SUPPORTED + Bolden = '\x02', + Italic = '\x1D', + Underline = '\x1F', + Strikethrough = '\x1E', + Monospace = '\x11', + + ReverseColors = '\x16', // NOT UNIVERSALLY SUPPORTED + + AsciiColor = '\x03', + HexColor = '\x04' +} -// Color characters -enum ascii_color_code = '\x03'; -enum hex_color_code = '\x04'; /** * Simple color codes @@ -50,11 +57,11 @@ char generate_color_control_char(string color) { if (color.length == 6) { - return hex_color_code; + return ControlCode.HexColor; } else if (color.length == 2) { - return ascii_color_code; + return ControlCode.AsciiColor; } else { @@ -62,18 +69,25 @@ char generate_color_control_char(string color) } } -// Generates a string that changes the foreground color -string set_foreground(string color) +/** + * Generates a string that changes the foreground color + * + * Params: + * color = the foreground color + * + * Returns: the color control sequence + */ +public string setForeground(string color) { char[1] control_char; if(color.length == 6) { - control_char[0] = hex_color_code; + control_char[0] = ControlCode.HexColor; } else if(color.length == 2) { - control_char[0] = ascii_color_code; + control_char[0] = ControlCode.AsciiColor; } else { @@ -106,11 +120,11 @@ public string setForegroundBackground(string fg, string bg) if(fg.length == 6) { - control_char[0] = hex_color_code; + control_char[0] = ControlCode.HexColor; } else if(fg.length == 2) { - control_char[0] = ascii_color_code; + control_char[0] = ControlCode.AsciiColor; } else { @@ -131,7 +145,7 @@ public string setForegroundBackground(string fg, string bg) pragma(inline) public string setForeground(SimpleColor color) { - return ascii_color_code~color; + return ControlCode.AsciiColor~color; } @@ -147,7 +161,7 @@ public string setForeground(SimpleColor color) pragma(inline) public string setForegroundBackground(SimpleColor fg, SimpleColor bg) { - return ascii_color_code~fg~","~bg; + return ControlCode.AsciiColor~fg~","~bg; } /** @@ -159,7 +173,7 @@ public string setForegroundBackground(SimpleColor fg, SimpleColor bg) pragma(inline) public string resetForegroundBackground() { - return [ascii_color_code].idup; + return [ControlCode.AsciiColor].idup; } // TODO: consider removing praghma(inline), not a bad thing to have though @@ -177,7 +191,7 @@ public string resetForegroundBackground() pragma(inline) public string bold(string text) { - return bold_code~text~bold_code; + return ControlCode.Bolden~text~ControlCode.Bolden; } /** @@ -191,7 +205,7 @@ public string bold(string text) pragma(inline) public string italics(string text) { - return italic_code~text~italic_code; + return ControlCode.Italic~text~ControlCode.Italic; } /** @@ -205,7 +219,7 @@ public string italics(string text) pragma(inline) public string underline(string text) { - return underline_code~text~underline_code; + return ControlCode.Underline~text~ControlCode.Underline; } /** @@ -219,7 +233,7 @@ public string underline(string text) pragma(inline) public string strikethrough(string text) { - return strikethrough_code~text~strikethrough_code; + return ControlCode.Strikethrough~text~ControlCode.Strikethrough; } /** @@ -233,5 +247,5 @@ public string strikethrough(string text) pragma(inline) public string monospace(string text) { - return monospace_code~text~monospace_code; + return ControlCode.Monospace~text~ControlCode.Monospace; } \ No newline at end of file From 6aa314603cf15c82b8b207229bc09740d933a799 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sun, 19 Mar 2023 16:14:09 +0200 Subject: [PATCH 14/20] - Documented --- source/birchwood/protocol/formatting.d | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/source/birchwood/protocol/formatting.d b/source/birchwood/protocol/formatting.d index 4f282d5..dcf96dd 100644 --- a/source/birchwood/protocol/formatting.d +++ b/source/birchwood/protocol/formatting.d @@ -51,15 +51,26 @@ public enum SimpleColor: string DEFAULT = "99" // NOT UNIVERSALLY SUPPORTED } -// 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 -// This function might be useless now that set_fg and set_fg_bg have been changed, but I'll keep it in case it's needed later. -char generate_color_control_char(string 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. + * + * This function might be useless now that set_fg and set_fg_bg have been + * changed, but I'll keep it in case it's needed later. + * + * Params: + * color = the color to check for + * + * Returns: the color control type + */ +private char generate_color_control_char(string color) { - if (color.length == 6) + if(color.length == 6) { return ControlCode.HexColor; } - else if (color.length == 2) + else if(color.length == 2) { return ControlCode.AsciiColor; } @@ -148,7 +159,6 @@ public string setForeground(SimpleColor color) return ControlCode.AsciiColor~color; } - /** * Generate a string that sets the foreground and background color (except enum) * From 37b568c21d90a9610c3e2e86a5356225603dfbbb Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sun, 19 Mar 2023 16:14:23 +0200 Subject: [PATCH 15/20] - Renamed method --- source/birchwood/protocol/formatting.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/birchwood/protocol/formatting.d b/source/birchwood/protocol/formatting.d index dcf96dd..1db3e82 100644 --- a/source/birchwood/protocol/formatting.d +++ b/source/birchwood/protocol/formatting.d @@ -64,7 +64,7 @@ public enum SimpleColor: string * * Returns: the color control type */ -private char generate_color_control_char(string color) +private char generateColorControlChar(string color) { if(color.length == 6) { From 89fbd3adf94df68ca1ea48d753f6a7cfae78a0cf Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Fri, 24 Mar 2023 15:43:28 +0200 Subject: [PATCH 16/20] Formatting - Use `cast(string)` instead of any `.dup` or `.idup` - Removed TODO - Removed `pragma(inline)`s --- source/birchwood/protocol/formatting.d | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/source/birchwood/protocol/formatting.d b/source/birchwood/protocol/formatting.d index 1db3e82..fb79b0f 100644 --- a/source/birchwood/protocol/formatting.d +++ b/source/birchwood/protocol/formatting.d @@ -105,12 +105,9 @@ public string setForeground(string color) throw new BirchwoodException(ErrorType.INVALID_FORMATTING, "Invalid color code (must be either two ASCII digits or a hexadecimal code of the form RRGGBB)"); } - return control_char.idup~color; + return cast(string)control_char~color; } - -// TODO: Investigate how we want to aloow people to use the below - /** * Generate a string that sets the foreground and background color * @@ -142,7 +139,7 @@ public string setForegroundBackground(string fg, string bg) throw new BirchwoodException(ErrorType.INVALID_FORMATTING, "Invalid color code (must be either two ASCII digits or a hexadecimal code of the form RRGGBB)"); } - return control_char.idup~fg~","~bg; + return cast(string)control_char~fg~","~bg; } /** @@ -153,7 +150,6 @@ public string setForegroundBackground(string fg, string bg) * * Returns: the control sequence */ -pragma(inline) public string setForeground(SimpleColor color) { return ControlCode.AsciiColor~color; @@ -168,7 +164,6 @@ public string setForeground(SimpleColor color) * * Returns: thecolor control sequence */ -pragma(inline) public string setForegroundBackground(SimpleColor fg, SimpleColor bg) { return ControlCode.AsciiColor~fg~","~bg; @@ -180,14 +175,11 @@ public string setForegroundBackground(SimpleColor fg, SimpleColor bg) * * Returns: The control string */ -pragma(inline) public string resetForegroundBackground() { - return [ControlCode.AsciiColor].idup; + return [ControlCode.AsciiColor]; } -// TODO: consider removing praghma(inline), not a bad thing to have though -// TOOD: investigate idup, makes sense me thinks but take a look at // Format strings with functions (TODO: remove comment) /** @@ -198,7 +190,6 @@ public string resetForegroundBackground() * * Returns: the boldened text */ -pragma(inline) public string bold(string text) { return ControlCode.Bolden~text~ControlCode.Bolden; @@ -212,7 +203,6 @@ public string bold(string text) * * Returns: the italicized text */ -pragma(inline) public string italics(string text) { return ControlCode.Italic~text~ControlCode.Italic; @@ -226,7 +216,6 @@ public string italics(string text) * * Returns: the underlined text */ -pragma(inline) public string underline(string text) { return ControlCode.Underline~text~ControlCode.Underline; @@ -240,7 +229,6 @@ public string underline(string text) * * Returns: the strikethroughed text */ -pragma(inline) public string strikethrough(string text) { return ControlCode.Strikethrough~text~ControlCode.Strikethrough; @@ -254,7 +242,6 @@ public string strikethrough(string text) * * Returns: the monospaced text */ -pragma(inline) public string monospace(string text) { return ControlCode.Monospace~text~ControlCode.Monospace; From 9cd4b910ac476398ccfdaf2cee2883cf16286634 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Fri, 24 Mar 2023 15:44:59 +0200 Subject: [PATCH 17/20] Formatting - Added a documentation comment to each member of `ControlCode` --- source/birchwood/protocol/formatting.d | 34 +++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/source/birchwood/protocol/formatting.d b/source/birchwood/protocol/formatting.d index fb79b0f..2219a00 100644 --- a/source/birchwood/protocol/formatting.d +++ b/source/birchwood/protocol/formatting.d @@ -12,17 +12,49 @@ import birchwood.client.exceptions; */ public enum ControlCode: char { + /** + * Reset styling + */ Reset = '\x0F', + /** + * Bold text styling + */ Bolden = '\x02', + + /** + * Italic text styling + */ Italic = '\x1D', + + /** + * Underlined text styling + */ Underline = '\x1F', + + /** + * Strikethough text styling + */ Strikethrough = '\x1E', + + /** + * Monospace text styling + */ Monospace = '\x11', - ReverseColors = '\x16', // NOT UNIVERSALLY SUPPORTED + /** + * Reverse colors (NOTE: not universally supported) + */ + ReverseColors = '\x16', + /** + * ASCII color encoding scheme + */ AsciiColor = '\x03', + + /** + * Hex color encoding scheme + */ HexColor = '\x04' } From f56c6eb7dcee7134b8dc8dc2f3fc7b02f10b0598 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Fri, 24 Mar 2023 15:52:47 +0200 Subject: [PATCH 18/20] Unit tests - Tested `setForeground(SimpleColor)` --- source/birchwood/client/client.d | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/birchwood/client/client.d b/source/birchwood/client/client.d index 6282fa7..980b2e9 100644 --- a/source/birchwood/client/client.d +++ b/source/birchwood/client/client.d @@ -1116,6 +1116,9 @@ public class Client : Thread string combination = bold(italics("Italiano Boldino")); client.channelMessage(combination, "#birchwood"); + string foregroundRedtext = setForeground(SimpleColor.RED)~"This is red text"; + client.channelMessage(foregroundRedtext, "#birchwood"); + /** From 70b7b89e188cb7c59f77ac36c3e3654093e396f5 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Fri, 24 Mar 2023 15:58:15 +0200 Subject: [PATCH 19/20] Unit tests - Test `setForegroundBackground(SimpleColor, SimpleColor)` --- source/birchwood/client/client.d | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/source/birchwood/client/client.d b/source/birchwood/client/client.d index 980b2e9..3548af6 100644 --- a/source/birchwood/client/client.d +++ b/source/birchwood/client/client.d @@ -1119,6 +1119,11 @@ public class Client : Thread string foregroundRedtext = setForeground(SimpleColor.RED)~"This is red text"; client.channelMessage(foregroundRedtext, "#birchwood"); + string alternatePattern = setForeground(SimpleColor.RED)~"This "~setForeground(SimpleColor.WHITE)~"is "~setForeground(SimpleColor.BLUE)~"America!"; + client.channelMessage(alternatePattern, "#birchwood"); + + string backgroundText = setForegroundBackground(SimpleColor.RED, SimpleColor.CYAN)~"Birchwood"; + client.channelMessage(backgroundText, "#birchwood"); /** From 680f245b4a9b6adc62a0d3f64ea1c8f1a4f41aa0 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Fri, 24 Mar 2023 16:02:30 +0200 Subject: [PATCH 20/20] Unit tests - Added cmobined eample that resets background color --- source/birchwood/client/client.d | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/birchwood/client/client.d b/source/birchwood/client/client.d index 3548af6..06400bd 100644 --- a/source/birchwood/client/client.d +++ b/source/birchwood/client/client.d @@ -1125,6 +1125,9 @@ public class Client : Thread string backgroundText = setForegroundBackground(SimpleColor.RED, SimpleColor.CYAN)~"Birchwood"; client.channelMessage(backgroundText, "#birchwood"); + string combined = combination~foregroundRedtext~resetForegroundBackground()~backgroundText~resetForegroundBackground()~alternatePattern; + client.channelMessage(combined, "#birchwood"); + /** * Test leaving multiple channels (multi)