diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/GetWindows.cs | 47 | ||||
| -rw-r--r-- | src/TimeOperations.cs | 93 | ||||
| -rw-r--r-- | src/WatchLog.cs | 181 |
3 files changed, 321 insertions, 0 deletions
diff --git a/src/GetWindows.cs b/src/GetWindows.cs new file mode 100644 index 0000000..d2bba9f --- /dev/null +++ b/src/GetWindows.cs @@ -0,0 +1,47 @@ +namespace hyprwatch.Window +{ + using System; + using System.Diagnostics; + using System.Text.RegularExpressions; + + public class GetWindows + { + public static string ActiveWindow() + { + string? activeWindow = null; + + try { + Process process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = "hyprctl", + Arguments = "activewindow", + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true, + } + }; + + process.Start(); + + string output = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + + var classMatch = Regex.Match(output, @"class:(.+)"); + + if(classMatch.Success) + { + activeWindow = classMatch.Groups[1].Value.Trim(); + } + } + catch(Exception ex) + { + Console.WriteLine(ex.Message); + } + + return activeWindow; + } + } + +} diff --git a/src/TimeOperations.cs b/src/TimeOperations.cs new file mode 100644 index 0000000..2bd3fc9 --- /dev/null +++ b/src/TimeOperations.cs @@ -0,0 +1,93 @@ +namespace hyprwatch.Time +{ + using System; + + public class TimeOperations + { + public static string TimeDifference(string a, string b) + { + int hr = int.Parse(b.Substring(0, 2)) - int.Parse(a.Substring(0, 2)); + int mn = int.Parse(b.Substring(3, 2)) - int.Parse(a.Substring(3, 2)); + int sec = int.Parse(b.Substring(6, 2)) - int.Parse(a.Substring(6, 2)); + + if (mn < 0 && sec < 0) + { + hr -= 1; + mn += 60 - 1; + sec += 60; + if (hr < 0) hr += 24; + } + else if (mn < 0 && sec >= 0) + { + hr -= 1; + mn += 60; + if (hr < 0) hr += 24; + } + else if (sec < 0 && mn > 0) + { + sec += 60; + mn -= 1; + if (hr < 0) hr += 24; + } + else if (sec < 0 && mn == 0) + { + hr -= 1; + mn = 59; + sec += 60; + } + + return $"{hr:D2}:{mn:D2}:{sec:D2}"; + } + + public static string TimeAddition(string a, string b) + { + int hr = int.Parse(b.Substring(0, 2)) + int.Parse(a.Substring(0, 2)); + int mn = int.Parse(b.Substring(3, 2)) + int.Parse(a.Substring(3, 2)); + int sec = int.Parse(b.Substring(6, 2)) + int.Parse(a.Substring(6, 2)); + + if (mn >= 60 && sec >= 60) + { + hr += 1; + mn = mn - 60 + 1; + sec -= 60; + } + else if (mn >= 60) + { + hr += 1; + mn -= 60; + } + else if (sec >= 60) + { + mn += 1; + sec -= 60; + } + + return $"{hr:D2}:{mn:D2}:{sec:D2}"; + } + + public static string FormatTime(string t) + { + return $"{t.Substring(0, 2)}h {t.Substring(3, 2)}m {t.Substring(6)}s"; + } + + public static int ConvertIntoSec(string t) + { + int hr = int.Parse(t.Substring(0, 2)); + int mn = int.Parse(t.Substring(3, 2)); + int sec = int.Parse(t.Substring(6)); + return hr * 3600 + mn * 60 + sec; + } + + public static string Convert(int sec) + { + sec %= 24 * 3600; + int hr = sec / 3600; + sec %= 3600; + int mn = sec / 60; + sec %= 60; + + return $"{hr:D2}:{mn:D2}:{sec:D2}"; + } + } +} + diff --git a/src/WatchLog.cs b/src/WatchLog.cs new file mode 100644 index 0000000..f24e5a6 --- /dev/null +++ b/src/WatchLog.cs @@ -0,0 +1,181 @@ +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<string, string> 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<string[]>(); + 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<string, string> ImportData(string file) + { + var data = new Dictionary<string, string>(); + + 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(); + } + } + } + } + } + } +} |
