/// <summary> /// Prompts the user to acknowledge a Prompt. /// </summary> /// <param name="message">Display message</param> /// <param name="clear">Option to clear the Console buffer. If true, can make the prompt more prominant.</param> /// <param name="allowEmpty">Specifies whether the user can provide an empty response. Default is typically the 'No', but can be overriden</param> /// <param name="defaultIsNo">Specifies whether the default entry should be 'No'. This only applies if 'allowEmpty' is true.</param> /// <returns>Boolean of users response relative to 'Yes' or 'No'</returns> public static bool Ask(string message, bool clear = false, bool allowEmpty = false, bool defaultIsNo = true) { string input = ""; string orEmpty = $" or Press Enter"; string[] options = new string[] { $"Y{(allowEmpty && !defaultIsNo ? orEmpty : string.Empty)}=Yes", $"N{(allowEmpty && defaultIsNo ? orEmpty : string.Empty)}=No" }; string optionMessage = $"({string.Join(", ", options)})"; do { if (clear) { Console.Clear(); } Consoul._write(message, RenderOptions.PromptColor); Consoul._write(optionMessage, RenderOptions.SubnoteColor); input = Read();// Console.ReadLine(); if (input.ToLower() != "y" && input.ToLower() != "n" && !string.IsNullOrEmpty(input)) { Consoul._write("Invalid input!", RenderOptions.InvalidColor); } } while ((allowEmpty ? false : string.IsNullOrEmpty(input)) && input.ToLower() != "y" && input.ToLower() != "n"); if (allowEmpty && string.IsNullOrEmpty(input)) { input = defaultIsNo ? "n" : "y"; } return(input.ToLower() == "y"); }
/// <summary> /// Waits for the user to press "Enter". Performs Console.ReadLine() /// <paramref name="silent">Flags whether or not to show continue message.</paramref> /// </summary> public static void Wait(bool silent = false) { if (!silent) { Consoul._write(RenderOptions.ContinueMessage, RenderOptions.SubnoteColor); } Read(); }
/// <summary> /// Prompts the user to provide a string input. /// </summary> /// <param name="message">Prompt Message</param> /// <param name="color">Override the Prompt Message color</param> /// <param name="allowEmpty">Specifies whether the user can provide an empty response. Can result in string.Empty</param> /// <returns>User response (string)</returns> public static string Input(string message, ConsoleColor?color = null, bool allowEmpty = false) { string output = string.Empty; bool valid = false; do { Consoul._write(message, RenderOptions.GetColor(color)); output = Read(); if (allowEmpty) { valid = true; } else if (!string.IsNullOrEmpty(output)) { valid = true; } } while (!valid); return(output); }
/// <summary> /// Displays the options for this prompt. Loops until the user "selects" the appropriate option. /// </summary> /// <returns>Zero-based index of the selected option.</returns> public int Run() { string[] escapePhrases = new string[] { "go back", "back", "exit", "goback" }; string input = ""; int selection = -1; PromptOption defaultOption = _options.FirstOrDefault(o => o.IsDefault); if (defaultOption != null) { _options.Where(o => o.Index != defaultOption.Index && o.IsDefault).ToList().ForEach(o => o.IsDefault = false); } do { if (ClearConsole) { Console.Clear(); } Consoul._write(Message, RenderOptions.PromptColor); Consoul._write("Choose the corresponding number from the options below:", RenderOptions.SubnoteColor); int i = 0; Routines.RegisterOptions(this); foreach (PromptOption option in Options) { Consoul._write(option.ToString(), option.Color); i++; } Console.ForegroundColor = RenderOptions.DefaultColor; input = Consoul.Read();// Console.ReadLine(); if (string.IsNullOrEmpty(input) && defaultOption != null) { selection = defaultOption.Index; } else if (escapePhrases.Any(o => input.Equals(o, StringComparison.OrdinalIgnoreCase))) { return(Consoul.EscapeIndex); } else { Int32.TryParse(input, out selection); if (selection <= 0 || selection > (_options.Count + 1)) { Consoul._write("Invalid selection!", RenderOptions.InvalidColor); selection = -1; } else { selection--; } } } while (selection < 0); _options[selection].Selected = true; return(selection); }