< Summary

Information
Class: MRA.Services.Helpers.ConsoleHelper
Assembly: MRA.Services
File(s): D:\a\MiguelRomerART\MiguelRomerART\MRA.Services\Helpers\ConsoleHelper.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 128
Coverable lines: 128
Total lines: 254
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 34
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

D:\a\MiguelRomerART\MiguelRomerART\MRA.Services\Helpers\ConsoleHelper.cs

#LineLine coverage
 1using System.ComponentModel;
 2
 3namespace MRA.Services.Helpers
 4{
 5    public class ConsoleHelper
 6    {
 07        private int PAD_RIGHT = 10;
 08        public bool ShowMessageType = true;
 9
 10        public void ShowMessageTrace(string message)
 11        {
 012            Console.ForegroundColor = ConsoleColor.DarkGray;
 013            Console.WriteLine(message);
 014            Console.ForegroundColor = ConsoleColor.White;
 015        }
 16        public void ShowMessageDebug(string message)
 17        {
 018            Console.ForegroundColor = ConsoleColor.DarkGreen;
 019            Console.WriteLine(message);
 020            Console.ForegroundColor = ConsoleColor.White;
 021        }
 22        public void ShowMessageInfo(string message)
 23        {
 024            Console.ForegroundColor = ConsoleColor.Cyan;
 025            Console.WriteLine(message);
 026            Console.ForegroundColor = ConsoleColor.White;
 027        }
 28
 29        public void ShowMessageWarning(string message)
 30        {
 031            Console.ForegroundColor = ConsoleColor.Yellow;
 032            Console.WriteLine(message);
 033            Console.ForegroundColor = ConsoleColor.White;
 034        }
 35
 36        public void ShowMessageSuccess(string message)
 37        {
 038            Console.ForegroundColor = ConsoleColor.Green;
 039            Console.WriteLine(message);
 040            Console.ForegroundColor = ConsoleColor.White;
 041        }
 42        public void ShowMessageError(string message)
 43        {
 044            Console.ForegroundColor = ConsoleColor.Red;
 045            Console.WriteLine(message);
 046            Console.ForegroundColor = ConsoleColor.White;
 047        }
 48        public void ShowMessageCritical(string message)
 49        {
 050            var previous = Console.BackgroundColor;
 051            Console.BackgroundColor = ConsoleColor.Red;
 052            Console.ForegroundColor = ConsoleColor.White;
 053            Console.WriteLine(message);
 054            Console.BackgroundColor = previous;
 055            Console.ForegroundColor = ConsoleColor.White;
 056        }
 57
 58        public void ShowMessagePrevious(bool isNew, string previous)
 59        {
 060            if (!isNew)
 61            {
 062                Console.ForegroundColor = ConsoleColor.DarkGray;
 063                Console.Write("".PadRight(PAD_RIGHT));
 064                Console.Write("[Default: '");
 65
 066                Console.ForegroundColor = ConsoleColor.Green;
 067                Console.Write(previous);
 68
 069                Console.ForegroundColor = ConsoleColor.DarkGray;
 070                Console.WriteLine("'][Leave empty to use it]");
 71
 072                Console.ForegroundColor = ConsoleColor.White;
 73            }
 074        }
 75
 76        public void ShowMessage(bool isNew, string previous, string field)
 77        {
 078            Console.ForegroundColor = ConsoleColor.White;
 079            ShowMessageInfo("------------------------------------------------------");
 080            System.Console.WriteLine("INPUT:".PadRight(PAD_RIGHT) + field.ToUpper());
 081            ShowMessagePrevious(isNew, previous);
 082        }
 83
 84        public void ShowMessage(string field)
 85        {
 086            Console.ForegroundColor = ConsoleColor.White;
 087            System.Console.WriteLine(field);
 088        }
 89
 90        public string ReadValueFromConsole()
 91        {
 092            Console.ForegroundColor = ConsoleColor.White;
 093            Console.Write("> ");
 094            return Console.ReadLine();
 95        }
 96
 97        public string ReadValue(bool isNew, string previous)
 98        {
 99
 0100            var input = ReadValueFromConsole();
 0101            if (!isNew && String.IsNullOrEmpty(input))
 102            {
 0103                input = previous;
 104            }
 0105            ShowValueSet(input.ToString());
 0106            return input;
 107        }
 108
 109        public string ReadValue()
 110        {
 111
 0112            var input = ReadValueFromConsole();
 0113            ShowValueSet(input.ToString());
 0114            return input;
 115        }
 116
 117        public int FillIntValue(bool isNew, int previous, string field, Dictionary<int, string> dictionary)
 118        {
 0119            ShowMessageInfo("------------------------------------------------------");
 0120            ShowMessageInfo(field.ToUpper() + ":");
 121
 0122            foreach (var type in dictionary)
 123            {
 0124                ShowMessageInfo(type.Key.ToString().PadRight(5) + "= " + type.Value);
 125            }
 126
 0127            if (!isNew)
 128            {
 0129                System.Console.WriteLine("  ");
 0130                ShowMessagePrevious(isNew, dictionary[previous].ToString());
 131            }
 132
 0133            var input = ReadValueFromConsole();
 0134            if (!isNew && String.IsNullOrEmpty(input))
 135            {
 0136                ShowValueSet(dictionary[previous]);
 0137                return previous;
 138            }
 139            else
 140            {
 0141                int.TryParse(input, out int numeroEntero);
 0142                ShowValueSet(dictionary[numeroEntero]);
 0143                return numeroEntero;
 144            }
 145        }
 146
 147        public int FillFreeIntValue(bool isNew, int previous, string field)
 148        {
 0149            ShowMessage(isNew, previous.ToString(), field);
 150
 0151            var input = ReadValueFromConsole();
 0152            if (!isNew && String.IsNullOrEmpty(input))
 153            {
 0154                ShowValueSet(previous.ToString());
 0155                return previous;
 156            }
 157            else
 158            {
 0159                int.TryParse(input, out int numeroEntero);
 0160                ShowValueSet(numeroEntero.ToString());
 0161                return numeroEntero;
 162            }
 163        }
 164
 165
 166        public bool FillBoolValue(string field)
 167        {
 0168            ShowMessage(field);
 0169            Console.ForegroundColor = ConsoleColor.DarkGray;
 0170            System.Console.WriteLine("".PadRight(PAD_RIGHT) + "[Y|y: 'True']");
 0171            Console.ForegroundColor = ConsoleColor.White;
 172
 0173            var input = ReadValueFromConsole();
 174
 0175            bool value = false;
 0176            if (input.ToLower().Equals("y"))
 177            {
 0178                value = true;
 179            }
 180
 0181            ShowValueSet(value.ToString());
 0182            return value;
 183        }
 184
 185        public bool FillBoolValue(bool isNew, bool previous, string field)
 186        {
 0187            ShowMessage(isNew, previous.ToString(), field);
 0188            Console.ForegroundColor = ConsoleColor.DarkGray;
 0189            System.Console.WriteLine("".PadRight(PAD_RIGHT) + "[Y|y: 'True']");
 0190            Console.ForegroundColor = ConsoleColor.White;
 191
 0192            var input = ReadValueFromConsole();
 193
 0194            if (!isNew && String.IsNullOrEmpty(input))
 195            {
 0196                ShowValueSet(previous.ToString());
 0197                return previous;
 198            }
 199            else
 200            {
 0201                bool value = false;
 0202                if (input.ToLower().Equals("y"))
 203                {
 0204                    value = true;
 205                }
 206
 0207                ShowValueSet(value.ToString());
 0208                return value;
 209            }
 210        }
 211
 212        public void ShowValueSet(string value)
 213        {
 0214            Console.WriteLine("");
 0215            Console.ForegroundColor = ConsoleColor.Green;
 0216            Console.Write("VALUE ".PadRight(PAD_RIGHT));
 0217            Console.WriteLine((String.IsNullOrEmpty(value) ? "''" : value));
 0218            Console.ForegroundColor = ConsoleColor.White;
 0219            Console.WriteLine("");
 0220        }
 221
 222        public string FillStringValue(bool isNew, string previous, string field)
 223        {
 0224            ShowMessage(isNew, previous, field);
 0225            return ReadValue(isNew, previous);
 226        }
 227
 228        public string FillStringValue(string field)
 229        {
 0230            ShowMessage(field);
 0231            return ReadValue();
 232        }
 233
 234        public void PrintPropreties(object obj)
 235        {
 236            // Obtener todas las propiedades
 0237            PropertyDescriptorCollection propiedades = TypeDescriptor.GetProperties(obj);
 238
 239            // Ordenar las propiedades por nombre
 0240            var propiedadesOrdenadas = propiedades.Sort()
 0241                                                  .Cast<PropertyDescriptor>()
 0242                                                  .ToList();
 243
 0244            var propiedadesOrdenadas2 = new PropertyDescriptorCollection(propiedadesOrdenadas.ToArray());
 245
 0246            foreach (PropertyDescriptor descriptor in propiedadesOrdenadas2)
 247            {
 0248                string name = descriptor.Name;
 0249                object value = descriptor.GetValue(obj) ?? "";
 0250                ShowMessageInfo(name.PadRight(20) + " = " + value);
 251            }
 0252        }
 253    }
 254}