/// <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 input a file path with a suggested default path. /// </summary> /// <param name="defaultPath">The default file path the user must accept.</param> /// <param name="message"><inheritdoc cref="Write" path="/param[@name='message']"/></param> /// <param name="checkExists"><inheritdoc cref="PromptForFilepath(string, bool, ConsoleColor?)" path="/param[@name='checkExists']"/></param> /// <param name="color"><inheritdoc cref="Write" path="/param[@name='color']"/></param> /// <returns></returns> public static string PromptForFilepath(string defaultPath, string message, bool checkExists, ConsoleColor?color = null) { string path = defaultPath; if (!File.Exists(path) || !Consoul.Ask($"Would you like to use the default path:\r\n{path}", defaultIsNo: false)) { path = PromptForFilepath(message, checkExists, color); } return(path); }
public void Update(string message, ConsoleColor? color = null) { int prevX, prevY; prevX = Console.CursorLeft; prevY = Console.CursorTop; if (message?.Length > (MaxWidth ?? _fw)) message = message.Substring(0, (MaxWidth ?? _fw) - 3) + "..."; // Clear Message Space Console.SetCursorPosition(_x, _y); Consoul.Write(new string(' ', MaxWidth ?? _fw), writeLine: false); // Write Message Console.SetCursorPosition(_x, _y); Consoul.Write(message, color, false); Console.SetCursorPosition(prevX, prevY); }
/// <summary> /// Prompts the user to input a file path. /// </summary> /// <param name="message"><inheritdoc cref="Write" path="/param[@name='message']"/></param> /// <param name="checkExists">Indicates whether to check the file exists before allowing the user exit the loop.</param> /// <param name="color"><inheritdoc cref="Write" path="/param[@name='color']"/></param> /// <returns></returns> public static string PromptForFilepath(string message, bool checkExists, ConsoleColor?color = null) { string path; do { Consoul.Write(message, color); path = Consoul.Read(); } while (string.IsNullOrEmpty(path) && (checkExists ? !File.Exists(path) : true)); if (path.StartsWith("\"") && path.EndsWith("\"")) { path = path.Substring(1, path.Length - 2); } return(path); }
public bool Test(T source) { bool response = _rememberChoice; if (_ask && Consoul.Ask(PromptMessage.Compile()(source))) { response = true; } if (!_asked) { if (Consoul.Ask(RememberMessage.Compile()(response))) { _rememberChoice = response; _ask = false; } _asked = true; } return(response); }
/// <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); }