Slight refactor

- Config model
- Wrapped the generation and printing code into functions
- Can now start over
- Starting over or exiting will clear screen
This commit is contained in:
Tony Bark 2025-03-12 14:28:36 -04:00
parent 8cf1e917f6
commit 85b2cda8e2
3 changed files with 58 additions and 26 deletions

8
Config.cs Normal file
View file

@ -0,0 +1,8 @@
namespace PublishTimes;
public class Config
{
public string? File { get; set; }
public string? Path { get; set; }
public TomlArray? Topics { get; set; }
}

View file

@ -1,3 +1,3 @@
global using Tomlyn; global using Tomlyn;
global using Tomlyn.Model; global using Tomlyn.Model;
global using System.Globalization; global using PublishTimes;

View file

@ -10,6 +10,7 @@ var storeSchedule = new List<String>();
var appDir = Directory.GetCurrentDirectory(); var appDir = Directory.GetCurrentDirectory();
// File directory is used for file location set in config // File directory is used for file location set in config
var fileDir = Directory.GetCurrentDirectory(); var fileDir = Directory.GetCurrentDirectory();
var isRestart = false;
var communities = new[] { "Games", "Politics", "Research", "Technology" }; var communities = new[] { "Games", "Politics", "Research", "Technology" };
var scheduleFile = "schedule.txt"; var scheduleFile = "schedule.txt";
var cfgFile = "config.toml"; var cfgFile = "config.toml";
@ -45,20 +46,8 @@ string TimeSpanToAMPM(TimeSpan time)
return $"{hours12}:{time.Minutes:D2} {period}"; return $"{hours12}:{time.Minutes:D2} {period}";
} }
Console.WriteLine(banner);
foreach (var time in scheduledTimes)
{
var articleTime = $"Article {scheduledTimes.IndexOf(time) + 1} Scheduled at: {TimeSpanToAMPM(time)}";
// Correct format string to display time in 12-hour format with AM/PM
Console.WriteLine(articleTime);
// Store the schedule to memory for option export
storeSchedule.Add(articleTime);
}
// Give the user an option to export the schedule void ExportSchedule()
Console.WriteLine("Export? Y/N");
if (Console.ReadKey().Key == ConsoleKey.Y)
{ {
var cfgPath = Path.Combine(appDir, cfgFile); var cfgPath = Path.Combine(appDir, cfgFile);
var filePath = Path.Combine(fileDir, scheduleFile); var filePath = Path.Combine(fileDir, scheduleFile);
@ -72,10 +61,10 @@ if (Console.ReadKey().Key == ConsoleKey.Y)
if (File.Exists(cfgPath)) if (File.Exists(cfgPath))
{ {
var toml = File.ReadAllText(cfgPath); var toml = File.ReadAllText(cfgPath);
var model = Toml.ToModel(toml); var model = Toml.ToModel<Config>(toml);
var usrDir = (string)model["path"]; var usrDir = model.Path;
var usrFileName = (string)model["file"]; var usrFileName = model.File;
var tomlList = string.Join(", ", (TomlArray)model["topics"]); var tomlList = string.Join(", ", model.Topics);
var usrList = tomlList.Split(','); var usrList = tomlList.Split(',');
if (!string.IsNullOrEmpty(usrDir)) if (!string.IsNullOrEmpty(usrDir))
@ -101,17 +90,52 @@ if (Console.ReadKey().Key == ConsoleKey.Y)
Console.WriteLine($"{Environment.NewLine}Add another schedule? Y/N"); Console.WriteLine($"{Environment.NewLine}Add another schedule? Y/N");
if (Console.ReadKey().Key == ConsoleKey.Y) if (Console.ReadKey().Key == ConsoleKey.Y)
appendSchedule = true; appendSchedule = true;
}
// Write to file. // Write to file.
using (var outputFile = new StreamWriter(filePath, appendSchedule)) using (var outputFile = new StreamWriter(filePath, appendSchedule))
{ {
outputFile.WriteLine($" === {topic} ==="); outputFile.WriteLine($" === {topic} ===");
foreach (var line in storeSchedule) foreach (var line in storeSchedule)
outputFile.WriteLine(line); outputFile.WriteLine(line);
} }
}
// Clear list from memory before exit
storeSchedule.Clear();
} }
// Clear list from memory before exit void PrintSchedule()
storeSchedule.Clear(); {
if (isRestart)
Console.Clear();
Console.WriteLine(banner);
foreach (var time in scheduledTimes)
{
var articleTime = $"Article {scheduledTimes.IndexOf(time) + 1} Scheduled at: {TimeSpanToAMPM(time)}";
// Correct format string to display time in 12-hour format with AM/PM
Console.WriteLine(articleTime);
// Store the schedule to memory for option export
storeSchedule.Add(articleTime);
}
// Give the user an option to export the schedule
Console.WriteLine($"{Environment.NewLine}Export? Y/N");
if (Console.ReadKey().Key == ConsoleKey.Y)
ExportSchedule();
Console.WriteLine($"{Environment.NewLine}Start Over? Y/N");
if (Console.ReadKey().Key == ConsoleKey.Y)
{
isRestart = true;
PrintSchedule();
}
else
{
Console.Clear();
Environment.Exit(Environment.ExitCode);
}
}
PrintSchedule();