-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
139 lines (92 loc) · 3.79 KB
/
main.cpp
File metadata and controls
139 lines (92 loc) · 3.79 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <string>
#include <regex>
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <locale>
#include "settings.hpp"
#include "processInfo.cpp"
using namespace std;
activeWindowData previousData;
void formatOutput(wstring& text, activeWindowData data) {
int idx = 0;
wstring time_w(data.time.begin(), data.time.end());
wstring name_w(data.name.begin(), data.name.end());
wstring pname_w(data.pname.begin(), data.pname.end());
string pidString = to_string(data.pid);
wstring pid_w(pidString.begin(), pidString.end());
while (text.find('%') != string::npos) {
idx = text.find('%', idx);
wstring temp = text.substr(idx, 4);
if (temp == L"%pid") text.replace(idx, 4, pid_w);
else if (temp == L"%nam") text.replace(idx, 5, name_w);
else if (temp == L"%tim") text.replace(idx, 5, time_w);
else if (temp == L"%pna") text.replace(idx, 6, pname_w);
else if (temp == L"%fil") text.replace(idx, 5, data.file);
else if (temp == L"%pro") text.replace(idx, 8, data.project);
else break;
}
}
bool getWindowTitleData(wstring& title, wstring format, wstring& file, wstring& project) {
wstring regexString = format;
regexString = regex_replace(regexString, wregex(L"%file"), L"(.*?)");
regexString = L"^" + regex_replace(regexString, wregex(L"%project"), L"(.*?)") + L"$";
//swcout << regexString << L" ";
wsmatch matches;
if (regex_search(title, matches, wregex(regexString))) {
if (matches.size() == 3) {
if (format.find(L"%file") < format.find(L"%project")) {
file = matches[1].str();
project = matches[2].str();
} else {
file = matches[1].str();
project = matches[2].str();
}
return true;
} else if (matches.size() == 2) {
if (format.find(L"%file") != string::npos) {
file = matches[1].str();
} else if (format.find(L"%project") != string::npos) {
project = matches[1].str();
}
return true;
}
}
return false;
}
int main() {
_setmode(_fileno(stdout), 0x40000);
HWND hwnd = GetConsoleWindow();
ShowWindow(hwnd, SW_HIDE);
cout << windowStartup << endl;
while (true) {
// Open the active window
HWND target = GetForegroundWindow();
// Acquire the application name, time and PID
activeWindowData data = fetchWindowInfo(target);
// Get the window title
wchar_t titleBuffer[256]{};
int y = GetWindowTextW(target, titleBuffer, 256);
wstring title(titleBuffer);
/* Debug function to print unicode title letter by letter.
Useful for finding title formats */
// for (auto el : title) {
// cout << (int)el << ' ';
// }
wstring file, project;
// Read file and project information from the title using the corresponding format
getWindowTitleData(title, titleFormats[data.pname], file, project);
data.file = file;
data.project = project;
// Print the found values to the template from settings.hpp
wstring a(format.begin(), format.end());
formatOutput(a, data);
/* Check if the PID is not equal to the previously acquired PID
so the output is only printed when the window changes */
if (data.name != "") wcout << a << endl;// << file << " " << project << endl;
previousData = data;
Sleep(interval);
}
return 0;
}