3
\$\begingroup\$

GetBootstrap v1.0.0.2 a console customizer update from this

Please give me tips, request and suggestion for my future updates. (I update every 2 weeks)

Sample Preview enter image description here

The following code contains the Write(), WriteLine() and Typewriter effect of the console.

using System;
using System.Linq;
using System.Threading.Tasks;

namespace GetBootstrap.Styles
{
    /// <summary>
    /// Project: Console-Bootstrap v1.0.0.2 Beta
    /// Description: Use to provide a customize console application. 
    /// Developer: Leonel Sarmiento
    /// Contact: [email protected]
    /// </summary>
    public static class Bootstrap 
    {
        public static void Write(string value, BootstrapType bootstrapType, BootstrapStyle bootstrapStyle)
        {
            Styler.SetStyle(bootstrapType, bootstrapStyle);
            Console.Write(value);
            Console.ResetColor();
        }

        public static void Write(string value, BootstrapType bootstrapType, BootstrapStyle bootstrapStyle, int minDelay, int maxDelay)
        {
            Styler.SetStyle(bootstrapType, bootstrapStyle);
            Typewriter.Write(value, minDelay, maxDelay);
            Console.ResetColor();
        }

        public static void Write(string format, BootstrapType bootstrapType, BootstrapStyle bootstrapStyle, params object[] arg)
        {
            Styler.SetStyle(bootstrapType, bootstrapStyle);
            Console.Write(format, arg);
            Console.ResetColor();
        }

        public static void Write(string format, BootstrapType bootstrapType, BootstrapStyle bootstrapStyle, int minDelay, int maxDelay, params object[] arg)
        {
            Styler.SetStyle(bootstrapType, bootstrapStyle);
            Typewriter.Write(string.Format(format,arg), minDelay, maxDelay);
            Console.ResetColor();
        }

        public static void WriteLine(string value, BootstrapType bootstrapType, BootstrapStyle bootstrapStyle)
        {
            Write(value, bootstrapType, bootstrapStyle);
            Console.WriteLine("");
        }

        public static void WriteLine(string value, BootstrapType bootstrapType, BootstrapStyle bootstrapStyle, int minDelay, int maxDelay)
        {
            Write(value, bootstrapType, bootstrapStyle, minDelay, maxDelay);
            Console.WriteLine("");
        }

        public static void WriteLine(string format, BootstrapType bootstrapType, BootstrapStyle bootstrapStyle, params object[] arg)
        {
            Write(format, bootstrapType, bootstrapStyle, arg);
            Console.WriteLine("");
        }

        public static void WriteLine(string format, BootstrapType bootstrapType, BootstrapStyle bootstrapStyle, int minDelay, int maxDelay, params object[] arg)
        {
            Write(format, bootstrapType, bootstrapStyle, minDelay, maxDelay, arg);
            Console.WriteLine("");
        }
    }

    public static class Typewriter
    {
        public static void Write(string value, int minDelay, int maxDelay)
        {
            Random random = new Random();
            for (int c = 0; c < value.Count(); c++)
            {
                System.Threading.Thread.Sleep(random.Next(minDelay, maxDelay));
                Console.Write(value.Substring(c,1));
            }
        }

        public static void WriteLine(string value, int minDelay, int maxDelay)
        {
            Random random = new Random();
            for (int c = 0; c < value.Count(); c++)
            {
                System.Threading.Thread.Sleep(random.Next(minDelay, maxDelay));
                if (c != value.Count() - 1)
                {
                    Console.Write(value.Substring(c, 1));
                }
                else
                {
                    Console.WriteLine(value.Substring(c, 1));
                }
            }
        }
    }

    public static class Styler
    {
        public static void SetStyle(BootstrapType bootstrapType, BootstrapStyle bootstrapStyle)
        {
            switch (bootstrapType)
            {
                case BootstrapType.Success:
                    if (bootstrapStyle == BootstrapStyle.Alert)
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.BackgroundColor = ConsoleColor.DarkGreen;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                    }
                    break;
                case BootstrapType.Info:
                    if (bootstrapStyle == BootstrapStyle.Alert)
                    {
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.BackgroundColor = ConsoleColor.DarkCyan;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Cyan;
                    }
                    break;
                case BootstrapType.Warning:
                    if (bootstrapStyle == BootstrapStyle.Alert)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.BackgroundColor = ConsoleColor.DarkYellow;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                    break;
                case BootstrapType.Danger:
                    if (bootstrapStyle == BootstrapStyle.Alert)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.BackgroundColor = ConsoleColor.DarkRed;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                    }
                    break;
                default:
                    if (bootstrapStyle == BootstrapStyle.Alert)
                    {
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.BackgroundColor = ConsoleColor.DarkGray;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
                    break;
            }
        }
    }

    public enum BootstrapStyle
    {
        None = 0,
        Alert = 1
    }

    public enum BootstrapType
    {
        Default = 0,
        Success = 1,
        Info = 2,
        Warning = 3,
        Danger = 4
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ In your if elses in your switch, you don't need elses if the exact same code is in the if, for example: vConsole.ForegroundColor = ConsoleColor.Red; \$\endgroup\$
    – PmanAce
    Mar 29, 2018 at 13:59

1 Answer 1

1
\$\begingroup\$

Simplify TypeWriter.WriteLine by implementing it in terms of TypeWriter.Write:

public static void WriteLine(string value, int minDelay, int maxDelay)
{
    Write(value, minDelay, maxDelay);
    Console.WriteLine("");
}

Do you really need a new Random() in every call to Write? Why not create a single Random object per class and reuse that in random.Next(...) calls?


Both branches of this if set the foreground to green no matter what:

if (bootstrapStyle == BootstrapStyle.Alert)
{
    Console.ForegroundColor = ConsoleColor.Green;
    Console.BackgroundColor = ConsoleColor.DarkGreen;
}
else
{
    Console.ForegroundColor = ConsoleColor.Green;
}

You can simplify to:

Console.ForegroundColor = ConsoleColor.Green;
if (bootstrapStyle == BootstrapStyle.Alert)
{
    Console.BackgroundColor = ConsoleColor.DarkGreen;
}

The same goes for all the case statements in the switch (bootstrapType).


When using enums, don't assign their values if you don't really need to. This is simpler and better:

public enum BootstrapStyle
{
    None,
    Alert
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.