Home > Software design >  How do I make a method out of this console write-line mess?
How do I make a method out of this console write-line mess?

Time:08-05

class WriteLine
{
    public static void Red(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Red;

        Console.WriteLine(input);
    }

    public static void Cyan(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Cyan;

        Console.WriteLine(input);
    }


    public static void Yellow (string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Yellow;

        Console.WriteLine(input);
    }


    public static void Purple(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Magenta;

        Console.WriteLine(input);
    }


    public static void White(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.White;

        Console.WriteLine(input);
    }


    public static void Black(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Black;

        Console.WriteLine(input);
    }

    public static void Green(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Green;

        Console.WriteLine(input);
    }

}
class Write
{
    public static void Red(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Red;

        Console.Write(input);
    }

    public static void Cyan(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Cyan;

        Console.Write(input);
    }


    public static void Yellow(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Yellow;

        Console.Write(input);
    }


    public static void Purple(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Magenta;

        Console.Write(input);
    }


    public static void White(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.White;

        Console.Write(input);
    }


    public static void Black(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Black;

        Console.Write(input);
    }

    public static void Green(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Green;

        Console.Write(input);
    }
}

Is there a way to shorten this and make it more compact, like into a method, I was thinking something so that I could use WriteLine.Red("test"); and it would print red text. New to C# any tips would be greatly appreciated

CodePudding user response:

You could use this class for all your use cases:

public class ConsoleLineWriter
{
    public void WriteLine(ConsoleColor color, string text)
    {
        Console.ForegroundColor = color;
        Console.WriteLine(text);
    }

    public void Write(ConsoleColor color, string text)
    {
        Console.ForegroundColor = color;
        Console.Write(text);
    }
}

Demo:

public static void Main()
{
    var writer = new ConsoleLineWriter();
    writer.WriteLine(ConsoleColor.DarkBlue, "Foo");
    writer.WriteLine(ConsoleColor.Green, "Bah");
    writer.Write(ConsoleColor.Red, "SameLine1");
    writer.Write(ConsoleColor.Cyan, "SameLine2");
    writer.Write(ConsoleColor.White, null); // back to default
}

CodePudding user response:

The below will not require changes to your existing code using the writers, but makes the code a bit better:

class WriteLine
{
    public static void InNewLine(ConsoleColor color, string s)
    {
        Console.WriteLine();

        Console.ForegroundColor = color;

        Console.WriteLine(s);
    }

    public static void Red(string s) => InNewLine(ConsoleColor.Red, s);
    public static void Cyan(string s) => InNewLine(ConsoleColor.Cyan, s);
    public static void Yellow (string s) => InNewLine(ConsoleColor.Yellow, s);
    public static void Purple(string s) => InNewLine(ConsoleColor.Magenta, s);
    public static void White(string s) => InNewLine(ConsoleColor.White, s);
    public static void Black(string s) => InNewLine(ConsoleColor.Black, s);
    public static void Green(string s) => InNewLine(ConsoleColor.Green, s);
}

// Similar for `Write`

BTW. The pattern of having a method that takes all options and then "shortcut" methods is used by ILogger Interface. It has Log(ILogger, LogLevel, String, Object[]), where you specify log level there also are "shortcut" methods:

  • LogCritical(ILogger, String, Object[])
  • LogDebug(ILogger, String, Object[]).
  • ...

BTW2. It may be polite to restore the previous color after writing:

public static void InNewLine(ConsoleColor color, string s)
{
   Console.WriteLine();

   var c = Console.ForegroundColor;
   Console.ForegroundColor = color;

   Console.WriteLine(s);
   Console.ForegroundColor = c;
}
  • Related