Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
343 lines (289 loc) · 12.5 KB
/
Copy pathmain.cpp
File metadata and controls
343 lines (289 loc) · 12.5 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
#include <cmath>
#include <chrono>
#include <csignal>
#include <cstdlib>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#include <poll.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
// ─── Arch Linux Logo (block characters) ────────────────────────────────────────
// Stripped of the original \033[...m prefix so we can apply our own colors.
static const std::vector<std::string> LOGO = {
R"( ▄)",
R"( ▟█▙)",
R"( ▟███▙)",
R"( ▟█████▙)",
R"( ▟███████▙)",
R"( ▂▔▀▜██████▙)",
R"( ▟██▅▂▝▜█████▙)",
R"( ▟█████████████▙)",
R"( ▟███████████████▙)",
R"( ▟█████████████████▙)",
R"( ▟███████████████████▙)",
R"( ▟█████████▛▀▀▜████████▙)",
R"( ▟████████▛ ▜███████▙)",
R"( ▟█████████ ████████▙)",
R"( ▟██████████ █████▆▅▄▃▂)",
R"( ▟██████████▛ ▜█████████▙)",
R"( ▟██████▀▀▀ ▀▀██████▙)",
R"( ▟███▀▘ ▝▀███▙)",
R"( ▟▛▀ ▀▜▙)",
};
// ─── Terminal State ─────────────────────────────────────────────────────────────
static struct termios g_origTermios;
static bool g_rawMode = false;
// Restore the terminal to its original state.
static void restoreTerminal() {
if (g_rawMode) {
// Disable mouse tracking
write(STDOUT_FILENO, "\033[?1000l", 8);
// Show cursor
write(STDOUT_FILENO, "\033[?25h", 6);
// Reset colors
write(STDOUT_FILENO, "\033[0m", 4);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &g_origTermios);
g_rawMode = false;
}
}
// Signal handler for clean exit on SIGINT / SIGTERM.
static void signalHandler(int) {
restoreTerminal();
write(STDOUT_FILENO, "\n", 1);
_exit(0);
}
// Put the terminal into raw mode so we get immediate keypresses.
static void enableRawMode() {
tcgetattr(STDIN_FILENO, &g_origTermios);
std::atexit(restoreTerminal);
std::signal(SIGINT, signalHandler);
std::signal(SIGTERM, signalHandler);
struct termios raw = g_origTermios;
raw.c_lflag &= ~(ECHO | ICANON | ISIG); // No echo, no canonical, no signals
raw.c_iflag &= ~(IXON | ICRNL | BRKINT); // No flow‑control, no CR→NL
raw.c_cc[VMIN] = 0; // Non‑blocking read
raw.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
g_rawMode = true;
// Hide cursor
write(STDOUT_FILENO, "\033[?25l", 6);
// Enable basic mouse click reporting (X10 mode)
write(STDOUT_FILENO, "\033[?1000h", 8);
}
// ─── Terminal Size ──────────────────────────────────────────────────────────────
static void getTermSize(int& cols, int& rows) {
struct winsize ws{};
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0 && ws.ws_col > 0) {
cols = ws.ws_col;
rows = ws.ws_row;
} else {
cols = 80;
rows = 24;
}
}
// ─── Non‑blocking input check ───────────────────────────────────────────────────
static bool hasInput() {
struct pollfd pfd{};
pfd.fd = STDIN_FILENO;
pfd.events = POLLIN;
return poll(&pfd, 1, 0) > 0;
}
// Drain any pending bytes from stdin.
static void drainInput() {
char buf[256];
while (read(STDIN_FILENO, buf, sizeof(buf)) > 0) {}
}
// ─── HSL → RGB Conversion ──────────────────────────────────────────────────────
struct RGB { int r, g, b; };
static RGB hslToRgb(double h, double s, double l) {
// h in [0, 360), s and l in [0, 1]
double c = (1.0 - std::fabs(2.0 * l - 1.0)) * s;
double hp = h / 60.0;
double x = c * (1.0 - std::fabs(std::fmod(hp, 2.0) - 1.0));
double r1 = 0, g1 = 0, b1 = 0;
if (hp < 1) { r1 = c; g1 = x; }
else if (hp < 2) { r1 = x; g1 = c; }
else if (hp < 3) { g1 = c; b1 = x; }
else if (hp < 4) { g1 = x; b1 = c; }
else if (hp < 5) { r1 = x; b1 = c; }
else { r1 = c; b1 = x; }
double m = l - c / 2.0;
return {
static_cast<int>(std::round((r1 + m) * 255.0)),
static_cast<int>(std::round((g1 + m) * 255.0)),
static_cast<int>(std::round((b1 + m) * 255.0)),
};
}
// ─── UTF‑8 Helpers ─────────────────────────────────────────────────────────────
// Split a UTF‑8 string into individual characters (each may be 1–4 bytes).
static std::vector<std::string> utf8Chars(const std::string& s) {
std::vector<std::string> chars;
size_t i = 0;
while (i < s.size()) {
unsigned char c = static_cast<unsigned char>(s[i]);
int len = 1;
if ((c & 0x80) == 0x00) len = 1;
else if ((c & 0xE0) == 0xC0) len = 2;
else if ((c & 0xF0) == 0xE0) len = 3;
else if ((c & 0xF8) == 0xF0) len = 4;
chars.push_back(s.substr(i, len));
i += len;
}
return chars;
}
// Count the number of visible (non-space-leading) characters for centering.
static int utf8VisualWidth(const std::string& s) {
return static_cast<int>(utf8Chars(s).size());
}
// ─── Compute the widest visual width in the logo ──────────────────────────────
static int logoMaxWidth() {
int maxW = 0;
for (const auto& line : LOGO) {
int w = utf8VisualWidth(line);
if (w > maxW) maxW = w;
}
return maxW;
}
// ─── Render a single frame ─────────────────────────────────────────────────────
static void renderFrame(double t) {
int termCols = 80, termRows = 24;
getTermSize(termCols, termRows);
const int totalLines = static_cast<int>(LOGO.size());
const int logoW = logoMaxWidth();
// +2 for the tagline and a blank line between logo and tagline.
int contentHeight = totalLines + 2;
int topPad = std::max(0, (termRows - contentHeight) / 2);
int leftPad = std::max(0, (termCols - logoW) / 2);
// Build the entire frame into a string buffer to minimize write() calls.
std::string buf;
buf.reserve(64 * 1024);
// Cursor home (no screen clear → flicker‑free).
buf += "\033[H";
// Vertical centering padding.
for (int i = 0; i < topPad; ++i) buf += "\033[2K\n";
for (int y = 0; y < totalLines; ++y) {
// Clear the line to prevent leftover artifacts on resize.
buf += "\033[2K";
auto chars = utf8Chars(LOGO[y]);
int lineW = static_cast<int>(chars.size());
// Horizontal centering: offset the entire line.
for (int i = 0; i < leftPad; ++i) buf += ' ';
int colIdx = 0;
for (const auto& ch : chars) {
// Skip spaces – just emit them without color to save bandwidth.
if (ch == " ") {
buf += ' ';
++colIdx;
continue;
}
// ── Smooth cyan gradient ──
// Hue stays in a tight cyan band [180, 195].
// The visual "wave" is created by modulating lightness with a sine.
double phase = static_cast<double>(y) * 0.35
+ static_cast<double>(colIdx) * 0.12
+ t * 1.8;
double wave = std::sin(phase); // in [-1, 1]
// Hue: subtle shift within cyan (180 ± 8).
double hue = 187.0 + wave * 8.0;
// Saturation: high, with a gentle pulse.
double sat = 0.88 + 0.07 * wave;
// Lightness: kept low for a darker cyan, with smooth oscillation.
double lit = 0.38 + 0.10 * wave;
RGB rgb = hslToRgb(hue, sat, lit);
buf += "\033[38;2;";
buf += std::to_string(rgb.r); buf += ';';
buf += std::to_string(rgb.g); buf += ';';
buf += std::to_string(rgb.b); buf += 'm';
buf += ch;
++colIdx;
}
// Pad the rest of the line to avoid trailing garbage on resize.
int remaining = termCols - leftPad - lineW;
for (int i = 0; i < remaining; ++i) buf += ' ';
buf += "\033[0m\n";
}
// "I use Arch btw" tagline centered below the logo.
{
const std::string tagline = "I use Arch btw";
auto tagChars = utf8Chars(tagline);
int tagLen = static_cast<int>(tagChars.size());
int tagPad = std::max(0, (termCols - tagLen) / 2);
buf += "\033[2K\n\033[2K";
for (int i = 0; i < tagPad; ++i) buf += ' ';
for (int i = 0; i < tagLen; ++i) {
if (tagChars[i] == " ") {
buf += ' ';
continue;
}
double phase = static_cast<double>(i) * 0.5 + t * 2.2;
double wave = std::sin(phase);
double hue = 187.0 + wave * 8.0;
double sat = 0.90 + 0.05 * wave;
double lit = 0.40 + 0.10 * wave;
RGB rgb = hslToRgb(hue, sat, lit);
buf += "\033[38;2;";
buf += std::to_string(rgb.r); buf += ';';
buf += std::to_string(rgb.g); buf += ';';
buf += std::to_string(rgb.b); buf += 'm';
buf += tagChars[i];
}
buf += "\033[0m\n";
}
// Clear any remaining rows below to handle terminal shrinking.
int usedRows = topPad + totalLines + 2;
for (int i = usedRows; i < termRows; ++i) buf += "\033[2K\n";
write(STDOUT_FILENO, buf.data(), buf.size());
}
// ─── Render the goodbye message with a cyan gradient ───────────────────────────
static void renderGoodbye() {
const std::string msg = "Goodbye Arch user!";
auto chars = utf8Chars(msg);
int len = static_cast<int>(chars.size());
std::string buf;
for (int i = 0; i < len; ++i) {
if (chars[i] == " ") {
buf += ' ';
continue;
}
// Dark cyan gradient across the message.
double t = static_cast<double>(i) / static_cast<double>(len - 1);
double hue = 185.0 + t * 10.0; // [185, 195]
double lit = 0.30 + t * 0.12; // dark cyan, slightly brighter at end
RGB rgb = hslToRgb(hue, 0.95, lit);
buf += "\033[38;2;";
buf += std::to_string(rgb.r); buf += ';';
buf += std::to_string(rgb.g); buf += ';';
buf += std::to_string(rgb.b); buf += 'm';
buf += chars[i];
}
buf += "\033[0m\n";
write(STDOUT_FILENO, buf.data(), buf.size());
}
// ─── Main ──────────────────────────────────────────────────────────────────────
int main() {
enableRawMode();
// Clear the entire screen once at startup.
write(STDOUT_FILENO, "\033[2J", 4);
using Clock = std::chrono::steady_clock;
auto start = Clock::now();
for (;;) {
double elapsed = std::chrono::duration<double>(Clock::now() - start).count();
renderFrame(elapsed);
// Check for any keypress or mouse click.
if (hasInput()) {
drainInput();
break;
}
// ~30 fps
std::this_thread::sleep_for(std::chrono::milliseconds(33));
}
// Clean up and say goodbye.
restoreTerminal();
// Clear the screen one final time for a clean exit.
write(STDOUT_FILENO, "\033[2J\033[H", 7);
renderGoodbye();
return 0;
}