From 696750646f676a38aa103f70a3ad604565308851 Mon Sep 17 00:00:00 2001 From: jame25 Date: Sun, 14 Apr 2024 03:02:24 +0100 Subject: [PATCH] added support for replace.dict --- TrayApplicationContext.cs | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/TrayApplicationContext.cs b/TrayApplicationContext.cs index 4b604ed..2570763 100644 --- a/TrayApplicationContext.cs +++ b/TrayApplicationContext.cs @@ -163,7 +163,7 @@ private void UpdatePiperArgs() private void ShowAboutWindow() { - string version = "1.1.8"; + string version = "1.1.9"; string message = $"Piper Tray\n\nVersion: {version}\n\nDeveloped by jame25"; string url = "https://github.com/jame25/Piper-Tray"; @@ -642,6 +642,21 @@ private async void StartMonitoring() // Read the ignore dictionary file string[] ignoreWords = File.Exists("ignore.dict") ? File.ReadAllLines("ignore.dict") : new string[0]; + // Read the replace dictionary file + Dictionary replaceWords = new Dictionary(); + if (File.Exists("replace.dict")) + { + string[] lines = File.ReadAllLines("replace.dict"); + foreach (string line in lines) + { + string[] parts = line.Split('='); + if (parts.Length == 2) + { + replaceWords[parts[0].Trim()] = parts[1].Trim(); + } + } + } + while (isRunning) { if (isMonitoringEnabled) @@ -678,10 +693,21 @@ private async void StartMonitoring() // Filter out the ignored words string filteredText = string.Join(" ", words.Where(word => !ignoreWords.Contains(word, StringComparer.OrdinalIgnoreCase))); - // Write the filtered text to the temporary file + // Replace words based on the replace dictionary + string[] modifiedWords = filteredText.Split(new[] { ' ', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); + for (int i = 0; i < modifiedWords.Length; i++) + { + if (replaceWords.ContainsKey(modifiedWords[i])) + { + modifiedWords[i] = replaceWords[modifiedWords[i]]; + } + } + string modifiedText = string.Join(" ", modifiedWords); + + // Write the modified text to the temporary file try { - File.WriteAllText(TempFile, filteredText); + File.WriteAllText(TempFile, modifiedText); } catch (IOException ex) { @@ -747,6 +773,7 @@ await Task.Run(() => } } + private void UpdateTrayIcon(ActivityState state) { if (trayIcon == null)