using System.Text.Json; using System.Text.RegularExpressions; using Microsoft.Extensions.Configuration; namespace RandomWallpaper { internal class Program { private static Queue previouslyDisplayed; private static int maxDisplayQueueLength = 1000; const String queueFileName = ".queue"; private static String[] targetDisplays = ["2"]; static void Main(string[] args) { var configurationBuilder = new ConfigurationBuilder(); configurationBuilder.SetBasePath(System.IO.Directory.GetCurrentDirectory()); configurationBuilder.AddJsonFile(path: "appSettings.json", optional: false, reloadOnChange: true); var config = configurationBuilder.Build(); if(File.Exists(queueFileName)) previouslyDisplayed = JsonSerializer.Deserialize>(File.ReadAllText(queueFileName)); else previouslyDisplayed = new Queue(); String wallpaperFolder = config["WallpaperFolder"]; foreach(String display in targetDisplays) { String id = ""; String randomPaper; do { randomPaper = GetRandomWallpaper(wallpaperFolder); id = GetWallhavenID(randomPaper); } while(previouslyDisplayed.Contains(id)); //set wallpaper String setWallpaperScript = KDEScripts.GetChangeWallpaperScript(display, randomPaper); KDEScripts.RunPlasmaScript(setWallpaperScript); AddToDisplayed(randomPaper); } File.WriteAllText(queueFileName, JsonSerializer.Serialize(previouslyDisplayed)); Console.WriteLine("Changed"); } static void AddToDisplayed(String fileName) { String pop; String wallhavenID = GetWallhavenID(fileName); if(previouslyDisplayed.Count >= maxDisplayQueueLength) previouslyDisplayed.TryDequeue(out pop); previouslyDisplayed.Enqueue(wallhavenID); } static String GetRandomWallpaper(String path) { String[] wallpaperFiles = Directory.GetFiles(path); int fileCount = wallpaperFiles.Length; Random rand = new (); int randomIndex = rand.Next(fileCount - 1); return wallpaperFiles[randomIndex]; } static String GetWallhavenID(String fileName) { String id = Regex.Match(fileName, @"wallhaven-(?[^\.]+)").Groups["id"].Value; return id; } } }