< Summary

Information
Class: MRA.Infrastructure.UserInput.ConsoleProvider
Assembly: MRA.Infrastructure
File(s): D:\a\MiguelRomerART\MiguelRomerART\MRA.Infrastructure\UserInput\ConsoleProvider.cs
Line coverage
1%
Covered lines: 2
Uncovered lines: 127
Coverable lines: 129
Total lines: 256
Line coverage: 1.5%
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.Infrastructure\UserInput\ConsoleProvider.cs

#LineLine coverage
 1using System.ComponentModel;
 2
 3namespace MRA.Infrastructure.UserInput;
 4
 5public class ConsoleProvider : IUserInputProvider
 6{
 17    private int PAD_RIGHT = 10;
 18    public bool ShowMessageType = true;
 9
 10
 11    public void ShowMessageTrace(string message)
 12    {
 013        Console.ForegroundColor = ConsoleColor.DarkGray;
 014        Console.WriteLine(message);
 015        Console.ForegroundColor = ConsoleColor.White;
 016    }
 17    public void ShowMessageDebug(string message)
 18    {
 019        Console.ForegroundColor = ConsoleColor.DarkGreen;
 020        Console.WriteLine(message);
 021        Console.ForegroundColor = ConsoleColor.White;
 022    }
 23    public void ShowMessageInfo(string message)
 24    {
 025        Console.ForegroundColor = ConsoleColor.Cyan;
 026        Console.WriteLine(message);
 027        Console.ForegroundColor = ConsoleColor.White;
 028    }
 29
 30    public void ShowMessageWarning(string message)
 31    {
 032        Console.ForegroundColor = ConsoleColor.Yellow;
 033        Console.WriteLine(message);
 034        Console.ForegroundColor = ConsoleColor.White;
 035    }
 36
 37    public void ShowMessageSuccess(string message)
 38    {
 039        Console.ForegroundColor = ConsoleColor.Green;
 040        Console.WriteLine(message);
 041        Console.ForegroundColor = ConsoleColor.White;
 042    }
 43    public void ShowMessageError(string message)
 44    {
 045        Console.ForegroundColor = ConsoleColor.Red;
 046        Console.WriteLine(message);
 047        Console.ForegroundColor = ConsoleColor.White;
 048    }
 49    public void ShowMessageCritical(string message)
 50    {
 051        var previous = Console.BackgroundColor;
 052        Console.BackgroundColor = ConsoleColor.Red;
 053        Console.ForegroundColor = ConsoleColor.White;
 054        Console.WriteLine(message);
 055        Console.BackgroundColor = previous;
 056        Console.ForegroundColor = ConsoleColor.White;
 057    }
 58
 59    public void ShowMessagePrevious(bool isNew, string previous)
 60    {
 061        if (!isNew)
 62        {
 063            Console.ForegroundColor = ConsoleColor.DarkGray;
 064            Console.Write("".PadRight(PAD_RIGHT));
 065            Console.Write("[Default: '");
 66
 067            Console.ForegroundColor = ConsoleColor.Green;
 068            Console.Write(previous);
 69
 070            Console.ForegroundColor = ConsoleColor.DarkGray;
 071            Console.WriteLine("'][Leave empty to use it]");
 72
 073            Console.ForegroundColor = ConsoleColor.White;
 74        }
 075    }
 76
 77    public void ShowMessage(bool isNew, string previous, string field)
 78    {
 079        Console.ForegroundColor = ConsoleColor.White;
 080        ShowMessageInfo("------------------------------------------------------");
 081        System.Console.WriteLine("INPUT:".PadRight(PAD_RIGHT) + field.ToUpper());
 082        ShowMessagePrevious(isNew, previous);
 083    }
 84
 85    public void ShowMessage(string field)
 86    {
 087        Console.ForegroundColor = ConsoleColor.White;
 088        System.Console.WriteLine(field);
 089    }
 90
 91    public string ReadValueFromConsole()
 92    {
 093        Console.ForegroundColor = ConsoleColor.White;
 094        Console.Write("> ");
 095        return Console.ReadLine();
 96    }
 97
 98    public string ReadValue(bool isNew, string previous)
 99    {
 100
 0101        var input = ReadValueFromConsole();
 0102        if (!isNew && String.IsNullOrEmpty(input))
 103        {
 0104            input = previous;
 105        }
 0106        ShowValueSet(input.ToString());
 0107        return input;
 108    }
 109
 110    public string ReadValue()
 111    {
 112
 0113        var input = ReadValueFromConsole();
 0114        ShowValueSet(input.ToString());
 0115        return input;
 116    }
 117
 118    public int FillIntValue(bool isNew, int previous, string field, Dictionary<int, string> dictionary)
 119    {
 0120        ShowMessageInfo("------------------------------------------------------");
 0121        ShowMessageInfo(field.ToUpper() + ":");
 122
 0123        foreach (var type in dictionary)
 124        {
 0125            ShowMessageInfo(type.Key.ToString().PadRight(5) + "= " + type.Value);
 126        }
 127
 0128        if (!isNew)
 129        {
 0130            System.Console.WriteLine("  ");
 0131            ShowMessagePrevious(isNew, dictionary[previous].ToString());
 132        }
 133
 0134        var input = ReadValueFromConsole();
 0135        if (!isNew && String.IsNullOrEmpty(input))
 136        {
 0137            ShowValueSet(dictionary[previous]);
 0138            return previous;
 139        }
 140        else
 141        {
 0142            int.TryParse(input, out int numeroEntero);
 0143            ShowValueSet(dictionary[numeroEntero]);
 0144            return numeroEntero;
 145        }
 146    }
 147
 148    public int FillFreeIntValue(bool isNew, int previous, string field)
 149    {
 0150        ShowMessage(isNew, previous.ToString(), field);
 151
 0152        var input = ReadValueFromConsole();
 0153        if (!isNew && String.IsNullOrEmpty(input))
 154        {
 0155            ShowValueSet(previous.ToString());
 0156            return previous;
 157        }
 158        else
 159        {
 0160            int.TryParse(input, out int numeroEntero);
 0161            ShowValueSet(numeroEntero.ToString());
 0162            return numeroEntero;
 163        }
 164    }
 165
 166
 167    public bool FillBoolValue(string field)
 168    {
 0169        ShowMessage(field);
 0170        Console.ForegroundColor = ConsoleColor.DarkGray;
 0171        System.Console.WriteLine("".PadRight(PAD_RIGHT) + "[Y|y: 'True']");
 0172        Console.ForegroundColor = ConsoleColor.White;
 173
 0174        var input = ReadValueFromConsole();
 175
 0176        bool value = false;
 0177        if (input.ToLower().Equals("y"))
 178        {
 0179            value = true;
 180        }
 181
 0182        ShowValueSet(value.ToString());
 0183        return value;
 184    }
 185
 186    public bool FillBoolValue(bool isNew, bool previous, string field)
 187    {
 0188        ShowMessage(isNew, previous.ToString(), field);
 0189        Console.ForegroundColor = ConsoleColor.DarkGray;
 0190        System.Console.WriteLine("".PadRight(PAD_RIGHT) + "[Y|y: 'True']");
 0191        Console.ForegroundColor = ConsoleColor.White;
 192
 0193        var input = ReadValueFromConsole();
 194
 0195        if (!isNew && String.IsNullOrEmpty(input))
 196        {
 0197            ShowValueSet(previous.ToString());
 0198            return previous;
 199        }
 200        else
 201        {
 0202            bool value = false;
 0203            if (input.ToLower().Equals("y"))
 204            {
 0205                value = true;
 206            }
 207
 0208            ShowValueSet(value.ToString());
 0209            return value;
 210        }
 211    }
 212
 213    public void ShowValueSet(string value)
 214    {
 0215        Console.WriteLine("");
 0216        Console.ForegroundColor = ConsoleColor.Green;
 0217        Console.Write("VALUE ".PadRight(PAD_RIGHT));
 0218        Console.WriteLine((String.IsNullOrEmpty(value) ? "''" : value));
 0219        Console.ForegroundColor = ConsoleColor.White;
 0220        Console.WriteLine("");
 0221    }
 222
 223    public string FillStringValue(bool isNew, string previous, string field)
 224    {
 0225        ShowMessage(isNew, previous, field);
 0226        return ReadValue(isNew, previous);
 227    }
 228
 229    public string ReadStringValue(string prompt)
 230    {
 0231        ShowMessage(prompt);
 0232        return ReadValue();
 233    }
 234
 235    public void PrintPropreties(object obj)
 236    {
 237        // Obtener todas las propiedades
 0238        PropertyDescriptorCollection propiedades = TypeDescriptor.GetProperties(obj);
 239
 240        // Ordenar las propiedades por nombre
 0241        var propiedadesOrdenadas = propiedades.Sort()
 0242                                              .Cast<PropertyDescriptor>()
 0243                                              .ToList();
 244
 0245        var propiedadesOrdenadas2 = new PropertyDescriptorCollection(propiedadesOrdenadas.ToArray());
 246
 0247        foreach (PropertyDescriptor descriptor in propiedadesOrdenadas2)
 248        {
 0249            string name = descriptor.Name;
 0250            object value = descriptor.GetValue(obj) ?? "";
 0251            ShowMessageInfo(name.PadRight(20) + " = " + value);
 252        }
 0253    }
 254
 0255    public void ReadKey() => Console.ReadKey();
 256}