-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshell_streams.hpp
More file actions
301 lines (254 loc) · 10.9 KB
/
shell_streams.hpp
File metadata and controls
301 lines (254 loc) · 10.9 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#ifndef LINUXIFY_SHELL_STREAMS_HPP
#define LINUXIFY_SHELL_STREAMS_HPP
#include <windows.h>
#include <mutex>
#include <atomic>
#include <functional>
#include <string>
#include <vector>
#include <algorithm>
#include "link_handler.hpp"
namespace ShellIO {
// Color Definitions
enum class Color {
Red, Green, Blue, Yellow, Cyan, Magenta, White, Gray,
LightRed, LightGreen, LightBlue, LightYellow, LightCyan, LightMagenta, LightWhite,
Reset, Bold, Faint
};
// Endl Token
struct Endl {};
inline Endl endl;
// --- Output Stream ---
class ShellOutStream {
private:
std::mutex outputMutex;
HANDLE hOut;
bool isErrorStream;
bool isConsole; // True if output is a console, False if pipe/file
std::atomic<bool> isPromptActive{false};
std::function<void()> redrawCallback = nullptr;
void writeRaw(const char* data, DWORD length) {
DWORD written;
if (isConsole) {
WriteConsoleA(hOut, data, length, &written, NULL);
} else {
WriteFile(hOut, data, length, &written, NULL);
}
}
void setAttributes(WORD attrs) {
if (isConsole) SetConsoleTextAttribute(hOut, attrs);
}
void resetAttributes() {
if (isConsole) SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
void clearLine() {
if (!isConsole) return;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hOut, &csbi);
COORD pos = {0, csbi.dwCursorPosition.Y};
DWORD written;
FillConsoleOutputCharacterA(hOut, ' ', csbi.dwSize.X, pos, &written);
SetConsoleCursorPosition(hOut, pos);
}
public:
ShellOutStream(bool error) : isErrorStream(error) {
hOut = GetStdHandle(error ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
DWORD mode;
isConsole = GetConsoleMode(hOut, &mode);
}
// Re-check handle type (useful if redirection changes)
void refreshHandle() {
hOut = GetStdHandle(isErrorStream ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
DWORD mode;
isConsole = GetConsoleMode(hOut, &mode);
}
void registerPromptCallback(std::function<void()> callback) {
std::lock_guard<std::mutex> lock(outputMutex);
redrawCallback = callback;
}
void setPromptActive(bool active) {
isPromptActive = active;
}
void print(const std::string& content) {
std::lock_guard<std::mutex> lock(outputMutex);
if (content.empty()) return;
if (isConsole && isPromptActive && redrawCallback) {
clearLine();
}
if (isErrorStream) {
setAttributes(FOREGROUND_RED | FOREGROUND_INTENSITY);
}
CONSOLE_SCREEN_BUFFER_INFO csbi;
int startRow = 0, startCol = 0;
if (isConsole && GetConsoleScreenBufferInfo(hOut, &csbi)) {
startRow = csbi.dwCursorPosition.Y;
startCol = csbi.dwCursorPosition.X;
}
writeRaw(content.c_str(), (DWORD)content.length());
if (isConsole && !isErrorStream) {
LinkHandler::get().detectUrls(content, startRow, startCol);
}
if (isErrorStream) {
resetAttributes();
}
if (isConsole && isPromptActive && redrawCallback) {
if (content.back() != '\n') {
writeRaw("\n", 1);
}
redrawCallback();
}
}
void printColor(Color c) {
if (!isConsole) return; // Colors ignored on pipes
std::lock_guard<std::mutex> lock(outputMutex);
WORD attrs = 0;
switch(c) {
case Color::Red: attrs = FOREGROUND_RED; break;
case Color::Green: attrs = FOREGROUND_GREEN; break;
case Color::Blue: attrs = FOREGROUND_BLUE; break;
case Color::Yellow: attrs = FOREGROUND_RED | FOREGROUND_GREEN; break;
case Color::Cyan: attrs = FOREGROUND_GREEN | FOREGROUND_BLUE; break;
case Color::Magenta: attrs = FOREGROUND_RED | FOREGROUND_BLUE; break;
case Color::White: attrs = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; break;
case Color::Gray: attrs = FOREGROUND_INTENSITY; break;
case Color::LightRed: attrs = FOREGROUND_RED | FOREGROUND_INTENSITY; break;
case Color::LightGreen: attrs = FOREGROUND_GREEN | FOREGROUND_INTENSITY; break;
case Color::LightBlue: attrs = FOREGROUND_BLUE | FOREGROUND_INTENSITY; break;
case Color::LightYellow: attrs = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; break;
case Color::LightCyan: attrs = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY; break;
case Color::LightMagenta: attrs = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY; break;
case Color::LightWhite: attrs = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY; break;
case Color::Reset: attrs = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; break;
default: attrs = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; break;
}
setAttributes(attrs);
}
// --- Operators ---
ShellOutStream& operator<<(const std::string& val) { print(val); return *this; }
ShellOutStream& operator<<(const char* val) { print(std::string(val)); return *this; }
ShellOutStream& operator<<(char val) { print(std::string(1, val)); return *this; }
ShellOutStream& operator<<(int val) { print(std::to_string(val)); return *this; }
ShellOutStream& operator<<(long val) { print(std::to_string(val)); return *this; }
ShellOutStream& operator<<(long long val) { print(std::to_string(val)); return *this; }
ShellOutStream& operator<<(unsigned int val) { print(std::to_string(val)); return *this; }
ShellOutStream& operator<<(unsigned long val) { print(std::to_string(val)); return *this; }
ShellOutStream& operator<<(unsigned long long val) { print(std::to_string(val)); return *this; }
ShellOutStream& operator<<(float val) { print(std::to_string(val)); return *this; }
ShellOutStream& operator<<(double val) { print(std::to_string(val)); return *this; }
ShellOutStream& operator<<(bool val) { print(val ? "true" : "false"); return *this; }
ShellOutStream& operator<<(Color c) { printColor(c); return *this; }
ShellOutStream& operator<<(Endl) { print("\r\n"); return *this; }
// Manual Flush (WriteConsole is usually immediate, but for pipes WriteFile is buffered by OS)
void flush() {
if (!isConsole) FlushFileBuffers(hOut);
}
};
// --- Input Stream ---
class ShellInStream {
private:
std::mutex inputMutex;
HANDLE hIn;
bool isConsole;
std::string buffer;
bool readChunk() {
const DWORD CHUNK_SIZE = 128;
char chunk[CHUNK_SIZE];
DWORD read;
bool success;
if (isConsole) {
success = ReadConsoleA(hIn, chunk, CHUNK_SIZE - 1, &read, NULL);
} else {
success = ReadFile(hIn, chunk, CHUNK_SIZE - 1, &read, NULL);
}
if (success && read > 0) {
chunk[read] = '\0';
buffer += chunk;
return true;
}
return false;
}
void skipWhitespace() {
while (true) {
size_t firstNonSpace = buffer.find_first_not_of(" \t\r\n");
if (firstNonSpace == std::string::npos) {
buffer.clear();
if (!readChunk()) return;
} else {
buffer.erase(0, firstNonSpace);
return;
}
}
}
// Internal helper: assumes lock is held
std::string readTokenInternal() {
skipWhitespace();
if (buffer.empty() && !readChunk()) return "";
size_t end = buffer.find_first_of(" \t\r\n");
while (end == std::string::npos) {
if (!readChunk()) {
std::string token = buffer;
buffer.clear();
return token;
}
end = buffer.find_first_of(" \t\r\n");
}
std::string token = buffer.substr(0, end);
buffer.erase(0, end);
return token;
}
public:
ShellInStream() {
hIn = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
isConsole = GetConsoleMode(hIn, &mode);
}
explicit ShellInStream(HANDLE handle) : hIn(handle) {
DWORD mode;
isConsole = GetConsoleMode(hIn, &mode);
}
ShellInStream& operator>>(std::string& out) {
std::lock_guard<std::mutex> lock(inputMutex);
out = readTokenInternal();
return *this;
}
ShellInStream& operator>>(int& out) {
std::lock_guard<std::mutex> lock(inputMutex);
std::string token = readTokenInternal();
if (!token.empty()) {
try { out = std::stoi(token); } catch(...) {}
}
return *this;
}
ShellInStream& operator>>(float& out) {
std::lock_guard<std::mutex> lock(inputMutex);
std::string token = readTokenInternal();
if (!token.empty()) {
try { out = std::stof(token); } catch(...) {}
}
return *this;
}
bool getline(std::string& line) {
std::lock_guard<std::mutex> lock(inputMutex);
line.clear();
if (buffer.empty() && !readChunk()) return false;
size_t newline = buffer.find('\n');
while (newline == std::string::npos) {
if (!readChunk()) {
line = buffer;
buffer.clear();
return !line.empty();
}
newline = buffer.find('\n');
}
line = buffer.substr(0, newline);
if (!line.empty() && line.back() == '\r') line.pop_back();
buffer.erase(0, newline + 1);
return true;
}
};
// Singleton Instances
inline ShellOutStream sout(false);
inline ShellOutStream serr(true);
inline ShellInStream sin;
} // namespace ShellIO
#endif // LINUXIFY_SHELL_STREAMS_HPP