using System; using System.Text; using System.Text.RegularExpressions; using DTT.Utils.Workflow; using UnityEngine; namespace DTT.Utils.Extensions { /// /// Contains helper methods for adding rich text to for example debug logs. /// public static class RichTextExtensions { /// /// Renders the text in boldface. /// /// Text to render in boldface. /// Text rendered in boldface. public static string Bold(this string input) => WrapAround("", input, ""); /// /// Renders the text in italics. /// /// Text to render in italics. /// Text rendered in italics. public static string Italics(this string input) => WrapAround("", input, ""); /// /// Sets the size of the text according to the parameter value, given in pixels. /// Although this tag is available for console logging, /// you will find that the line spacing in the window bar /// and Console looks strange if the size is set too large. /// /// Text to apply sizing to. /// The size for the text, given in pixels. /// Text that has been sized. public static string Size(this string input, int size) => WrapAround($"", input, ""); /// /// Sets the color of the text according to the parameter value. /// /// Text to set the color of. /// The color to set the text to. /// Text that a color has been applied to. public static string Color(this string input, Color color) => WrapAround($"", input, ""); /// /// Sets the color of the text according to the parameter value. /// /// Text to set the color of. /// The color type to set the text to. /// Text that a color has been applied to. public static string Color(this string input, RichTextColor color) => WrapAround($"", input, ""); /// /// Sets the color of the text according to the parameter value. /// /// Text to set the color of. /// The color type to set the text to. /// Text that a color has been applied to. public static string Color(this string input, string hexColor) { StringBuilder sb = new StringBuilder(hexColor); if (sb[0] != '#') sb.Insert(0, '#'); string newHex = sb.ToString(); if (!StringUtility.IsHexadecimal(newHex)) throw new ArgumentException(nameof(hexColor), $"Passed hex color string isn't a valid notation. Passed value: {hexColor}"); return WrapAround($"", input, ""); } /// /// Wraps a start and end element around an input and returns the result. /// Contains additional behavior for returning just the input based on the platform. /// This can help with reducing rich text which doesn't get formatted in build logs. /// /// The string to elements around. /// The first element. /// The last element. /// The new string with elements wrapped around the input. private static string WrapAround(string startElement, string input, string endElement) => string.Join(string.Empty, startElement, input, endElement); } }