From 2ca5774c6b2f965553825ddb10088b206d2bf75a Mon Sep 17 00:00:00 2001 From: DemonKingSwarn Date: Sat, 25 Jan 2025 19:14:51 +0530 Subject: chore: minor bug fix --- src/WatchLog.cs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'src/WatchLog.cs') diff --git a/src/WatchLog.cs b/src/WatchLog.cs index f24e5a6..b153804 100644 --- a/src/WatchLog.cs +++ b/src/WatchLog.cs @@ -40,7 +40,7 @@ namespace hyprwatch.Logger Console.WriteLine(ex.Message); } - return t; + return t ?? string.Empty; } public static string GetDate() @@ -73,16 +73,24 @@ namespace hyprwatch.Logger Console.WriteLine(ex.Message); } - return d; + return d ?? string.Empty; } static void UpdateCSV(string date, Dictionary data) { - string homeDir = Environment.GetEnvironmentVariable("HOME"); - string dataDir = Path.Combine(homeDir, ".cache", "hyprwatch", "daily_data"); + string homeDir = Environment.GetEnvironmentVariable("HOME") ?? throw new InvalidOperationException("HOME environment variable is not set."); string filePath = Path.Combine(homeDir, ".cache", "hyprwatch", "daily_data", $"{date}.csv"); - Directory.CreateDirectory(Path.GetDirectoryName(dataDir)); + + string? dirPath = Path.GetDirectoryName(filePath); + if(dirPath is not null) + { + Directory.CreateDirectory(dirPath); + } + else + { + throw new InvalidOperationException("Invalid file path."); + } var overwriteData = new List(); foreach(var kvp in data) @@ -133,12 +141,14 @@ namespace hyprwatch.Logger { // The using block ensures the file is created and closed properly } + } bool isAfk = false; - int afkTimeout = 1; + //int afkTimeout = 1; var data = ImportData(filename); - + data ??= new Dictionary(); + while(true) { string date = GetDate(); @@ -148,13 +158,13 @@ namespace hyprwatch.Logger if(!isAfk) { string activeWindow = GetWindows.ActiveWindow(); - string usage = data.TryGetValue(activeWindow, out string value) ? value : null; + string usage = data.TryGetValue(activeWindow, out string? value) ? value : null; if(usage == null) { usage = "00:00:00"; } - Thread.Sleep(1); + Thread.Sleep(1000); usage = TimeOperations.TimeAddition("00:00:01", usage); data[$"{activeWindow}"] = usage; @@ -174,7 +184,7 @@ namespace hyprwatch.Logger data.Clear(); } } - } + } } } -- cgit v1.1