From 46a86f9ef8eab0412c73c77305dcadcbe27236bd Mon Sep 17 00:00:00 2001
From: Tony Bark <mnctnh817@mozmail.com>
Date: Tue, 11 Mar 2025 23:30:11 -0400
Subject: [PATCH] Optional config file

---
 .gitignore          |  1 +
 GlobalUsings.cs     |  2 ++
 Program.cs          | 41 ++++++++++++++++++++++++++++++-----------
 PublishTimes.csproj |  4 ++++
 README.md           |  2 +-
 config.toml.sample  |  2 ++
 6 files changed, 40 insertions(+), 12 deletions(-)
 create mode 100644 GlobalUsings.cs
 create mode 100644 config.toml.sample

diff --git a/.gitignore b/.gitignore
index 233762f..6136cef 100644
--- a/.gitignore
+++ b/.gitignore
@@ -545,3 +545,4 @@ FodyWeavers.xsd
 
 .idea/**
 *.txt
+*.toml
diff --git a/GlobalUsings.cs b/GlobalUsings.cs
new file mode 100644
index 0000000..8324182
--- /dev/null
+++ b/GlobalUsings.cs
@@ -0,0 +1,2 @@
+global using Tomlyn;
+global using System.Diagnostics;
diff --git a/Program.cs b/Program.cs
index de15230..b4b0f6c 100644
--- a/Program.cs
+++ b/Program.cs
@@ -6,6 +6,12 @@ var startTime = new TimeSpan(9, 0, 0); // Starting time at 9:00 AM
 var random = new Random();
 var scheduledTimes = new List<TimeSpan>();
 var storeSchedule = new List<String>();
+// App directory is used for config file
+var appDir = Directory.GetCurrentDirectory();
+// File directory is used for file location set in config
+var fileDir = Directory.GetCurrentDirectory();
+var scheduleFile = "schedule.txt";
+var cfgFile = "config.toml";
 
 for (int i = 0; i < numberOfArticles; i++)
 {
@@ -53,11 +59,28 @@ Console.WriteLine("Export? Y/N");
 
 if (Console.ReadKey().Key == ConsoleKey.Y)
 {
-    var appPath = Directory.GetCurrentDirectory();
-    var scheduleFile = "schedule.txt";
-    var filePath = Path.Combine(appPath, scheduleFile);
+    var cfgPath = Path.Combine(appDir, cfgFile);
+    var filePath = Path.Combine(fileDir, scheduleFile);
     var appendSchedule = false;
 
+    // If the config file exists, read from that but don't assume anything is filled
+    if (File.Exists(cfgPath))
+    {
+        var toml = File.ReadAllText(cfgPath);
+        var model = Toml.ToModel(toml);
+        var usrDir = (string)model["path"];
+        var usrFileName = (string)model["file"];
+
+        if (!string.IsNullOrEmpty(usrDir))
+            fileDir = usrDir;
+
+        if (!string.IsNullOrEmpty(usrFileName))
+            scheduleFile = usrFileName;
+
+        // Set new file Path
+        filePath = Path.Combine(fileDir, scheduleFile);
+    }
+
     // If the file already exists, assume a previous schedule was written
     if (File.Exists(filePath))
     {
@@ -67,7 +90,7 @@ if (Console.ReadKey().Key == ConsoleKey.Y)
     }
 
     // Write to file.
-    using (var outputFile = new StreamWriter(Path.Combine(appPath, scheduleFile), appendSchedule))
+    using (var outputFile = new StreamWriter(filePath, appendSchedule))
     {
         // Add separator between times
         if (appendSchedule)
@@ -76,11 +99,7 @@ if (Console.ReadKey().Key == ConsoleKey.Y)
         foreach (var line in storeSchedule)
             outputFile.WriteLine(line);
     }
+}
 
-    // Clear list from memory before exit
-    storeSchedule.Clear();
-}
-else
-{
-    Environment.Exit(Environment.ExitCode);
-}
+// Clear list from memory before exit
+storeSchedule.Clear();
diff --git a/PublishTimes.csproj b/PublishTimes.csproj
index fd4bd08..109581c 100644
--- a/PublishTimes.csproj
+++ b/PublishTimes.csproj
@@ -7,4 +7,8 @@
     <Nullable>enable</Nullable>
   </PropertyGroup>
 
+  <ItemGroup>
+    <PackageReference Include="Tomlyn" Version="0.19.0" />
+  </ItemGroup>
+
 </Project>
diff --git a/README.md b/README.md
index ed804da..98d5dd7 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@ This is a very simple console application that generates a list of times to sche
 
 ## Background
 
-A while back, I [found a tool](https://schedule.lemmings.world) to schedule articles on Lemmy. I've been posting within a few hour apart at random minutes and I wanted to something decide that for me. I had AI write the algorithm.
+A while back, I [found a tool](https://schedule.lemmings.world) to schedule articles on Lemmy. I've been posting within a few hour apart at random minutes and I wanted to something decide that for me. I had AI write the base algorithm everything else is my own touches.
 
 ## License
 
diff --git a/config.toml.sample b/config.toml.sample
new file mode 100644
index 0000000..c49564a
--- /dev/null
+++ b/config.toml.sample
@@ -0,0 +1,2 @@
+path = "/home/tonytins/Documents/"
+file = "newscycle.txt"