| | 1 | | namespace Towel; |
| | 2 | |
|
| | 3 | | /// <summary>Contains static helper methods for <see cref="Console"/>.</summary> |
| | 4 | | public static class ConsoleHelper |
| | 5 | | { |
| | 6 | | /// <summary>Flushes the console input buffer.</summary> |
| | 7 | | /// <param name="intercept"> |
| | 8 | | /// Determines whether to display the pressed key in the console window. |
| | 9 | | /// true to not display the pressed key; otherwise, false. |
| | 10 | | /// </param> |
| | 11 | | public static void FlushInputBuffer(bool intercept = true) |
| 0 | 12 | | { |
| 0 | 13 | | while (Console.KeyAvailable) |
| 0 | 14 | | { |
| 0 | 15 | | Console.ReadKey(intercept); |
| 0 | 16 | | } |
| 0 | 17 | | } |
| | 18 | |
|
| | 19 | | /// <summary>Prompts the user to press [enter] in the console before continuing.</summary> |
| | 20 | | /// <param name="key">The key to wait for the user to press before continuing.</param> |
| | 21 | | public static void PressToContinue(ConsoleKey key = ConsoleKey.Enter) |
| 0 | 22 | | { |
| 0 | 23 | | if (!key.IsDefined()) |
| 0 | 24 | | { |
| 0 | 25 | | throw new ArgumentOutOfRangeException(nameof(key), key, $"{nameof(key)} is not a defined value in the {nameof(Cons |
| | 26 | | } |
| 0 | 27 | | while (Console.ReadKey(true).Key != key) |
| 0 | 28 | | { |
| 0 | 29 | | continue; |
| | 30 | | } |
| 0 | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary>Prompts the user to press [enter] in the console before continuing.</summary> |
| | 34 | | /// <param name="prompt">The prompt to display to the user. Default: "Press [enter] to continue...".</param> |
| | 35 | | /// <param name="key">The key to wait for the user to press before continuing.</param> |
| | 36 | | public static void PromptPressToContinue(string? prompt = null, ConsoleKey key = ConsoleKey.Enter) |
| 0 | 37 | | { |
| 0 | 38 | | if (!key.IsDefined()) |
| 0 | 39 | | { |
| 0 | 40 | | throw new ArgumentOutOfRangeException(nameof(key), key, $"{nameof(key)} is not a defined value in the {nameof(Cons |
| | 41 | | } |
| 0 | 42 | | prompt ??= $"Press [{key}] to continue..."; |
| 0 | 43 | | Console.Write(prompt); |
| 0 | 44 | | PressToContinue(key); |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | /// <summary>Prompts the user to select a menu option in the console before continuing.</summary> |
| | 48 | | /// <param name="options">The options of the menu.</param> |
| | 49 | | public static void IntMenu(params (string DisplayName, Action Action)[] options) => |
| 0 | 50 | | IntMenu(null, null, null, options); |
| | 51 | |
|
| | 52 | | /// <summary>Prompts the user to select a menu option in the console before continuing.</summary> |
| | 53 | | /// <param name="title">The title of the menu.</param> |
| | 54 | | /// <param name="prompt">The prompt message to display when requesting console input from the user.</param> |
| | 55 | | /// <param name="invalidMessage">The message to display if invalid input is detected.</param> |
| | 56 | | /// <param name="options">The options of the menu.</param> |
| | 57 | | public static void IntMenu( |
| | 58 | | string? title = null, |
| | 59 | | string? prompt = null, |
| | 60 | | string? invalidMessage = null, |
| | 61 | | params (string DisplayName, Action Action)[] options) |
| 0 | 62 | | { |
| 0 | 63 | | if (options is null) throw new ArgumentNullException(nameof(options)); |
| 0 | 64 | | if (options.Length <= 0) |
| 0 | 65 | | { |
| 0 | 66 | | throw new ArgumentException($"{nameof(options)} is empty", nameof(options)); |
| | 67 | | } |
| 0 | 68 | | prompt ??= $"Choose an option (1-{options.Length}): "; |
| 0 | 69 | | invalidMessage ??= "Invalid Input. Try Again..."; |
| 0 | 70 | | if (title is not null) |
| 0 | 71 | | { |
| 0 | 72 | | Console.WriteLine(title); |
| 0 | 73 | | } |
| 0 | 74 | | for (int i = 0; i < options.Length; i++) |
| 0 | 75 | | { |
| 0 | 76 | | Console.WriteLine($"{i + 1}. {options[i].DisplayName ?? "null"}"); |
| 0 | 77 | | } |
| | 78 | | int inputValue; |
| 0 | 79 | | Console.Write(prompt); |
| 0 | 80 | | while (!int.TryParse(Console.ReadLine(), out inputValue) || inputValue < 1 || options.Length < inputValue) |
| 0 | 81 | | { |
| 0 | 82 | | Console.WriteLine(invalidMessage); |
| 0 | 83 | | Console.Write(prompt); |
| 0 | 84 | | } |
| 0 | 85 | | options[inputValue - 1].Action?.Invoke(); |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | /// <summary>Gets console input from the user.</summary> |
| | 89 | | /// <typeparam name="T">The generic type of console input to get from the user.</typeparam> |
| | 90 | | /// <param name="prompt">The prompt message to display when requesting console input from the user.</param> |
| | 91 | | /// <param name="invalidMessage">The message to display if invalid input is detected.</param> |
| | 92 | | /// <param name="tryParse">The <see cref="TryParse"/> method for converting <see cref="string"/> into a <typeparamref |
| | 93 | | /// <param name="validation">The predicate for validating the value of the input.</param> |
| | 94 | | /// <returns>The validated value of the console input provided by the user.</returns> |
| | 95 | | public static T? GetInput<T>( |
| | 96 | | string? prompt = null, |
| | 97 | | string? invalidMessage = null, |
| | 98 | | Func<string, (bool Success, T? Value)>? tryParse = null, |
| | 99 | | Predicate<T?>? validation = null) |
| 0 | 100 | | { |
| 0 | 101 | | if (tryParse is null && (typeof(T) != typeof(string) && !typeof(T).IsEnum && Meta.GetTryParseMethod<T>() is null)) |
| 0 | 102 | | { |
| 0 | 103 | | throw new InvalidOperationException($"Using {nameof(ConsoleHelper)}.{nameof(GetInput)} without providing a {nameof |
| | 104 | | } |
| 0 | 105 | | tryParse ??= typeof(T) == typeof(string) |
| 0 | 106 | | ? s => (true, (T)(object)s) |
| 0 | 107 | | : TryParse<T>; |
| 0 | 108 | | validation ??= v => true; |
| 0 | 109 | | GetInput: |
| 0 | 110 | | Console.Write(prompt ?? $"Input a {typeof(T).Name} value: "); |
| 0 | 111 | | string? readLine = Console.ReadLine(); |
| 0 | 112 | | if (readLine is null) |
| 0 | 113 | | { |
| 0 | 114 | | throw new ArgumentException($"{nameof(System)}.{nameof(Console)}.{nameof(Console.ReadLine)} returned null"); |
| | 115 | | } |
| 0 | 116 | | var (success, value) = tryParse(readLine); |
| 0 | 117 | | if (!success || !validation(value)) |
| 0 | 118 | | { |
| 0 | 119 | | Console.WriteLine(invalidMessage ?? $"Invalid input. Try again..."); |
| 0 | 120 | | goto GetInput; |
| | 121 | | } |
| 0 | 122 | | return value; |
| 0 | 123 | | } |
| | 124 | |
|
| | 125 | | /// <summary>Similar to <see cref="Console.ReadLine"/> but with hidden input characters.</summary> |
| | 126 | | /// <param name="shownCharacter">The display character to use for all input.</param> |
| | 127 | | /// <returns>The <see cref="string"/> input provided by the user.</returns> |
| | 128 | | public static string HiddenReadLine(char shownCharacter = '*') |
| 0 | 129 | | { |
| 0 | 130 | | System.Collections.Generic.List<char> list = new(); |
| 0 | 131 | | HiddenReadLineBase( |
| 0 | 132 | | shownCharacter: shownCharacter, |
| 0 | 133 | | GetLength: () => list.Count, |
| 0 | 134 | | Append: list.Add, |
| 0 | 135 | | InsertAt: list.Insert, |
| 0 | 136 | | RemoveAt: list.RemoveAt, |
| 0 | 137 | | RemoveRange: list.RemoveRange, |
| 0 | 138 | | Clear: list.Clear); |
| 0 | 139 | | return string.Concat(list); |
| 0 | 140 | | } |
| | 141 | |
|
| | 142 | | internal static void HiddenReadLineBase( |
| | 143 | | char shownCharacter, |
| | 144 | | Func<int> GetLength, |
| | 145 | | Action<char> Append, |
| | 146 | | Action<int, char> InsertAt, |
| | 147 | | Action<int> RemoveAt, |
| | 148 | | Action<int, int>? RemoveRange = null, |
| | 149 | | Action? Clear = null) |
| 0 | 150 | | { |
| 0 | 151 | | int position = 0; |
| | 152 | |
|
| 0 | 153 | | RemoveRange ??= (index, length) => |
| 0 | 154 | | { |
| 0 | 155 | | for (int i = 0; i < length; i++) |
| 0 | 156 | | { |
| 0 | 157 | | RemoveAt(index); |
| 0 | 158 | | } |
| 0 | 159 | | }; |
| | 160 | |
|
| 0 | 161 | | Clear ??= () => RemoveRange(0, GetLength()); |
| | 162 | |
|
| | 163 | | void MoveToOrigin() => MoveNegative(position); |
| | 164 | |
|
| | 165 | | void MoveToTail() => MovePositive(GetLength() - position); |
| | 166 | |
|
| 0 | 167 | | while (true) |
| 0 | 168 | | { |
| 0 | 169 | | ConsoleKeyInfo keyInfo = Console.ReadKey(true); |
| 0 | 170 | | if (keyInfo.Key is ConsoleKey.Enter) |
| 0 | 171 | | { |
| 0 | 172 | | if (!keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control) && |
| 0 | 173 | | !keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift) && |
| 0 | 174 | | !keyInfo.Modifiers.HasFlag(ConsoleModifiers.Alt)) |
| 0 | 175 | | { |
| 0 | 176 | | MovePositive(GetLength() - position); |
| 0 | 177 | | Console.WriteLine(); |
| 0 | 178 | | break; |
| | 179 | | } |
| 0 | 180 | | } |
| 0 | 181 | | else if (keyInfo.Key is ConsoleKey.Backspace) |
| 0 | 182 | | { |
| 0 | 183 | | if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control)) |
| 0 | 184 | | { |
| 0 | 185 | | MoveToOrigin(); |
| 0 | 186 | | ConsoleWriteString(new string(shownCharacter, GetLength() - position) + new string(' ', position)); |
| 0 | 187 | | MoveNegative(GetLength()); |
| 0 | 188 | | RemoveRange(0, position); |
| 0 | 189 | | position = 0; |
| 0 | 190 | | } |
| 0 | 191 | | else if (position > 0) |
| 0 | 192 | | { |
| 0 | 193 | | if (position == GetLength()) |
| 0 | 194 | | { |
| 0 | 195 | | MoveNegative(1); |
| 0 | 196 | | ConsoleWriteChar(' '); |
| 0 | 197 | | MoveNegative(1); |
| 0 | 198 | | } |
| | 199 | | else |
| 0 | 200 | | { |
| 0 | 201 | | MoveToTail(); |
| 0 | 202 | | MoveNegative(1); |
| 0 | 203 | | ConsoleWriteChar(' '); |
| 0 | 204 | | MoveNegative(GetLength() - position + 1); |
| 0 | 205 | | } |
| 0 | 206 | | RemoveAt(position - 1); |
| 0 | 207 | | position--; |
| 0 | 208 | | } |
| 0 | 209 | | } |
| 0 | 210 | | else if (keyInfo.Key is ConsoleKey.Delete) |
| 0 | 211 | | { |
| 0 | 212 | | if (!keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control) && |
| 0 | 213 | | !keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift) && |
| 0 | 214 | | !keyInfo.Modifiers.HasFlag(ConsoleModifiers.Alt)) |
| 0 | 215 | | { |
| 0 | 216 | | if (position < GetLength()) |
| 0 | 217 | | { |
| 0 | 218 | | int left = Console.CursorLeft; |
| 0 | 219 | | int top = Console.CursorTop; |
| 0 | 220 | | MoveToTail(); |
| 0 | 221 | | MoveNegative(1); |
| 0 | 222 | | ConsoleWriteChar(' '); |
| 0 | 223 | | Console.CursorLeft = left; |
| 0 | 224 | | Console.CursorTop = top; |
| 0 | 225 | | RemoveAt(position); |
| 0 | 226 | | continue; |
| | 227 | | } |
| 0 | 228 | | } |
| 0 | 229 | | } |
| 0 | 230 | | else if (keyInfo.Key is ConsoleKey.Escape) |
| 0 | 231 | | { |
| 0 | 232 | | if (!keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control) && |
| 0 | 233 | | !keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift) && |
| 0 | 234 | | !keyInfo.Modifiers.HasFlag(ConsoleModifiers.Alt)) |
| 0 | 235 | | { |
| 0 | 236 | | MoveToOrigin(); |
| 0 | 237 | | int left = Console.CursorLeft; |
| 0 | 238 | | int top = Console.CursorTop; |
| 0 | 239 | | ConsoleWriteString(new string(' ', GetLength())); |
| 0 | 240 | | Console.CursorLeft = left; |
| 0 | 241 | | Console.CursorTop = top; |
| 0 | 242 | | Clear(); |
| 0 | 243 | | position = 0; |
| 0 | 244 | | } |
| 0 | 245 | | } |
| 0 | 246 | | else if (keyInfo.Key is ConsoleKey.Home) |
| 0 | 247 | | { |
| 0 | 248 | | if (!keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift) && |
| 0 | 249 | | !keyInfo.Modifiers.HasFlag(ConsoleModifiers.Alt)) |
| 0 | 250 | | { |
| 0 | 251 | | if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control)) |
| 0 | 252 | | { |
| 0 | 253 | | MoveToOrigin(); |
| 0 | 254 | | ConsoleWriteString(new string(shownCharacter, GetLength() - position) + new string(' ', position)); |
| 0 | 255 | | MoveNegative(GetLength()); |
| 0 | 256 | | RemoveRange(0, position); |
| 0 | 257 | | position = 0; |
| 0 | 258 | | } |
| | 259 | | else |
| 0 | 260 | | { |
| 0 | 261 | | MoveToOrigin(); |
| 0 | 262 | | position = 0; |
| 0 | 263 | | } |
| 0 | 264 | | } |
| 0 | 265 | | } |
| 0 | 266 | | else if (keyInfo.Key is ConsoleKey.End) |
| 0 | 267 | | { |
| 0 | 268 | | if (!keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift) && |
| 0 | 269 | | !keyInfo.Modifiers.HasFlag(ConsoleModifiers.Alt)) |
| 0 | 270 | | { |
| 0 | 271 | | if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control)) |
| 0 | 272 | | { |
| 0 | 273 | | MoveToOrigin(); |
| 0 | 274 | | ConsoleWriteString(new string(shownCharacter, position) + new string(' ', GetLength() - position)); |
| 0 | 275 | | MoveNegative(GetLength() - position); |
| 0 | 276 | | RemoveRange(position, GetLength() - position); |
| 0 | 277 | | } |
| | 278 | | else |
| 0 | 279 | | { |
| 0 | 280 | | MoveToTail(); |
| 0 | 281 | | position = GetLength(); |
| 0 | 282 | | } |
| 0 | 283 | | } |
| 0 | 284 | | } |
| 0 | 285 | | else if (keyInfo.Key is ConsoleKey.LeftArrow) |
| 0 | 286 | | { |
| 0 | 287 | | if (!keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift) && |
| 0 | 288 | | !keyInfo.Modifiers.HasFlag(ConsoleModifiers.Alt)) |
| 0 | 289 | | { |
| 0 | 290 | | if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control)) |
| 0 | 291 | | { |
| 0 | 292 | | MoveToOrigin(); |
| 0 | 293 | | position = 0; |
| 0 | 294 | | } |
| | 295 | | else |
| 0 | 296 | | { |
| 0 | 297 | | if (position > 0) |
| 0 | 298 | | { |
| 0 | 299 | | MoveNegative(1); |
| 0 | 300 | | position--; |
| 0 | 301 | | } |
| 0 | 302 | | } |
| 0 | 303 | | } |
| 0 | 304 | | } |
| 0 | 305 | | else if (keyInfo.Key is ConsoleKey.RightArrow) |
| 0 | 306 | | { |
| 0 | 307 | | if (!keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift) && |
| 0 | 308 | | !keyInfo.Modifiers.HasFlag(ConsoleModifiers.Alt)) |
| 0 | 309 | | { |
| 0 | 310 | | if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control)) |
| 0 | 311 | | { |
| 0 | 312 | | MoveToTail(); |
| 0 | 313 | | position = GetLength(); |
| 0 | 314 | | } |
| | 315 | | else |
| 0 | 316 | | { |
| 0 | 317 | | if (position < GetLength()) |
| 0 | 318 | | { |
| 0 | 319 | | MovePositive(1); |
| 0 | 320 | | position++; |
| 0 | 321 | | } |
| 0 | 322 | | } |
| 0 | 323 | | } |
| 0 | 324 | | } |
| | 325 | | else |
| 0 | 326 | | { |
| 0 | 327 | | if (keyInfo.KeyChar is not '\0') |
| 0 | 328 | | { |
| 0 | 329 | | if (position == GetLength()) |
| 0 | 330 | | { |
| 0 | 331 | | ConsoleWriteChar(shownCharacter); |
| 0 | 332 | | Append(keyInfo.KeyChar); |
| 0 | 333 | | position++; |
| 0 | 334 | | } |
| | 335 | | else |
| 0 | 336 | | { |
| 0 | 337 | | int left = Console.CursorLeft; |
| 0 | 338 | | int top = Console.CursorTop; |
| 0 | 339 | | MoveToTail(); |
| 0 | 340 | | ConsoleWriteChar(shownCharacter); |
| 0 | 341 | | Console.CursorLeft = left; |
| 0 | 342 | | Console.CursorTop = top; |
| 0 | 343 | | MovePositive(1); |
| 0 | 344 | | InsertAt(position, keyInfo.KeyChar); |
| 0 | 345 | | position++; |
| 0 | 346 | | } |
| 0 | 347 | | } |
| 0 | 348 | | } |
| 0 | 349 | | } |
| 0 | 350 | | } |
| | 351 | |
|
| | 352 | | /// <summary>Animates an elipsis in the console to indicate processing.</summary> |
| | 353 | | /// <param name="condition">The condition of the loop.</param> |
| | 354 | | /// <param name="delay">The delay function.</param> |
| | 355 | | /// <param name="length">The length of the ellipsis.</param> |
| | 356 | | public static void AnimatedEllipsis( |
| | 357 | | Func<bool> condition, |
| | 358 | | Action delay, |
| | 359 | | int length = 3) |
| 0 | 360 | | { |
| 0 | 361 | | if (condition is null) throw new ArgumentNullException(nameof(condition)); |
| 0 | 362 | | if (delay is null) throw new ArgumentNullException(nameof(delay)); |
| 0 | 363 | | if (sourceof(length < 1, out string c1)) throw new ArgumentOutOfRangeException(nameof(length), length, c1); |
| | 364 | |
|
| 0 | 365 | | void MoveToOrigin() => MoveNegative(length); |
| | 366 | |
|
| | 367 | | void Render(int frame) |
| 0 | 368 | | { |
| 0 | 369 | | for (int i = 0; i < frame; i++) |
| 0 | 370 | | { |
| 0 | 371 | | ConsoleWriteChar('.'); |
| 0 | 372 | | } |
| 0 | 373 | | for (int i = frame; i < length; i++) |
| 0 | 374 | | { |
| 0 | 375 | | ConsoleWriteChar(' '); |
| 0 | 376 | | } |
| 0 | 377 | | } |
| | 378 | |
|
| 0 | 379 | | int frame = 0; |
| 0 | 380 | | Render(frame++); |
| 0 | 381 | | while (condition()) |
| 0 | 382 | | { |
| 0 | 383 | | MoveToOrigin(); |
| 0 | 384 | | Render(frame++); |
| 0 | 385 | | delay(); |
| 0 | 386 | | if (frame > length) |
| 0 | 387 | | { |
| 0 | 388 | | frame = 0; |
| 0 | 389 | | } |
| 0 | 390 | | } |
| 0 | 391 | | MoveToOrigin(); |
| 0 | 392 | | ConsoleWriteString(new string(' ', length)); |
| 0 | 393 | | MoveToOrigin(); |
| 0 | 394 | | } |
| | 395 | |
|
| | 396 | | /// <summary>Displays a progress bar in the console.</summary> |
| | 397 | | /// <param name="action">The action to track the progress of.</param> |
| | 398 | | /// <param name="length">The character length of the progress bar (must be >= 6).</param> |
| | 399 | | /// <param name="header">The header character of the progress bar.</param> |
| | 400 | | /// <param name="footer">The footer character of the progress bar.</param> |
| | 401 | | /// <param name="done">The character for represening completed progress.</param> |
| | 402 | | /// <param name="remaining">The character representing ongoing processing.</param> |
| | 403 | | /// <param name="errorDigit">The characters to display in the numerical display when an invalid percentage is recieved |
| | 404 | | /// <param name="postClear">Whether or not to clear the progress bar from the view when complete.</param> |
| | 405 | | public static void ProgressBar( |
| | 406 | | Action<Action<double>> action, |
| | 407 | | int length = 17, |
| | 408 | | char header = '[', |
| | 409 | | char footer = ']', |
| | 410 | | char done = '=', |
| | 411 | | char remaining = '-', |
| | 412 | | char errorDigit = '?', |
| | 413 | | bool postClear = true) |
| 0 | 414 | | { |
| 0 | 415 | | if (action is null) throw new ArgumentNullException(nameof(action)); |
| 0 | 416 | | if (sourceof(length < 6, out string c1)) throw new ArgumentOutOfRangeException(nameof(length), length, c1); |
| | 417 | |
|
| 0 | 418 | | void MoveToOrigin() => MoveNegative(length); |
| | 419 | |
|
| | 420 | | void Render(double percentage) |
| 0 | 421 | | { |
| 0 | 422 | | ConsoleWriteChar(header); |
| 0 | 423 | | if (percentage < 0 || percentage > 100) |
| 0 | 424 | | { |
| 0 | 425 | | ConsoleWriteString(new string(remaining, length - 7)); |
| 0 | 426 | | } |
| | 427 | | else |
| 0 | 428 | | { |
| 0 | 429 | | int doneCount = (int)(percentage / 100 * (length - 7)); |
| 0 | 430 | | int remainingCount = length - 7 - doneCount; |
| 0 | 431 | | ConsoleWriteString(new string(done, doneCount)); |
| 0 | 432 | | ConsoleWriteString(new string(remaining, remainingCount)); |
| 0 | 433 | | } |
| 0 | 434 | | ConsoleWriteChar(footer); |
| 0 | 435 | | ConsoleWriteChar(' '); |
| 0 | 436 | | if (percentage < 0 || percentage > 100) |
| 0 | 437 | | { |
| 0 | 438 | | ConsoleWriteString(new string(errorDigit, 2)); |
| 0 | 439 | | ConsoleWriteChar('%'); |
| 0 | 440 | | ConsoleWriteChar(' '); |
| 0 | 441 | | } |
| | 442 | | else |
| 0 | 443 | | { |
| 0 | 444 | | string percentString = ((int)percentage).ToString(System.Globalization.CultureInfo.InvariantCulture); |
| 0 | 445 | | ConsoleWriteString(percentString); |
| 0 | 446 | | ConsoleWriteChar('%'); |
| 0 | 447 | | for (int i = percentString.Length; i < 3; i++) |
| 0 | 448 | | { |
| 0 | 449 | | ConsoleWriteChar(' '); |
| 0 | 450 | | } |
| 0 | 451 | | } |
| 0 | 452 | | } |
| | 453 | |
|
| 0 | 454 | | Render(0); |
| 0 | 455 | | action(percent => { MoveToOrigin(); Render(percent); }); |
| 0 | 456 | | if (postClear) |
| 0 | 457 | | { |
| 0 | 458 | | MoveToOrigin(); |
| 0 | 459 | | ConsoleWriteString(new string(' ', length)); |
| 0 | 460 | | MoveToOrigin(); |
| 0 | 461 | | } |
| | 462 | | else |
| 0 | 463 | | { |
| 0 | 464 | | MoveToOrigin(); |
| 0 | 465 | | Render(100); |
| 0 | 466 | | } |
| 0 | 467 | | } |
| | 468 | |
|
| | 469 | | internal static void MoveNegative(int count) |
| 0 | 470 | | { |
| 0 | 471 | | int bufferWidth = Console.BufferWidth; |
| 0 | 472 | | int left = Console.CursorLeft; |
| 0 | 473 | | int top = Console.CursorTop; |
| 0 | 474 | | for (int i = 0; i < count; i++) |
| 0 | 475 | | { |
| 0 | 476 | | if (left > 0) |
| 0 | 477 | | { |
| 0 | 478 | | left--; |
| 0 | 479 | | } |
| | 480 | | else |
| 0 | 481 | | { |
| 0 | 482 | | top--; |
| 0 | 483 | | left = bufferWidth - 1; |
| 0 | 484 | | } |
| 0 | 485 | | } |
| 0 | 486 | | Console.CursorLeft = left; |
| 0 | 487 | | Console.CursorTop = top; |
| 0 | 488 | | } |
| | 489 | |
|
| | 490 | | internal static void MovePositive(int count) |
| 0 | 491 | | { |
| 0 | 492 | | int bufferWidth = Console.BufferWidth; |
| 0 | 493 | | int left = Console.CursorLeft; |
| 0 | 494 | | int top = Console.CursorTop; |
| 0 | 495 | | for (int i = 0; i < count; i++) |
| 0 | 496 | | { |
| 0 | 497 | | if (left == bufferWidth - 1) |
| 0 | 498 | | { |
| 0 | 499 | | top++; |
| 0 | 500 | | left = 0; |
| 0 | 501 | | } |
| | 502 | | else |
| 0 | 503 | | { |
| 0 | 504 | | left++; |
| 0 | 505 | | } |
| 0 | 506 | | } |
| 0 | 507 | | Console.CursorLeft = left; |
| 0 | 508 | | Console.CursorTop = top; |
| 0 | 509 | | } |
| | 510 | |
|
| | 511 | | internal static void ConsoleWriteChar(char @char) |
| 0 | 512 | | { |
| 0 | 513 | | int temp = Console.CursorLeft; |
| 0 | 514 | | Console.Write(@char); |
| 0 | 515 | | if (Console.CursorLeft == temp) |
| 0 | 516 | | { |
| 0 | 517 | | MovePositive(1); |
| 0 | 518 | | } |
| 0 | 519 | | } |
| | 520 | |
|
| | 521 | | internal static void ConsoleWriteString(string @string) |
| 0 | 522 | | { |
| 0 | 523 | | foreach (char c in @string) |
| 0 | 524 | | { |
| 0 | 525 | | ConsoleWriteChar(c); |
| 0 | 526 | | } |
| 0 | 527 | | } |
| | 528 | | } |