private static int ComputeSpacesBefore(int textWidth, int width, ConsoleTextAlignment alignment) { return(alignment switch { ConsoleTextAlignment.Right => width - textWidth, ConsoleTextAlignment.Center => (int)Math.Ceiling((double)((width - textWidth) / 2)), _ => 0 });
/// <summary> /// Draws a pattern on the console, each non-space caracter being replaced by the color provided /// </summary> /// <param name="pattern"></param> /// <param name="color"></param> /// <param name="alignment"></param> public static void DrawPattern(string pattern, ConsoleColor color, ConsoleTextAlignment alignment = ConsoleTextAlignment.Left) { DrawPattern(pattern, (c) => { if (c != ' ') { return(color); } return(null); }, alignment); }
/// <summary> /// Writes a line of text or multilined text with colouring and alignment.<br/> /// Important : for multiline text use the constant : <code>Environment.NewLine</code> /// </summary> /// <example> /// <code> /// ConsoleExtended("First line" + Environment.NewLine + "Second line", ConsoleTextAlignment.Center); /// </code> /// </example> /// <param name="line">The line of text to write</param> /// <param name="alignment">The text alignment expected</param> /// <param name="backgroundColor">The color of the console background for the text</param> /// <param name="foregroundColor">The color of the foreground for the text</param> /// <param name="fill">True to fill the whole line with the background color</param> public static void WriteLine(string line, ConsoleTextAlignment alignment = ConsoleTextAlignment.Left, ConsoleColor?backgroundColor = null, ConsoleColor?foregroundColor = null, bool fill = false) { if (line.Contains(Environment.NewLine)) { foreach (var chunk in line.Split(Environment.NewLine)) { WriteLine(chunk, alignment, backgroundColor, foregroundColor, fill); } return; } var baseBackgroundColor = Console.BackgroundColor; var baseForegroundColor = Console.ForegroundColor; var width = Console.WindowWidth; var spaces = ComputeSpacesBefore(line.Length, width, alignment); var textBackgroundColor = backgroundColor ?? baseBackgroundColor; var textForegroundColor = foregroundColor ?? baseForegroundColor; var spacesColor = fill ? textBackgroundColor : baseBackgroundColor; if (spaces >= 0) { Console.BackgroundColor = spacesColor; Console.Write(new string(' ', spaces)); Console.BackgroundColor = textBackgroundColor; Console.ForegroundColor = textForegroundColor; Console.Write(line); Console.BackgroundColor = spacesColor; Console.WriteLine(new string(' ', spaces)); Console.BackgroundColor = baseBackgroundColor; Console.ForegroundColor = baseForegroundColor; } else { foreach (var chunk in line.ToChunks(width, true)) { DoWriteString(chunk, width, alignment, backgroundColor, foregroundColor, fill); } } }
/// <summary> /// Draw a pattern in the console, with a dictionnary providing the different colors for each caracter in the pattern /// </summary> /// <param name="pattern"></param> /// <param name="mappings"></param> /// <param name="alignment"></param> public static void DrawPattern(string pattern, Dictionary <char, ConsoleColor?> mappings, ConsoleTextAlignment alignment = ConsoleTextAlignment.Left) { DrawPattern(pattern, (c) => { if (mappings.TryGetValue(c, out ConsoleColor? color)) { return(color); } return(null); }, alignment); }
/// <summary> /// Draws a pattern on the console. The pattern is composed of chars corresponding to different colors /// </summary> /// <param name="pattern"></param> /// <param name="charMapper">Maps each char to a color</param> /// <param name="alignment"></param> public static void DrawPattern(string pattern, Func <char, ConsoleColor?> charMapper, ConsoleTextAlignment alignment = ConsoleTextAlignment.Left) { var defaultColor = Console.BackgroundColor; var lines = pattern.Split(Environment.NewLine.ToCharArray()).Where(l => !string.IsNullOrEmpty(l)).ToList(); var spaces = ComputeSpacesBefore(lines.Select(l => l.Length).Max(), Console.WindowWidth, alignment); foreach (var line in lines) { Console.BackgroundColor = defaultColor; Console.Write(new String(' ', spaces)); foreach (var c in line) { var color = charMapper(c); Console.BackgroundColor = color ?? defaultColor; Console.Write(" "); Console.BackgroundColor = defaultColor; } Console.WriteLine(""); } Console.BackgroundColor = defaultColor; }