0

I am trying to draw a grid using StringBuilder but I want every cell to have its own colour. I just want to use Console.WriteLine() once to print the whole grid on the console but with different colors.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Tebogo Langa
  • 47
  • 1
  • 10
  • 1
    You can't do that in one go. You'll need to install a tool like [AnsiCon](https://stackoverflow.com/questions/34073467/ansi-coloring-console-output-with-net) and then you can include the ANSI codes in your string which allows you to WriteLine the stringbuilder and the colors in one go. There is no out of the box support. – rene Oct 30 '17 at 08:04
  • 1
    I would have been really surprised if there was an out of the box solution to be honest. – L. Guthardt Oct 30 '17 at 08:07
  • So, what have you tried so far? – Sá´‡M Oct 30 '17 at 08:14

2 Answers2

1

What you essentially need is to chain formatting and data output operations and later play them in one go.

ANSI-encoded colors in a data string could be an option but this turns out to be not that easy as per @rene's comment.

Still you can chain the operations themselves. Establish a stream of Actions with some of them dealing with formatting and others dealing with data output and later you can play the stream just by executing the actions:

var data = new[] { new[] { "a", "b", "c" }, new[] { "d", "e", "f" } };
var colors = new[] { ConsoleColor.Red, ConsoleColor.Green };

// Build a stream of commands with with some of them dealing
// with formatting and others dealing with data output
var commandBuilder = new List<Action>();
var colorIndex = 0;
foreach (var row in data)
{
    foreach (var cell in row)
    {
        // Define a local variable 
        var cellColor = colors[colorIndex];
        commandBuilder.Add(() => SetCellColor(cellColor));
        commandBuilder.Add(() => DrawCell(cell));

        // flip colors
        colorIndex = ++colorIndex % colors.Length;
    }
    commandBuilder.Add(NewRow);
}

// Now, as we've built our command stream, play it:
commandBuilder.ForEach(cmd => cmd());

void SetCellColor(ConsoleColor color) { Console.ForegroundColor = color; }
void DrawCell(string cellText) { Console.Write(cellText); }
void NewRow() { Console.WriteLine(); }

Output:

enter image description here

Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40
0

There is no way to print a string in multiple colors using single Console.WriteLine call.

What you can do is create an extension method of Console.WriteLine which accepts a StringBuilder object and then in it you can par the e string to print each line in different color like this:

Assuming you need to print

Hello World in red

Hello World in blue

then generate your text like(as you need to mention each color somewhere in your string itself)

Red:Hello World in red

Blue:Hello World in blue

public static WriteLine(this Console console, StringBuilder builder)
{
  var data = builder.ToString().Split('\n');
  foreach(var row in data) 
  {
    var rowData = row.Split(':');
    var color = rowData[0];
    var text = rowData[1];
    Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), color);
    Console.WriteLine(text);
  }
}
Ipsit Gaur
  • 2,872
  • 1
  • 23
  • 38