| | 1 | | using System.ComponentModel; |
| | 2 | |
|
| | 3 | | namespace MRA.Services.Helpers |
| | 4 | | { |
| | 5 | | public class ConsoleHelper |
| | 6 | | { |
| 0 | 7 | | private int PAD_RIGHT = 10; |
| 0 | 8 | | public bool ShowMessageType = true; |
| | 9 | |
|
| | 10 | | public void ShowMessageTrace(string message) |
| | 11 | | { |
| 0 | 12 | | Console.ForegroundColor = ConsoleColor.DarkGray; |
| 0 | 13 | | Console.WriteLine(message); |
| 0 | 14 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 15 | | } |
| | 16 | | public void ShowMessageDebug(string message) |
| | 17 | | { |
| 0 | 18 | | Console.ForegroundColor = ConsoleColor.DarkGreen; |
| 0 | 19 | | Console.WriteLine(message); |
| 0 | 20 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 21 | | } |
| | 22 | | public void ShowMessageInfo(string message) |
| | 23 | | { |
| 0 | 24 | | Console.ForegroundColor = ConsoleColor.Cyan; |
| 0 | 25 | | Console.WriteLine(message); |
| 0 | 26 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 27 | | } |
| | 28 | |
|
| | 29 | | public void ShowMessageWarning(string message) |
| | 30 | | { |
| 0 | 31 | | Console.ForegroundColor = ConsoleColor.Yellow; |
| 0 | 32 | | Console.WriteLine(message); |
| 0 | 33 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | public void ShowMessageSuccess(string message) |
| | 37 | | { |
| 0 | 38 | | Console.ForegroundColor = ConsoleColor.Green; |
| 0 | 39 | | Console.WriteLine(message); |
| 0 | 40 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 41 | | } |
| | 42 | | public void ShowMessageError(string message) |
| | 43 | | { |
| 0 | 44 | | Console.ForegroundColor = ConsoleColor.Red; |
| 0 | 45 | | Console.WriteLine(message); |
| 0 | 46 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 47 | | } |
| | 48 | | public void ShowMessageCritical(string message) |
| | 49 | | { |
| 0 | 50 | | var previous = Console.BackgroundColor; |
| 0 | 51 | | Console.BackgroundColor = ConsoleColor.Red; |
| 0 | 52 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 53 | | Console.WriteLine(message); |
| 0 | 54 | | Console.BackgroundColor = previous; |
| 0 | 55 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | public void ShowMessagePrevious(bool isNew, string previous) |
| | 59 | | { |
| 0 | 60 | | if (!isNew) |
| | 61 | | { |
| 0 | 62 | | Console.ForegroundColor = ConsoleColor.DarkGray; |
| 0 | 63 | | Console.Write("".PadRight(PAD_RIGHT)); |
| 0 | 64 | | Console.Write("[Default: '"); |
| | 65 | |
|
| 0 | 66 | | Console.ForegroundColor = ConsoleColor.Green; |
| 0 | 67 | | Console.Write(previous); |
| | 68 | |
|
| 0 | 69 | | Console.ForegroundColor = ConsoleColor.DarkGray; |
| 0 | 70 | | Console.WriteLine("'][Leave empty to use it]"); |
| | 71 | |
|
| 0 | 72 | | Console.ForegroundColor = ConsoleColor.White; |
| | 73 | | } |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | public void ShowMessage(bool isNew, string previous, string field) |
| | 77 | | { |
| 0 | 78 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 79 | | ShowMessageInfo("------------------------------------------------------"); |
| 0 | 80 | | System.Console.WriteLine("INPUT:".PadRight(PAD_RIGHT) + field.ToUpper()); |
| 0 | 81 | | ShowMessagePrevious(isNew, previous); |
| 0 | 82 | | } |
| | 83 | |
|
| | 84 | | public void ShowMessage(string field) |
| | 85 | | { |
| 0 | 86 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 87 | | System.Console.WriteLine(field); |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | public string ReadValueFromConsole() |
| | 91 | | { |
| 0 | 92 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 93 | | Console.Write("> "); |
| 0 | 94 | | return Console.ReadLine(); |
| | 95 | | } |
| | 96 | |
|
| | 97 | | public string ReadValue(bool isNew, string previous) |
| | 98 | | { |
| | 99 | |
|
| 0 | 100 | | var input = ReadValueFromConsole(); |
| 0 | 101 | | if (!isNew && String.IsNullOrEmpty(input)) |
| | 102 | | { |
| 0 | 103 | | input = previous; |
| | 104 | | } |
| 0 | 105 | | ShowValueSet(input.ToString()); |
| 0 | 106 | | return input; |
| | 107 | | } |
| | 108 | |
|
| | 109 | | public string ReadValue() |
| | 110 | | { |
| | 111 | |
|
| 0 | 112 | | var input = ReadValueFromConsole(); |
| 0 | 113 | | ShowValueSet(input.ToString()); |
| 0 | 114 | | return input; |
| | 115 | | } |
| | 116 | |
|
| | 117 | | public int FillIntValue(bool isNew, int previous, string field, Dictionary<int, string> dictionary) |
| | 118 | | { |
| 0 | 119 | | ShowMessageInfo("------------------------------------------------------"); |
| 0 | 120 | | ShowMessageInfo(field.ToUpper() + ":"); |
| | 121 | |
|
| 0 | 122 | | foreach (var type in dictionary) |
| | 123 | | { |
| 0 | 124 | | ShowMessageInfo(type.Key.ToString().PadRight(5) + "= " + type.Value); |
| | 125 | | } |
| | 126 | |
|
| 0 | 127 | | if (!isNew) |
| | 128 | | { |
| 0 | 129 | | System.Console.WriteLine(" "); |
| 0 | 130 | | ShowMessagePrevious(isNew, dictionary[previous].ToString()); |
| | 131 | | } |
| | 132 | |
|
| 0 | 133 | | var input = ReadValueFromConsole(); |
| 0 | 134 | | if (!isNew && String.IsNullOrEmpty(input)) |
| | 135 | | { |
| 0 | 136 | | ShowValueSet(dictionary[previous]); |
| 0 | 137 | | return previous; |
| | 138 | | } |
| | 139 | | else |
| | 140 | | { |
| 0 | 141 | | int.TryParse(input, out int numeroEntero); |
| 0 | 142 | | ShowValueSet(dictionary[numeroEntero]); |
| 0 | 143 | | return numeroEntero; |
| | 144 | | } |
| | 145 | | } |
| | 146 | |
|
| | 147 | | public int FillFreeIntValue(bool isNew, int previous, string field) |
| | 148 | | { |
| 0 | 149 | | ShowMessage(isNew, previous.ToString(), field); |
| | 150 | |
|
| 0 | 151 | | var input = ReadValueFromConsole(); |
| 0 | 152 | | if (!isNew && String.IsNullOrEmpty(input)) |
| | 153 | | { |
| 0 | 154 | | ShowValueSet(previous.ToString()); |
| 0 | 155 | | return previous; |
| | 156 | | } |
| | 157 | | else |
| | 158 | | { |
| 0 | 159 | | int.TryParse(input, out int numeroEntero); |
| 0 | 160 | | ShowValueSet(numeroEntero.ToString()); |
| 0 | 161 | | return numeroEntero; |
| | 162 | | } |
| | 163 | | } |
| | 164 | |
|
| | 165 | |
|
| | 166 | | public bool FillBoolValue(string field) |
| | 167 | | { |
| 0 | 168 | | ShowMessage(field); |
| 0 | 169 | | Console.ForegroundColor = ConsoleColor.DarkGray; |
| 0 | 170 | | System.Console.WriteLine("".PadRight(PAD_RIGHT) + "[Y|y: 'True']"); |
| 0 | 171 | | Console.ForegroundColor = ConsoleColor.White; |
| | 172 | |
|
| 0 | 173 | | var input = ReadValueFromConsole(); |
| | 174 | |
|
| 0 | 175 | | bool value = false; |
| 0 | 176 | | if (input.ToLower().Equals("y")) |
| | 177 | | { |
| 0 | 178 | | value = true; |
| | 179 | | } |
| | 180 | |
|
| 0 | 181 | | ShowValueSet(value.ToString()); |
| 0 | 182 | | return value; |
| | 183 | | } |
| | 184 | |
|
| | 185 | | public bool FillBoolValue(bool isNew, bool previous, string field) |
| | 186 | | { |
| 0 | 187 | | ShowMessage(isNew, previous.ToString(), field); |
| 0 | 188 | | Console.ForegroundColor = ConsoleColor.DarkGray; |
| 0 | 189 | | System.Console.WriteLine("".PadRight(PAD_RIGHT) + "[Y|y: 'True']"); |
| 0 | 190 | | Console.ForegroundColor = ConsoleColor.White; |
| | 191 | |
|
| 0 | 192 | | var input = ReadValueFromConsole(); |
| | 193 | |
|
| 0 | 194 | | if (!isNew && String.IsNullOrEmpty(input)) |
| | 195 | | { |
| 0 | 196 | | ShowValueSet(previous.ToString()); |
| 0 | 197 | | return previous; |
| | 198 | | } |
| | 199 | | else |
| | 200 | | { |
| 0 | 201 | | bool value = false; |
| 0 | 202 | | if (input.ToLower().Equals("y")) |
| | 203 | | { |
| 0 | 204 | | value = true; |
| | 205 | | } |
| | 206 | |
|
| 0 | 207 | | ShowValueSet(value.ToString()); |
| 0 | 208 | | return value; |
| | 209 | | } |
| | 210 | | } |
| | 211 | |
|
| | 212 | | public void ShowValueSet(string value) |
| | 213 | | { |
| 0 | 214 | | Console.WriteLine(""); |
| 0 | 215 | | Console.ForegroundColor = ConsoleColor.Green; |
| 0 | 216 | | Console.Write("VALUE ".PadRight(PAD_RIGHT)); |
| 0 | 217 | | Console.WriteLine((String.IsNullOrEmpty(value) ? "''" : value)); |
| 0 | 218 | | Console.ForegroundColor = ConsoleColor.White; |
| 0 | 219 | | Console.WriteLine(""); |
| 0 | 220 | | } |
| | 221 | |
|
| | 222 | | public string FillStringValue(bool isNew, string previous, string field) |
| | 223 | | { |
| 0 | 224 | | ShowMessage(isNew, previous, field); |
| 0 | 225 | | return ReadValue(isNew, previous); |
| | 226 | | } |
| | 227 | |
|
| | 228 | | public string FillStringValue(string field) |
| | 229 | | { |
| 0 | 230 | | ShowMessage(field); |
| 0 | 231 | | return ReadValue(); |
| | 232 | | } |
| | 233 | |
|
| | 234 | | public void PrintPropreties(object obj) |
| | 235 | | { |
| | 236 | | // Obtener todas las propiedades |
| 0 | 237 | | PropertyDescriptorCollection propiedades = TypeDescriptor.GetProperties(obj); |
| | 238 | |
|
| | 239 | | // Ordenar las propiedades por nombre |
| 0 | 240 | | var propiedadesOrdenadas = propiedades.Sort() |
| 0 | 241 | | .Cast<PropertyDescriptor>() |
| 0 | 242 | | .ToList(); |
| | 243 | |
|
| 0 | 244 | | var propiedadesOrdenadas2 = new PropertyDescriptorCollection(propiedadesOrdenadas.ToArray()); |
| | 245 | |
|
| 0 | 246 | | foreach (PropertyDescriptor descriptor in propiedadesOrdenadas2) |
| | 247 | | { |
| 0 | 248 | | string name = descriptor.Name; |
| 0 | 249 | | object value = descriptor.GetValue(obj) ?? ""; |
| 0 | 250 | | ShowMessageInfo(name.PadRight(20) + " = " + value); |
| | 251 | | } |
| 0 | 252 | | } |
| | 253 | | } |
| | 254 | | } |