drewnoakes / figgle

ASCII banner generation for .NET
Apache License 2.0
412 stars 42 forks source link

Print on the Same Line Different Color #4

Open natrajgs opened 4 years ago

natrajgs commented 4 years ago

I am trying to print some characters in one color and rest of then in another color - but cannot Console.Write is acting like Console.WriteLine i.e. adding an line break.

Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.Write(FiggleFonts.Cards.Render($"{charArray[0]}{charArray[1]}{charArray[2]}"));
Console.ForegroundColor = ConsoleColor.White;
Console.Write(FiggleFonts.Cards.Render($"{charArray[3]}"));
drewnoakes commented 4 years ago

There are two kinds of lines at play here.

 _____ _         _     
|   __|_|___ ___| |___ 
|   __| | . | . | | -_|
|__|  |_|_  |_  |_|___|
        |___|___|   

In that example, you could say the first line is the word "Figgle". But actually, from the perspective of the console, that text is 5 different lines of text:

Line 1:  _____ _         _     
Line 2: |   __|_|___ ___| |___ 
Line 3: |   __| | . | . | | -_|
Line 4: |__|  |_|_  |_  |_|___|
Line 5:         |___|___|   

The Render method is returning a multi-line string (i.e. containing line endings). When you write it to the console, the cursor ends up at the bottom right corner of the text. If you try to write more text, it'll probably appear slightly broken and underneath the first thing you wrote.


Assuming I understand you correctly, you're asking for multi-colour printing support. Does that sound right? If so, then this is a feature request as it's not something that's currently supported.

natrajgs commented 4 years ago

Yes, that is what exactly I was looking for - please change to a feature request.

ghost commented 3 years ago

Try like this.

var text = FiggleFonts.Standard.Render(message);
var result = Regex.Split(text, "\r\n|\r|\n");
var currentColor = Console.ForegroundColor;
var colors = new Queue<ConsoleColor>();

colors.Enqueue(ConsoleColor.Red);
colors.Enqueue(ConsoleColor.Green);
colors.Enqueue(ConsoleColor.Blue);

foreach (var line in result)
{
    var color = colors.Dequeue();

    Console.ForegroundColor = color;
    Console.WriteLine(line);

    colors.Enqueue(color);
}

Console.ForegroundColor = currentColor;

image

drewnoakes commented 1 year ago

I interpret the original request here as allowing individual characters to be printed in different colours. In the "Hello World!" example, the 'H' might be a different colour to the 'e'.

I suspect something like this would need to use control characters in the output to work correctly.