From df33078d1a4eb3c9f012968589ad49842ad5d8a6 Mon Sep 17 00:00:00 2001 From: Tony Bark Date: Thu, 13 Mar 2025 02:12:23 -0400 Subject: [PATCH] Streamline user selection - New topic selection - Removed white space from topics --- Program.cs | 95 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 80 insertions(+), 15 deletions(-) diff --git a/Program.cs b/Program.cs index 279ba50..1d87019 100644 --- a/Program.cs +++ b/Program.cs @@ -1,5 +1,20 @@ // I hereby waive this project under the public domain - see UNLICENSE for details. +/// +/// 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. +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; +} + /// /// Generates a schedule of article publishing times, ensuring a randomized /// delay between each while avoiding time conflicts within a 30-minute window. @@ -51,6 +66,61 @@ string ConvertTo12Hour(TimeSpan time) return $"{hours12}:{time.Minutes:D2} {period}"; } +/// +/// 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. +string SelectTopics(List topics) +{ + var topicChoice = ""; + var topicNum = 0; + var userChoices = new List(); + var numOfTopics = 0; + var topicDict = new Dictionary(); + + foreach (var topic in topics) + { + numOfTopics++; + topicDict.Add(numOfTopics, topic.Trim()); + userChoices.Add($"{numOfTopics} {topic.Trim()}"); + } + + var selection = string.Join(", ", userChoices.ToArray()); + Console.WriteLine($"Select a Topic (Choose a Number){Environment.NewLine}{selection}"); + var input = Console.ReadLine(); + + // + if (int.TryParse(input, out topicNum) == true) + topicChoice = topicDict[topicNum]; + else + { + Console.Clear(); + Console.Write($"{Environment.NewLine}Try again."); + NewTopic(topics); + } + + return topicChoice; +} + +string NewTopic(List topics, bool retry = false) +{ + var newTopic = ""; + + if (UserChoice("Choose a Topic?")) + newTopic = SelectTopics(topics); + else + { + var rng = new Random(); + var chooseTopic = rng.Next(0, topics.ToArray().Length); + newTopic = topics[chooseTopic].Trim(); + } + + return newTopic; +} + /// /// Exports the scheduled articles to a file, allowing the user to modify /// the directory, filename, and list of topics based on @@ -71,10 +141,6 @@ void ExportSchedule(List storeSchedule) var appendSchedule = false; var topic = ""; - var rng = new Random(); - var chooseTopic = rng.Next(0, communities.Length); - topic = communities[chooseTopic]; - // If the config file exists, read from that but don't assume anything is filled if (File.Exists(cfgPath)) { @@ -92,21 +158,18 @@ void ExportSchedule(List storeSchedule) scheduleFile = usrFileName; if (usrList.Length > 0) - { - var chooseUsrTopic = rng.Next(0, usrList.Length); - topic = usrList[chooseUsrTopic]; - } - + communities = usrList; // Set new file Path filePath = Path.Combine(fileDir, scheduleFile); } + topic = NewTopic(communities.ToList()); + // If the file already exists, assume a previous schedule was written if (File.Exists(filePath)) { - Console.WriteLine($"{Environment.NewLine}Add another schedule? Y/N"); - if (Console.ReadKey().Key == ConsoleKey.Y) + if (UserChoice("Add to existing file?")) appendSchedule = true; // Write to file. @@ -146,12 +209,14 @@ void PrintSchedule(bool isRestart = false) } // Give the user an option to export the schedule - Console.WriteLine($"{Environment.NewLine}Export? Y/N"); - if (Console.ReadKey().Key == ConsoleKey.Y) + if (UserChoice("Retry?")) + PrintSchedule(true); + + // Give the user an option to export the schedule + if (UserChoice("Export?")) ExportSchedule(storeSchedule); - Console.WriteLine($"{Environment.NewLine}Start Over? Y/N"); - if (Console.ReadKey().Key == ConsoleKey.Y) + if (UserChoice("Generate A New Batch?")) PrintSchedule(true); else {