/// <summary> /// Writes colored text to the console output. /// </summary> /// <param name="foreground">The foreground color of the text.</param> /// <param name="background">The background color of the text.</param> /// <param name="message">The message to send.</param> /// <param name="args">Optional arguments to use when formatting the message.</param> public static void Write(IConsoleColor foreground, IConsoleColor background, string message, params object[] args) { message = Formatters.GetColorFormattedString(new ColorFormat(foreground, background), message, args); if (!string.IsNullOrEmpty(message)) { if (isLastWriteALine) { WriteToConsoles("{0}{1}", headerText, message); } else { WriteToConsoles(message); } } }
/// <summary> /// Formats the provided text to include an ANSI SGR escape sequence. /// </summary> /// <param name="foreground">The <see cref="IConsoleColor"/> to use for the foreground.</param> /// <param name="background">The <see cref="IConsoleColor"/> to use for the background.</param> /// <param name="textToFormat">The text to format.</param> /// <param name="args">Optional array of arguments to use when formatting the text.</param> /// <returns>A <see langword="string"/> containing the provided text and ANSI SGR color formatting based on the colors provided.</returns> public static string FormatText(IConsoleColor foreground, IConsoleColor background, string textToFormat, params object[] args) { if (Options.Instance.ColorizeConsoleOutput) { var fore = foreground.AsForeground(); var back = background.AsBackground(); return(string.Format("\x1b[{0}{1}{2}m{3}\x1b[0m", fore, fore != string.Empty && back != string.Empty ? ";" : string.Empty, back, string.Format(textToFormat, args))); } else { return(string.Format(textToFormat, args)); } }
/// <summary> /// Initializes a new instance of the <see cref="ColorFormat"/> class. /// </summary> /// <param name="foreground">The foreground color to assign the format.</param> /// <param name="background">The background color to assign the format.</param> public ColorFormat(IConsoleColor foreground, IConsoleColor background) { if (foreground == null) { Foreground = (StandardColor)ColorCode.None; } else { Foreground = foreground; } if (background == null) { Background = (StandardColor)ColorCode.None; } else { Background = background; } }
/// <summary> /// Formats the provided text to include an ANSI SGR escape sequence. /// </summary> /// <param name="foreground">The <see cref="IConsoleColor"/> to use for the foreground.</param> /// <param name="background">The <see cref="IConsoleColor"/> to use for the background.</param> /// <param name="closeColorFormat">When true will end the text with a terminating ANSI SGR sequence.</param> /// <param name="textToFormat">The text to format.</param> /// <param name="args">Optional array of arguments to use when formatting the text.</param> /// <returns>A <see langword="string"/> containing the provided text and ANSI SGR color formatting based on the colors provided.</returns> public static string FormatText(IConsoleColor foreground, IConsoleColor background, bool closeColorFormat, string textToFormat, params object[] args) { if (Options.Instance.ColorizeConsoleOutput) { var fore = foreground.AsForeground(); var back = background.AsBackground(); if (string.IsNullOrEmpty(fore) && string.IsNullOrEmpty(back)) { return(textToFormat.OptionalFormat(args)); } return(string.Format("\x1b[{0}{1}{2}m{3}{4}", fore, !string.IsNullOrEmpty(fore) && !string.IsNullOrEmpty(back) ? ";" : string.Empty, back, textToFormat.OptionalFormat(args), closeColorFormat ? "\x1b[0m" : string.Empty)); } else { return(textToFormat.OptionalFormat(args)); } }
/// <summary> /// Initializes a new instance of the <see cref="ColorFormat"/> class. /// </summary> /// <param name="foreground">The foreground color to assign the format.</param> public ColorFormat(IConsoleColor foreground) : this(foreground, null) { }
/// <summary> /// Formats the provided text to include an ANSI SGR escape sequence. /// </summary> /// <param name="foreground">The <see cref="IConsoleColor"/> to use for the foreground.</param> /// <param name="background">The <see cref="IConsoleColor"/> to use for the background.</param> /// <param name="textToFormat">The text to format.</param> /// <param name="args">Optional array of arguments to use when formatting the text.</param> /// <returns>A <see langword="string"/> containing the provided text and ANSI SGR color formatting based on the colors provided.</returns> public static string FormatText(IConsoleColor foreground, IConsoleColor background, string textToFormat, params object[] args) { return(FormatText(foreground, background, true, textToFormat, args)); }