-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwinfetch.cpp
More file actions
372 lines (328 loc) · 12.4 KB
/
winfetch.cpp
File metadata and controls
372 lines (328 loc) · 12.4 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <sstream>
#include <windows.h>
#include <conio.h>
// Link necessary Windows libraries
#pragma comment(lib, "Advapi32.lib")
#pragma comment(lib, "User32.lib")
// ANSI Escape Codes for text formatting
#define RESET "\x1b[0m"
#define BOLD "\x1b[1m"
// Foreground colors
#define BLACK "\x1b[30m"
#define RED "\x1b[31m"
#define GREEN "\x1b[32m"
#define YELLOW "\x1b[33m"
#define BLUE "\x1b[34m"
#define MAGENTA "\x1b[35m"
#define CYAN "\x1b[36m"
#define WHITE "\x1b[37m"
// Bright foreground colors
#define BRIGHT_BLACK "\x1b[90m"
#define BRIGHT_RED "\x1b[91m"
#define BRIGHT_GREEN "\x1b[92m"
#define BRIGHT_YELLOW "\x1b[93m"
#define BRIGHT_BLUE "\x1b[94m"
#define BRIGHT_MAGENTA "\x1b[95m"
#define BRIGHT_CYAN "\x1b[96m"
#define BRIGHT_WHITE "\x1b[97m"
// Background colors
#define BG_BLACK "\x1b[40m"
#define BG_RED "\x1b[41m"
#define BG_GREEN "\x1b[42m"
#define BG_YELLOW "\x1b[43m"
#define BG_BLUE "\x1b[44m"
#define BG_MAGENTA "\x1b[45m"
#define BG_CYAN "\x1b[46m"
#define BG_WHITE "\x1b[47m"
#define BG_BRIGHT_BLACK "\x1b[100m"
#define BG_BRIGHT_RED "\x1b[101m"
#define BG_BRIGHT_GREEN "\x1b[102m"
#define BG_BRIGHT_YELLOW "\x1b[103m"
#define BG_BRIGHT_BLUE "\x1b[104m"
#define BG_BRIGHT_MAGENTA "\x1b[105m"
#define BG_BRIGHT_CYAN "\x1b[106m"
#define BG_BRIGHT_WHITE "\x1b[107m"
// Console control codes
#define CLEAR_SCREEN "\x1b[2J"
#define CURSOR_HOME "\x1b[H"
#define HIDE_CURSOR "\x1b[?25l"
#define SHOW_CURSOR "\x1b[?25h"
using namespace std;
// ============================================================
// Enables ANSI escape sequences in the Windows console
// ============================================================
void enableANSI() {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode = 0;
GetConsoleMode(hOut, &dwMode);
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hOut, dwMode);
SetConsoleOutputCP(65001);
}
// ============================================================
// Helper functions to read from the Windows Registry
// ============================================================
string getRegString(HKEY baseKey, const char* subKey, const char* valueName) {
HKEY hKey;
if (RegOpenKeyExA(baseKey, subKey, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
return "N/A";
char value[512];
DWORD valSize = sizeof(value);
DWORD type = 0;
if (RegQueryValueExA(hKey, valueName, NULL, &type, (LPBYTE)value, &valSize) != ERROR_SUCCESS) {
RegCloseKey(hKey);
return "N/A";
}
RegCloseKey(hKey);
return string(value);
}
DWORD getRegDword(HKEY baseKey, const char* subKey, const char* valueName) {
HKEY hKey;
if (RegOpenKeyExA(baseKey, subKey, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
return 0;
DWORD value = 0, valSize = sizeof(DWORD), type = 0;
RegQueryValueExA(hKey, valueName, NULL, &type, (LPBYTE)&value, &valSize);
RegCloseKey(hKey);
return value;
}
// ============================================================
// System Information Fetchers
// ============================================================
// Fetches username and hostname (e.g., user@desktop)
string getUserHost() {
char username[256], hostname[256];
DWORD unSize = sizeof(username), hnSize = sizeof(hostname);
GetUserNameA(username, &unSize);
GetComputerNameA(hostname, &hnSize);
return string(username) + "@" + string(hostname);
}
// Retrieves OS product name and version
string getOS() {
string name = getRegString(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "ProductName");
string displayVer = getRegString(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "DisplayVersion");
string build = getRegString(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "CurrentBuildNumber");
string result = name;
if (displayVer != "N/A" && !displayVer.empty())
result += " " + displayVer;
if (build != "N/A" && !build.empty())
result += " (Build " + build + ")";
return result;
}
// Retrieves Windows Kernel version details
string getKernel() {
string major = to_string(getRegDword(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "CurrentMajorVersionNumber"));
string minor = to_string(getRegDword(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "CurrentMinorVersionNumber"));
string build = getRegString(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "CurrentBuildNumber");
string ubr = to_string(getRegDword(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "UBR"));
return major + "." + minor + "." + build + "." + ubr;
}
// Retrieves CPU model and thread count
string getCPU() {
string cpu = getRegString(HKEY_LOCAL_MACHINE,
"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", "ProcessorNameString");
string result;
bool lastSpace = false;
for (char c : cpu) {
if (c == ' ') {
if (!lastSpace) { result += c; lastSpace = true; }
} else {
result += c; lastSpace = false;
}
}
SYSTEM_INFO si;
GetSystemInfo(&si);
result += " (" + to_string(si.dwNumberOfProcessors) + " threads)";
return result;
}
// Retrieves GPU model(s)
string getGPU() {
DISPLAY_DEVICEA dd;
dd.cb = sizeof(dd);
set<string> seen;
vector<string> gpuList;
for (int i = 0; EnumDisplayDevicesA(NULL, i, &dd, 0); i++) {
if (dd.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER) continue;
string name(dd.DeviceString);
if (name.empty()) continue;
if (seen.count(name)) continue;
seen.insert(name);
gpuList.push_back(name);
}
if (gpuList.empty()) return "N/A";
string result;
for (size_t i = 0; i < gpuList.size(); i++) {
if (i > 0) result += ", ";
result += gpuList[i];
}
return result;
}
// Retrieves Physical RAM usage
string getRAM() {
MEMORYSTATUSEX mem;
mem.dwLength = sizeof(mem);
GlobalMemoryStatusEx(&mem);
DWORDLONG totalMB = mem.ullTotalPhys / (1024 * 1024);
DWORDLONG usedMB = (mem.ullTotalPhys - mem.ullAvailPhys) / (1024 * 1024);
return to_string(usedMB) + " MB / " + to_string(totalMB) + " MB ("
+ to_string(mem.dwMemoryLoad) + "%)";
}
// Calculates system uptime
string getUptime() {
ULONGLONG ms = GetTickCount64();
int days = (int)(ms / (1000ULL * 60 * 60 * 24));
int hours = (int)((ms / (1000ULL * 60 * 60)) % 24);
int mins = (int)((ms / (1000ULL * 60)) % 60);
int secs = (int)((ms / 1000ULL) % 60);
string r;
if (days > 0) r += to_string(days) + "d ";
if (hours > 0) r += to_string(hours) + "h ";
r += to_string(mins) + "m " + to_string(secs) + "s";
return r;
}
// Retrieves primary display resolution and refresh rate
string getResolution() {
int w = GetSystemMetrics(SM_CXSCREEN);
int h = GetSystemMetrics(SM_CYSCREEN);
DEVMODEA dm;
memset(&dm, 0, sizeof(dm));
dm.dmSize = sizeof(dm);
string hz;
if (EnumDisplaySettingsA(NULL, ENUM_CURRENT_SETTINGS, &dm))
hz = " @ " + to_string(dm.dmDisplayFrequency) + " Hz";
return to_string(w) + "x" + to_string(h) + hz;
}
// Retrieves the current shell environment
string getShell() {
char* comspec = getenv("COMSPEC");
if (comspec) return string(comspec);
return "cmd.exe";
}
// Retrieves disk usage for fixed drives
string getDisk() {
string result;
DWORD drives = GetLogicalDrives();
for (int i = 0; i < 26; i++) {
if (!(drives & (1 << i))) continue;
char root[] = { (char)('A' + i), ':', '\\', '\0' };
if (GetDriveTypeA(root) != DRIVE_FIXED) continue;
ULARGE_INTEGER freeB, totalB, totalFreeB;
if (!GetDiskFreeSpaceExA(root, &freeB, &totalB, &totalFreeB)) continue;
ULONGLONG totalGB = totalB.QuadPart / (1024ULL * 1024 * 1024);
ULONGLONG usedGB = (totalB.QuadPart - totalFreeB.QuadPart) / (1024ULL * 1024 * 1024);
int pct = (int)(usedGB * 100 / (totalGB ? totalGB : 1));
if (!result.empty()) result += " | ";
result += string(root, 2) + " " + to_string(usedGB) + "G/"
+ to_string(totalGB) + "G (" + to_string(pct) + "%)";
}
return result.empty() ? "N/A" : result;
}
// ============================================================
// UI Rendering & Color Palette
// ============================================================
string makeColorBlock(const char* bgColor, int width = 3) {
string spaces(width, ' ');
return string(bgColor) + spaces + RESET;
}
string getPaletteLine1() {
return makeColorBlock(BG_BLACK) + makeColorBlock(BG_RED)
+ makeColorBlock(BG_GREEN) + makeColorBlock(BG_YELLOW)
+ makeColorBlock(BG_BLUE) + makeColorBlock(BG_MAGENTA)
+ makeColorBlock(BG_CYAN) + makeColorBlock(BG_WHITE);
}
string getPaletteLine2() {
return makeColorBlock(BG_BRIGHT_BLACK) + makeColorBlock(BG_BRIGHT_RED)
+ makeColorBlock(BG_BRIGHT_GREEN) + makeColorBlock(BG_BRIGHT_YELLOW)
+ makeColorBlock(BG_BRIGHT_BLUE) + makeColorBlock(BG_BRIGHT_MAGENTA)
+ makeColorBlock(BG_BRIGHT_CYAN) + makeColorBlock(BG_BRIGHT_WHITE);
}
// ============================================================
// Draws the ASCII logo and system information side-by-side
// ============================================================
void drawFrame(const vector<string>& logo) {
string userHost = getUserHost();
string separator(userHost.length(), '-');
vector<string> info = {
CYAN BOLD + userHost + RESET,
CYAN + separator + RESET,
CYAN BOLD "OS: " RESET + getOS(),
CYAN BOLD "Kernel: " RESET + getKernel(),
CYAN BOLD "Uptime: " RESET + getUptime(),
CYAN BOLD "Shell: " RESET + getShell(),
CYAN BOLD "Resolution: " RESET + getResolution(),
CYAN BOLD "CPU: " RESET + getCPU(),
CYAN BOLD "GPU: " RESET + getGPU(),
CYAN BOLD "Memory: " RESET + getRAM(),
CYAN BOLD "Disk: " RESET + getDisk(),
"",
getPaletteLine1(),
getPaletteLine2(),
"",
BRIGHT_BLACK "[Press Q or Ctrl+C to exit]" RESET
};
size_t maxLines = max(logo.size(), info.size());
cout << CURSOR_HOME;
cout << "\n";
for (size_t i = 0; i < maxLines; i++) {
if (i < logo.size())
cout << " " << logo[i];
cout << "\x1b[42G";
if (i < info.size())
cout << info[i];
cout << "\x1b[K";
cout << RESET << "\n";
}
cout << "\x1b[J";
cout.flush();
}
// ============================================================
// MAIN
// ============================================================
int main() {
enableANSI();
cout << HIDE_CURSOR;
cout << CLEAR_SCREEN;
vector<string> logo = {
RED " ,.=:!!t3Z3z., ",
RED " :tt:::tt333EE3 ",
RED " Et:::ztt33EEEL" GREEN " @Ee., .., ",
RED " ;tt:::tt333EE7" GREEN " ;EEEEEEttttt33# ",
RED " :Et:::zt333EEQ." GREEN " $EEEEEttttt33QL ",
RED " it::::tt333EEF" GREEN " @EEEEEEttttt33F ",
RED " ;3=*^```\"*4EEV" GREEN " :EEEEEEttttt33@.",
BLUE " ,.=::::!t=., " RED "`" GREEN " @EEEEEEtttz33QF",
BLUE " ;::::::::zt33) " GREEN "\"4EEEtttji3P* ",
BLUE " :t::::::::tt33." YELLOW ":Z3z.. " GREEN "``" YELLOW " ,..g. ",
BLUE " i::::::::zt33F" YELLOW " AEEEtttt::::ztF ",
BLUE " ;:::::::::t33V" YELLOW " ;EEEttttt::::t3 ",
BLUE " E::::::::zt33L" YELLOW " @EEEtttt::::z3F ",
BLUE "{3=*^```\"*4E3)" YELLOW " ;EEEtttt:::::tZ` ",
BLUE " `" YELLOW " :EEEEtttt::::z7 ",
YELLOW " \"VEzjt:;;z>*` "
};
while (true) {
drawFrame(logo);
for (int tick = 0; tick < 20; tick++) {
Sleep(50);
if (_kbhit()) {
int ch = _getch();
if (ch == 'q' || ch == 'Q' || ch == 27) {
cout << SHOW_CURSOR << RESET;
return 0;
}
}
}
}
cout << SHOW_CURSOR << RESET;
return 0;
}