mirror of
https://github.com/tonytins/PublishTimes.git
synced 2025-03-14 20:01:20 +00:00
Streamline user selection
- New topic selection - Removed white space from topics
This commit is contained in:
parent
2f41109477
commit
df33078d1a
1 changed files with 80 additions and 15 deletions
95
Program.cs
95
Program.cs
|
@ -1,5 +1,20 @@
|
||||||
// I hereby waive this project under the public domain - see UNLICENSE for details.
|
// I hereby waive this project under the public domain - see UNLICENSE for details.
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Prompts the user with a yes/no question and returns their choice as a boolean value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="choice">The message to display to the user.</param>
|
||||||
|
/// <returns>True if the user selects 'Y' or presses Enter, otherwise false.</returns>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generates a schedule of article publishing times, ensuring a randomized
|
/// Generates a schedule of article publishing times, ensuring a randomized
|
||||||
/// delay between each while avoiding time conflicts within a 30-minute window.
|
/// 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}";
|
return $"{hours12}:{time.Minutes:D2} {period}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Prompts the user to select a topic from a given list
|
||||||
|
/// and returns the chosen topic.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="topics">An array of available topics.</param>
|
||||||
|
/// <returns>The selected topic as a string.</returns>
|
||||||
|
string SelectTopics(List<string> topics)
|
||||||
|
{
|
||||||
|
var topicChoice = "";
|
||||||
|
var topicNum = 0;
|
||||||
|
var userChoices = new List<string>();
|
||||||
|
var numOfTopics = 0;
|
||||||
|
var topicDict = new Dictionary<int,
|
||||||
|
string>();
|
||||||
|
|
||||||
|
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<string> 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;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Exports the scheduled articles to a file, allowing the user to modify
|
/// Exports the scheduled articles to a file, allowing the user to modify
|
||||||
/// the directory, filename, and list of topics based on
|
/// the directory, filename, and list of topics based on
|
||||||
|
@ -71,10 +141,6 @@ void ExportSchedule(List<String> storeSchedule)
|
||||||
var appendSchedule = false;
|
var appendSchedule = false;
|
||||||
var topic = "";
|
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 the config file exists, read from that but don't assume anything is filled
|
||||||
if (File.Exists(cfgPath))
|
if (File.Exists(cfgPath))
|
||||||
{
|
{
|
||||||
|
@ -92,21 +158,18 @@ void ExportSchedule(List<String> storeSchedule)
|
||||||
scheduleFile = usrFileName;
|
scheduleFile = usrFileName;
|
||||||
|
|
||||||
if (usrList.Length > 0)
|
if (usrList.Length > 0)
|
||||||
{
|
communities = usrList;
|
||||||
var chooseUsrTopic = rng.Next(0, usrList.Length);
|
|
||||||
topic = usrList[chooseUsrTopic];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Set new file Path
|
// Set new file Path
|
||||||
filePath = Path.Combine(fileDir, scheduleFile);
|
filePath = Path.Combine(fileDir, scheduleFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
topic = NewTopic(communities.ToList());
|
||||||
|
|
||||||
// If the file already exists, assume a previous schedule was written
|
// If the file already exists, assume a previous schedule was written
|
||||||
if (File.Exists(filePath))
|
if (File.Exists(filePath))
|
||||||
{
|
{
|
||||||
Console.WriteLine($"{Environment.NewLine}Add another schedule? Y/N");
|
if (UserChoice("Add to existing file?"))
|
||||||
if (Console.ReadKey().Key == ConsoleKey.Y)
|
|
||||||
appendSchedule = true;
|
appendSchedule = true;
|
||||||
|
|
||||||
// Write to file.
|
// Write to file.
|
||||||
|
@ -146,12 +209,14 @@ void PrintSchedule(bool isRestart = false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Give the user an option to export the schedule
|
// Give the user an option to export the schedule
|
||||||
Console.WriteLine($"{Environment.NewLine}Export? Y/N");
|
if (UserChoice("Retry?"))
|
||||||
if (Console.ReadKey().Key == ConsoleKey.Y)
|
PrintSchedule(true);
|
||||||
|
|
||||||
|
// Give the user an option to export the schedule
|
||||||
|
if (UserChoice("Export?"))
|
||||||
ExportSchedule(storeSchedule);
|
ExportSchedule(storeSchedule);
|
||||||
|
|
||||||
Console.WriteLine($"{Environment.NewLine}Start Over? Y/N");
|
if (UserChoice("Generate A New Batch?"))
|
||||||
if (Console.ReadKey().Key == ConsoleKey.Y)
|
|
||||||
PrintSchedule(true);
|
PrintSchedule(true);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue