Optional config file

This commit is contained in:
Tony Bark 2025-03-11 23:30:11 -04:00
parent 9fcc37315e
commit 46a86f9ef8
6 changed files with 40 additions and 12 deletions

1
.gitignore vendored
View file

@ -545,3 +545,4 @@ FodyWeavers.xsd
.idea/** .idea/**
*.txt *.txt
*.toml

2
GlobalUsings.cs Normal file
View file

@ -0,0 +1,2 @@
global using Tomlyn;
global using System.Diagnostics;

View file

@ -6,6 +6,12 @@ var startTime = new TimeSpan(9, 0, 0); // Starting time at 9:00 AM
var random = new Random(); var random = new Random();
var scheduledTimes = new List<TimeSpan>(); var scheduledTimes = new List<TimeSpan>();
var storeSchedule = new List<String>(); 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++) for (int i = 0; i < numberOfArticles; i++)
{ {
@ -53,11 +59,28 @@ Console.WriteLine("Export? Y/N");
if (Console.ReadKey().Key == ConsoleKey.Y) if (Console.ReadKey().Key == ConsoleKey.Y)
{ {
var appPath = Directory.GetCurrentDirectory(); var cfgPath = Path.Combine(appDir, cfgFile);
var scheduleFile = "schedule.txt"; var filePath = Path.Combine(fileDir, scheduleFile);
var filePath = Path.Combine(appPath, scheduleFile);
var appendSchedule = false; 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 the file already exists, assume a previous schedule was written
if (File.Exists(filePath)) if (File.Exists(filePath))
{ {
@ -67,7 +90,7 @@ if (Console.ReadKey().Key == ConsoleKey.Y)
} }
// Write to file. // Write to file.
using (var outputFile = new StreamWriter(Path.Combine(appPath, scheduleFile), appendSchedule)) using (var outputFile = new StreamWriter(filePath, appendSchedule))
{ {
// Add separator between times // Add separator between times
if (appendSchedule) if (appendSchedule)
@ -76,11 +99,7 @@ if (Console.ReadKey().Key == ConsoleKey.Y)
foreach (var line in storeSchedule) foreach (var line in storeSchedule)
outputFile.WriteLine(line); outputFile.WriteLine(line);
} }
}
// Clear list from memory before exit // Clear list from memory before exit
storeSchedule.Clear(); storeSchedule.Clear();
}
else
{
Environment.Exit(Environment.ExitCode);
}

View file

@ -7,4 +7,8 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Tomlyn" Version="0.19.0" />
</ItemGroup>
</Project> </Project>

View file

@ -4,7 +4,7 @@ This is a very simple console application that generates a list of times to sche
## Background ## 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 ## License

2
config.toml.sample Normal file
View file

@ -0,0 +1,2 @@
path = "/home/tonytins/Documents/"
file = "newscycle.txt"