// I hereby waive this project under the public domain - see UNLICENSE for details. namespace StaggerPost; internal static class Interactive { /// /// Prompts the user with a yes/no question and returns their choice as a boolean value. /// /// The message to display to the user. /// True if the user selects 'Y' or presses Enter, otherwise false. public static bool UserChoice(string choice) { Console.WriteLine($"{Environment.NewLine}{choice} Y/N"); var input = Console.ReadKey().Key; if (input == ConsoleKey.Y || input == ConsoleKey.Enter) return true; return false; } /// /// Prompts the user to select a topic from a given list /// and returns the chosen topic. /// /// An array of available topics. /// The selected topic as a string. public static string SelectTopics(List communities) { var topicChoice = ""; var topicNum = 0; var userChoices = new List(); var numOfTopics = 0; var topicDict = new Dictionary(); foreach (var community in communities) { numOfTopics++; var title = community.Trim(); topicDict.Add(numOfTopics, title); userChoices.Add( $"{Environment.NewLine}{numOfTopics} {title.TrimEnd(new char[] { ',' })}" ); } var topicSelect = string.Join(", ", userChoices.ToArray()); Console.WriteLine($"{Environment.NewLine}Choose a Topic{Environment.NewLine}{topicSelect}"); var input = Console.ReadLine(); // Attempt to parse a number. if (int.TryParse(input, out topicNum) == true) topicChoice = topicDict[topicNum]; else SelectTopics(communities); return topicChoice; } /// /// Prompts the user to select a date (either today or tomorrow) and returns the selected date as a formatted string. /// /// A string representing the selected date in a short date format. public static string SelectDate() { var dtChoices = new[] { "Today", "Tomorrow" }; var dtDict = new Dictionary(); var dtSelection = new List(); var dtChoice = 0; var dtNum = 0; foreach (var days in dtChoices) { dtNum++; var day = days.Trim(); dtDict.Add(dtNum, day); dtSelection.Add($"{dtNum} {day}"); } var topicSelect = string.Join(", ", dtSelection.ToArray()); Console.WriteLine($"{Environment.NewLine}Choose a Date{Environment.NewLine}{topicSelect}"); var input = Console.ReadLine(); // Attempt to parse a number. if (int.TryParse(input, out dtNum) == true) dtChoice = dtNum; // Any choice above 2 tomorrow if (dtChoice >= 2) { var dt = DateTime.Now.AddDays(1); return dt.ToString("d"); } return DateTime.Today.ToString("d"); } }