namespace hyprwatch.Logger { using System; using System.IO; using System.Threading; using System.Diagnostics; using System.Collections.Generic; using hyprwatch.Window; using hyprwatch.Time; public class WatchLog { public static string GetTime() { string? t = null; try { Process process = new Process { StartInfo = new ProcessStartInfo { FileName = "date", Arguments = "+%T", RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true, } }; process.Start(); string output = process.StandardOutput.ReadToEnd(); process.WaitForExit(); t = output.Substring(0, output.Length - 1); } catch(Exception ex) { Console.WriteLine(ex.Message); } return t; } public static string GetDate() { string? d = null; try { Process process = new Process { StartInfo = new ProcessStartInfo { FileName = "date", Arguments = "+%d-%m-%Y", RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true, } }; process.Start(); string output = process.StandardOutput.ReadToEnd(); process.WaitForExit(); d = output.Substring(0, output.Length - 1); } catch(Exception ex) { Console.WriteLine(ex.Message); } return d; } static void UpdateCSV(string date, Dictionary data) { string homeDir = Environment.GetEnvironmentVariable("HOME"); string dataDir = Path.Combine(homeDir, ".cache", "hyprwatch", "daily_data"); string filePath = Path.Combine(homeDir, ".cache", "hyprwatch", "daily_data", $"{date}.csv"); Directory.CreateDirectory(Path.GetDirectoryName(dataDir)); var overwriteData = new List(); foreach(var kvp in data) { overwriteData.Add(new[] { kvp.Value, kvp.Key }); } using (var writer = new StreamWriter(filePath)) { foreach (var row in overwriteData) { writer.WriteLine(string.Join("\t", row)); } } } static Dictionary ImportData(string file) { var data = new Dictionary(); var rawData = File.ReadAllLines(file); foreach(var line in rawData) { var parts = line.Split('\t'); if(parts.Length >= 2) { string key = parts[1].TrimEnd(); string value = parts[0]; data[key] = value; } } return data; } public static void LogCreation() { string homeDir = Environment.GetEnvironmentVariable("HOME"); string filename = Path.Combine($"{homeDir}", ".cache", "hyprwatch", "daily_data", $"{GetDate()}.csv"); if(!File.Exists(filename)) { string directoryPath = Path.Combine($"{homeDir}", ".cache", "Watcher", "daily_data"); Directory.CreateDirectory(directoryPath); using (var fp = File.Create(filename)) { // The using block ensures the file is created and closed properly } bool isAfk = false; int afkTimeout = 1; var data = ImportData(filename); while(true) { string date = GetDate(); filename = Path.Combine($"{homeDir}", ".cache", "hyprwatch", "daily_data", $"{date}.csv"); Console.WriteLine(data); if(!isAfk) { string activeWindow = GetWindows.ActiveWindow(); string usage = data.TryGetValue(activeWindow, out string value) ? value : null; if(usage == null) { usage = "00:00:00"; } Thread.Sleep(1); usage = TimeOperations.TimeAddition("00:00:01", usage); data[$"{activeWindow}"] = usage; if(File.Exists(filename)) { UpdateCSV(GetDate(), data); } else if(!File.Exists(filename)) { string newFilename = Path.Combine($"{homeDir}", ".cache", "hyprwatch", "daily_data", $"{GetDate()}.csv"); using (var fp = File.Create(newFilename)) { // The using block ensures the file is created and closed properly } data.Clear(); } } } } } } }