/// <summary> /// Draw a progress bar /// </summary> /// <param name="complete"></param> /// <param name="maxVal"></param> /// <param name="barSize"></param> /// <param name="progressCharacter"></param> public static void DrawProgressBar(double complete, double maxVal, int barSize, char progressCharacter, ConsoleColor primaryColor = ConsoleColor.Green, ConsoleColor secondaryColor = ConsoleColor.DarkGreen) { Console.CursorVisible = false; int left = Console.CursorLeft; double perc = complete / maxVal; int chars = (int)Math.Floor(perc / (1d / barSize)); string p1 = String.Empty, p2 = String.Empty; for (int i = 0; i < chars; i++) { p1 += progressCharacter; } for (int i = 0; i < barSize - chars; i++) { p2 += progressCharacter; } Console.ForegroundColor = primaryColor; CFormat.Write(p1); Console.ForegroundColor = secondaryColor; CFormat.Write(p2); Console.ForegroundColor = primaryColor; CFormat.Write(String.Format(" {0}%", (perc * 100).ToString("N2"))); Console.ResetColor(); Console.CursorVisible = true; Console.CursorLeft = left; }
public static ConsoleAnswer UserChoice(ConsoleAnswerType type) { switch (type) { case ConsoleAnswerType.YesNo: string result = ReadFromConsole("(YES / NO): ").ToString().ToLower(); if (result.ToLower() == "y" || result.ToLower() == "yes") { return(ConsoleAnswer.Yes); } else if (result.ToLower() == "n" || result.ToLower() == "no") { return(ConsoleAnswer.No); } else { return(UserChoice(type)); // Re ask } case ConsoleAnswerType.YesNoCancel: string result1 = ReadFromConsole("(YES / NO / CANCEL): ").ToString().ToLower(); if (result1.ToLower() == "y" || result1.ToLower() == "yes") { return(ConsoleAnswer.Yes); } else if (result1.ToLower() == "n" || result1.ToLower() == "no") { return(ConsoleAnswer.No); } else if (result1.ToLower() == "c" || result1.ToLower() == "cancel") { return(ConsoleAnswer.Cancel); } else { return(UserChoice(type)); // Re ask } case ConsoleAnswerType.TrueFalse: string result2 = ReadFromConsole("(TRUE / FALSE): ").ToString().ToLower(); if (result2.ToLower() == "t" || result2.ToLower() == "true") { return(ConsoleAnswer.Yes); } else if (result2.ToLower() == "f" || result2.ToLower() == "false") { return(ConsoleAnswer.No); } else { return(UserChoice(type)); // Re ask } default: CFormat.WriteLine("Could not get user choice, specifed answer type does not exist.", ConsoleColor.Red); return(ConsoleAnswer.Undefined); } }
private Task _client_MessageReceived(SocketMessage arg) { if (arg.Author.Id == Config._INSTANCE.XanaId) { CFormat.Print(arg.Content, "XANA", DateTime.Now, ConsoleColor.Red); } else if (arg.Content.Substring(0, 2) == "x!") { CFormat.Print(arg.Content, arg.Author.ToString(), DateTime.Now); } return(Task.CompletedTask); }
/// <summary> /// User must make a pick a number between 0 and maxNumber. /// </summary> /// <param name="maxNumber"></param> /// <returns></returns> public static int UserPickInt(int maxNumber) { var pickNumber = ReadFromConsole("Enter a number between 0 and " + maxNumber + ": ", ConsoleInputType.Int, true, ConsoleColor.White, maxNumber.ToString().Length); if (pickNumber == null) { CFormat.WriteLine("Canceled."); return(-1); } if ((int)pickNumber >= 0 && (int)pickNumber <= maxNumber) { return((int)pickNumber); } else { UserPickInt(maxNumber); // re ask return(-1); // in case of error. Should never reach this point. } }
public Task Log(LogMessage arg) { CFormat.WriteLine(arg.ToString()); return(Task.CompletedTask); }
public static object ReadFromConsole(string promptMessage = "", ConsoleInputType inputType = ConsoleInputType.String, bool canEscape = false, ConsoleColor color = ConsoleColor.White, int maxChars = -1, char charMask = Char.MinValue) { // set vars ConsoleColor initialColor = Console.ForegroundColor; bool maskChars = (charMask != Char.MinValue); bool pressedEnter = false; string prompt = (promptMessage == "" ? _readPrompt : promptMessage); cursorPos = 0; historyIndex = 0; _readBuffer = ""; // Show a prompt Console.ForegroundColor = color; Console.Write(prompt); // Get input while (!pressedEnter) { ConsoleKeyInfo key = Console.ReadKey(true); // ENTER KEY if (key.Key == ConsoleKey.Enter) { ValidateHistory(); AddToHistory(_readBuffer); Console.ForegroundColor = initialColor; CFormat.WriteLine(""); if (inputType == ConsoleInputType.String) { return(_readBuffer); } else if (inputType == ConsoleInputType.Int) { return(int.Parse(_readBuffer)); // we can parse safely because input keys were filtered } else if (inputType == ConsoleInputType.Double) { return(Double.Parse(_readBuffer.Replace(".", ","))); // we can parse safely because input keys were filtered } else if (inputType == ConsoleInputType.Ulong) { return(ulong.Parse(_readBuffer)); // we can parse safely because input keys were filtered } } // BACKSPACE KEY else if (key.Key == ConsoleKey.Backspace) { ValidateHistory(); if (cursorPos <= 0) { continue; } if (cursorPos != _readBuffer.Length) { string beforeChar = _readBuffer.Substring(0, cursorPos - 1); string afterChar = _readBuffer.Substring(cursorPos, _readBuffer.Length - cursorPos); _readBuffer = beforeChar + afterChar; MoveCursorBack(); UserWrite(afterChar + " "); for (int i = 0; i < (afterChar.Length + 1); i++) { MoveCursorBack(); } } else { _readBuffer = _readBuffer.Substring(0, _readBuffer.Length - 1); MoveCursorBack(); UserWrite(" "); MoveCursorBack(); } } // DELETE KEY else if (key.Key == ConsoleKey.Delete) { ValidateHistory(); if (cursorPos >= _readBuffer.Length) { continue; } string beforeChar = _readBuffer.Substring(0, cursorPos); string afterChar = _readBuffer.Substring(cursorPos + 1, _readBuffer.Length - cursorPos - 1); _readBuffer = beforeChar + afterChar; UserWrite(afterChar + " "); for (int i = 0; i < (afterChar.Length + 1); i++) { MoveCursorBack(); } } // DEBUT KEY else if (key.Key == ConsoleKey.Home) { for (int i = cursorPos; i > 0; i--) { MoveCursorBack(); } } // END KEY else if (key.Key == ConsoleKey.End) { for (int i = cursorPos; i < _readBuffer.Length; i++) { MoveCursorAhead(); } } // ESCAPE KEY else if (key.Key == ConsoleKey.Escape) { if (canEscape) { return(null); } } // INSERT KEY else if (key.Key == ConsoleKey.Insert) { insertMode = !insertMode; if (insertMode) { Console.CursorSize = 100; } else { Console.CursorSize = 1; } } // Do not seek for specific kees after this line: --- // ARROW KEYS and WHITE SPACE KEYS else if (key.Key != ConsoleKey.Spacebar && key.KeyChar == Char.MinValue) { if (key.Key == ConsoleKey.RightArrow && cursorPos < _readBuffer.Length) { MoveCursorAhead(); } else if (key.Key == ConsoleKey.LeftArrow && cursorPos > 0) { MoveCursorBack(); } else if (key.Key == ConsoleKey.UpArrow) { OlderHistory(); } else if (key.Key == ConsoleKey.DownArrow) { NewerHistory(); } } // ANY OTHER KEY else { ValidateHistory(); // MAX CHARS CHECK if (maxChars > 0 && _readBuffer.Length >= maxChars) { continue; } // INPUT TYPE CHECK if (inputType == ConsoleInputType.Int) { if (!int.TryParse(key.KeyChar.ToString(), out int temp) && (key.KeyChar.ToString() != "-" || (key.KeyChar.ToString() == "-" && _readBuffer.Length != 0))) // If input is not a number in int mode, continue { continue; } } else if (inputType == ConsoleInputType.Double) { if (!int.TryParse(key.KeyChar.ToString(), out int temp) && (key.KeyChar.ToString() != "." || (key.KeyChar.ToString() == "." && _readBuffer.Contains("."))) && (key.KeyChar.ToString() != "-" || (key.KeyChar.ToString() == "-" && (_readBuffer.Length != 0 || _readBuffer.Contains("."))))) // good luck { continue; } } else if (inputType == ConsoleInputType.Ulong) { if (!int.TryParse(key.KeyChar.ToString(), out int temp) && (key.KeyChar.ToString() != "." || (key.KeyChar.ToString() == "." && _readBuffer.Contains(".")))) // good luck { continue; } } if (cursorPos != _readBuffer.Length && !insertMode) { _readBuffer = _readBuffer.Substring(0, cursorPos) + key.KeyChar + _readBuffer.Substring(cursorPos, _readBuffer.Length - cursorPos); } else if (cursorPos != _readBuffer.Length && insertMode) { _readBuffer = _readBuffer.Substring(0, cursorPos) + key.KeyChar + _readBuffer.Substring(cursorPos + 1, _readBuffer.Length - cursorPos - 1); } else { _readBuffer += key.KeyChar; } if (maskChars) { UserWrite(charMask.ToString()); } else { UserWrite(key.KeyChar.ToString()); if (cursorPos != _readBuffer.Length && !insertMode) { string aheadCursor = _readBuffer.Substring(cursorPos, _readBuffer.Length - cursorPos); UserWrite(aheadCursor); // Re write text that was ahead cursor for (int i = 0; i < (aheadCursor.Length); i++) { MoveCursorBack(); } } } } //System.Diagnostics.Debug.WriteLine(_readBuffer); } return(null); }