-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlink_handler.hpp
More file actions
275 lines (218 loc) · 8.31 KB
/
link_handler.hpp
File metadata and controls
275 lines (218 loc) · 8.31 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Compile: g++ -std=c++17 -static -o linuxify.exe main.cpp registry.cpp -lpsapi -lws2_32 -liphlpapi -lwininet -lwlanapi
#ifndef LINUXIFY_LINK_HANDLER_HPP
#define LINUXIFY_LINK_HANDLER_HPP
#include <windows.h>
#include <shellapi.h>
#include <string>
#include <vector>
#include <mutex>
#include <algorithm>
namespace LinkHandler {
struct ScreenLink {
std::string url;
int row;
int startCol;
int endCol;
};
class LinkManager {
private:
std::vector<ScreenLink> activeLinks;
int hoveredLinkIndex = -1;
std::mutex linkMutex;
HANDLE hOut;
bool enabled = true;
int findCharType(char c) {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) return 1;
if (c >= '0' && c <= '9') return 1;
if (c == '-' || c == '_' || c == '.' || c == '~') return 1;
if (c == ':' || c == '/' || c == '?' || c == '#' || c == '[' || c == ']' || c == '@') return 1;
if (c == '!' || c == '$' || c == '&' || c == '\'' || c == '(' || c == ')') return 1;
if (c == '*' || c == '+' || c == ',' || c == ';' || c == '=' || c == '%') return 1;
return 0;
}
bool isUrlChar(char c) {
return findCharType(c) == 1;
}
std::vector<std::pair<size_t, size_t>> findUrls(const std::string& text) {
std::vector<std::pair<size_t, size_t>> results;
const char* prefixes[] = {"https://", "http://", "ftp://", "file:///"};
const size_t prefixLens[] = {8, 7, 6, 8};
for (int p = 0; p < 4; p++) {
size_t pos = 0;
while ((pos = text.find(prefixes[p], pos)) != std::string::npos) {
size_t start = pos;
size_t end = pos + prefixLens[p];
while (end < text.length() && isUrlChar(text[end])) {
end++;
}
while (end > start + prefixLens[p] && (text[end-1] == '.' || text[end-1] == ',' ||
text[end-1] == ')' || text[end-1] == ']' || text[end-1] == '\'' || text[end-1] == '"')) {
end--;
}
if (end > start + prefixLens[p]) {
results.push_back({start, end});
}
pos = end;
}
}
std::sort(results.begin(), results.end());
return results;
}
void drawUnderline(int row, int startCol, int endCol, bool underline) {
if (!hOut || hOut == INVALID_HANDLE_VALUE) return;
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(hOut, &csbi)) return;
COORD savedPos = csbi.dwCursorPosition;
std::string escSeq;
if (underline) {
escSeq = "\033[4m";
} else {
escSeq = "\033[24m";
}
COORD pos = {(SHORT)startCol, (SHORT)row};
SetConsoleCursorPosition(hOut, pos);
std::vector<CHAR_INFO> lineBuffer(endCol - startCol);
COORD bufSize = {(SHORT)(endCol - startCol), 1};
COORD bufCoord = {0, 0};
SMALL_RECT readRect = {(SHORT)startCol, (SHORT)row, (SHORT)(endCol - 1), (SHORT)row};
if (ReadConsoleOutputA(hOut, lineBuffer.data(), bufSize, bufCoord, &readRect)) {
for (auto& ci : lineBuffer) {
if (underline) {
ci.Attributes |= COMMON_LVB_UNDERSCORE;
} else {
ci.Attributes &= ~COMMON_LVB_UNDERSCORE;
}
}
WriteConsoleOutputA(hOut, lineBuffer.data(), bufSize, bufCoord, &readRect);
}
SetConsoleCursorPosition(hOut, savedPos);
}
public:
static LinkManager& getInstance() {
static LinkManager instance;
return instance;
}
LinkManager() {
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
}
void setEnabled(bool e) { enabled = e; }
bool isEnabled() const { return enabled; }
void detectUrls(const std::string& text, int baseRow, int baseCol) {
if (!enabled) return;
std::lock_guard<std::mutex> lock(linkMutex);
auto urls = findUrls(text);
int currentRow = baseRow;
int currentCol = baseCol;
CONSOLE_SCREEN_BUFFER_INFO csbi;
int width = 120;
if (GetConsoleScreenBufferInfo(hOut, &csbi)) {
width = csbi.dwSize.X;
}
for (const auto& urlPos : urls) {
int urlColStart = baseCol;
int urlRow = baseRow;
for (size_t i = 0; i < urlPos.first; i++) {
if (text[i] == '\n') {
urlRow++;
urlColStart = 0;
} else if (text[i] == '\r') {
urlColStart = 0;
} else {
urlColStart++;
if (urlColStart >= width) {
urlColStart = 0;
urlRow++;
}
}
}
std::string url = text.substr(urlPos.first, urlPos.second - urlPos.first);
int urlLen = (int)url.length();
ScreenLink link;
link.url = url;
link.row = urlRow;
link.startCol = urlColStart;
link.endCol = urlColStart + urlLen;
if (link.endCol > width) {
link.endCol = width;
}
activeLinks.push_back(link);
}
}
int getLinkAt(int row, int col) {
std::lock_guard<std::mutex> lock(linkMutex);
for (int i = 0; i < (int)activeLinks.size(); i++) {
const auto& link = activeLinks[i];
if (link.row == row && col >= link.startCol && col < link.endCol) {
return i;
}
}
return -1;
}
void setHover(int linkIndex) {
if (!enabled) return;
std::lock_guard<std::mutex> lock(linkMutex);
if (hoveredLinkIndex == linkIndex) return;
if (hoveredLinkIndex >= 0 && hoveredLinkIndex < (int)activeLinks.size()) {
const auto& oldLink = activeLinks[hoveredLinkIndex];
drawUnderline(oldLink.row, oldLink.startCol, oldLink.endCol, false);
}
hoveredLinkIndex = linkIndex;
if (hoveredLinkIndex >= 0 && hoveredLinkIndex < (int)activeLinks.size()) {
const auto& newLink = activeLinks[hoveredLinkIndex];
drawUnderline(newLink.row, newLink.startCol, newLink.endCol, true);
}
}
void openLink(int linkIndex) {
std::string url;
{
std::lock_guard<std::mutex> lock(linkMutex);
if (linkIndex < 0 || linkIndex >= (int)activeLinks.size()) return;
url = activeLinks[linkIndex].url;
}
ShellExecuteA(NULL, "open", url.c_str(), NULL, NULL, SW_SHOWNORMAL);
}
void clearRow(int row) {
std::lock_guard<std::mutex> lock(linkMutex);
activeLinks.erase(
std::remove_if(activeLinks.begin(), activeLinks.end(),
[row](const ScreenLink& link) { return link.row == row; }),
activeLinks.end()
);
if (hoveredLinkIndex >= (int)activeLinks.size()) {
hoveredLinkIndex = -1;
}
}
void clearAllLinks() {
std::lock_guard<std::mutex> lock(linkMutex);
activeLinks.clear();
hoveredLinkIndex = -1;
}
void shiftLinksUp(int amount) {
std::lock_guard<std::mutex> lock(linkMutex);
for (auto& link : activeLinks) {
link.row -= amount;
}
activeLinks.erase(
std::remove_if(activeLinks.begin(), activeLinks.end(),
[](const ScreenLink& link) { return link.row < 0; }),
activeLinks.end()
);
if (hoveredLinkIndex >= (int)activeLinks.size()) {
hoveredLinkIndex = -1;
}
}
void onMouseEvent(int x, int y, int eventType) {
if (!enabled) return;
int linkIdx = getLinkAt(y, x);
if (eventType == 0) {
setHover(linkIdx);
} else if (eventType == 1 && linkIdx >= 0) {
openLink(linkIdx);
}
}
};
inline LinkManager& get() {
return LinkManager::getInstance();
}
}
#endif