0

I'm just new writing in console application. I'm still learning how to use it properly.

My question is how I can change the writeline foregroundColor?

I want to change the color of each title for example:

TITLE: DURATION (cyan color) >>>> info (white color)
TITLE: CATEGORY (cyan color) >>>> info (white color)
TITLE: STATUS  (cyan color)  >>>> info (white color)

here's my code:

        var sb = new StringBuilder();
        sb.Append(string.Format("\r\nTITLE:       {0} - EP {1}/{2}\n", title, episode, totalEpisode));
        sb.Append(string.Format("DURATION:        {0}\n", _duration));
        sb.Append(string.Format("CATEGORY:        {0}\n", category));
        sb.Append(string.Format("STATUS:          {0} ({1} EP)\n\n", releaseStatus, totalEpisode));

        sb.Append("720p Direct Link:" + "\n");
        sb.Append(string.Format("   DIRECT LINK:  {0}\n", directLink720p));
        sb.Append(string.Format("   BITRATE:      {0}\n", bitrate));
        sb.Append(string.Format("   RESOLUTION:   {0}x{1}p\n", width, height));
        sb.Append(string.Format("   DURATION:     {0}\n", duration));
        sb.Append(string.Format("   FILESIZE:     {0}\n", filesize));
        sb.Append(string.Format("   CODEC:        {0}\n\n", codec));

        sb.Append("SUBTITLE: " + "\n");
        sb.Append(string.Format("   SUBTITLE URL:  {0}\n", subURL));
        sb.Append(string.Format("   SUBTITLE LANG: {0}\n\n", subName));

        sb.Append(string.Format("VIDEO:\n    {0}\n\n", _array[0]));
        sb.Append(string.Format("AUDIO:\n    {0}\n", _array[1]));

        Console.WriteLine(sb.ToString());
zackmark15
  • 657
  • 6
  • 12
  • 2
    You won't be able to use the `StringBuilder` for this, since the calls to Write will always use the current colors. You will have to make multiple calls to `Console.Write`, changing the foreground and background colors between each call. You can change colors by setting `Console.ForegroundColor`, and `Console.BackgroundColor` properties. – Bradley Uffner Jul 30 '20 at 13:34
  • 1
    You could also use a package library like [Edokan.KaiZen.Colors](https://github.com/edokan/Edokan.KaiZen.Colors) to make things a little easier. – Bradley Uffner Jul 30 '20 at 13:37
  • @BradleyUffner thank you I think that is what I'm looking for. Let me try ;) – zackmark15 Jul 30 '20 at 13:43
  • @BradleyUffner alright thank you for the info. I will not use stringbuilder since I want to change the color. – zackmark15 Jul 30 '20 at 13:44
  • That KaiZen library actually uses an interesting trick to change the colors. It installs its self in to the console output stream, and catches custom color escape sequences in the string, converting them in to console color change commands, character by character as it goes. This would actually make it possible to use the `StringBuilder`. It's very clever. – Bradley Uffner Jul 30 '20 at 13:46
  • If you want to get *really* fancy, check out the [Colorful.Console](http://colorfulconsole.com/) package. – Bradley Uffner Jul 30 '20 at 13:59
  • @BradleyUffner wow it's good to know that i can still use stringbuilder. I'll try it – zackmark15 Jul 30 '20 at 14:07

1 Answers1

0

Alright. I have found a solution. Here's what I did

writeMessage("\n" + " TITLE:", string.Format("{0} - EP {1}/{2}\n", title, episode, totalEpisode));
writeMessage("DURATION:",      string.Format("{0}\n", _duration));
writeMessage("CATEGORY:",      string.Format("{0}\n", category));
writeMessage("STATUS:",        string.Format("{0} ({1} EP)\n", releaseStatus, totalEpisode));

and here's the class that I found here: https://stackoverflow.com/a/34866857/13621395

   private static object messageLock = new object();
    public static void writeMessage(string message, string message2)
    {
        lock (messageLock)
        {
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.Write(message);
            Console.ResetColor();
            Console.Write(" " + message2 + " ");
        }
    }
zackmark15
  • 657
  • 6
  • 12
  • To make this more dynamic, you may add a `ConsoleColor foregroundColor` and `ConsoleColor backgroundColor` parameters to the `WriteMessage` method. – 41686d6564 stands w. Palestine Jul 30 '20 at 14:19
  • Are you doing multi-threading? If not, you don't need to `lock` anything, simplifying your code. – Bradley Uffner Jul 30 '20 at 14:21
  • @BradleyUffner not mutli-threading only once. Alright I'll remove the lock class – zackmark15 Jul 30 '20 at 14:35
  • @AhmedAbdelhameed alright. How I can input RGB color? there is no option – zackmark15 Jul 30 '20 at 14:36
  • @zackmark15 The `ForegroundColor` and `BackgroundColor` properties of the Console class only support 16 _specific_ colors named in the [`ConsoleColor` enum](https://learn.microsoft.com/en-us/dotnet/api/system.consolecolor). If you want to change the colors of the console using any arbitrary colors, things get [a little more complicated](https://stackoverflow.com/q/7937256/8967612). – 41686d6564 stands w. Palestine Jul 30 '20 at 14:56