-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnhclock.c
296 lines (296 loc) · 7.31 KB
/
nhclock.c
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
// SPDX-License-Identifier: MIT
/*
*
* This file is part of nothing-hexclock, with ABSOLUTELY NO WARRANTY.
*
* MIT License
*
* Copyright (c) 2023 Moe-hacker
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
// I think you can understand these simple code.
// So I needn't write comments.
// If you can't, you can just rewrite a better one.
// Trust me, it's very very easy.
#include "nhclock.h"
static short x = 0;
static short y = 0;
static int offset = 0;
char *hexclock = " ● ● ●●●●● ● ●\n"
" ● ● ● ● ●\n"
" ●●●●● ●●●● ●\n"
" ● ● ● ● ●\n"
" ● ● ●●●●● ● ●\n"
"\n"
" ●●● ● ●●● ●●● ● ●\n"
"● ● ● ● ● ● ● ● ●\n"
"● ● ● ● ● ●●●\n"
"● ● ● ● ● ● ● ● ●\n"
" ●●● ●●●●● ●●● ●●● ● ●";
void on_exit(int _unused) {
printf("\033[0m\033[?25h\ec");
exit(0);
}
void show_logo(void) {
struct winsize size;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
short col = size.ws_col;
col /= 2;
col -= 13;
short rol = size.ws_row;
rol /= 2;
rol -= 4;
printf("\ec\033[?25l\033[1;38;2;254;228;208m");
printf("\033[%dH\033[%dG", rol, col);
for (size_t i = 0; i < strlen(hexclock); i++) {
if (hexclock[i] != ' ') {
usleep(9000);
}
if (hexclock[i] == '\n') {
printf("\n\033[%dG", col);
} else {
printf("%c", hexclock[i]);
}
fflush(stdout);
}
sleep(1);
printf("\033[%dH\033[%dG", rol, col);
for (size_t i = 0; i < strlen(hexclock); i++) {
if (hexclock[i] != ' ') {
if (hexclock[i] == '\n') {
printf("\n\033[%dG", col);
} else {
printf("%s", "×");
i++;
i++;
}
usleep(7000);
} else {
printf("%c", ' ');
}
fflush(stdout);
}
sleep(1);
printf("\033[%dH\033[%dG", rol, col);
for (size_t i = 0; i < strlen(hexclock); i++) {
if (hexclock[i] != ' ') {
usleep(3000);
}
if (hexclock[i] == '\n') {
printf("\n\033[%dG", col);
} else {
printf("%c", hexclock[i]);
}
fflush(stdout);
}
sleep(1);
printf("\033[%dH\033[%dG", rol, col);
for (size_t i = 0; i < strlen(hexclock); i++) {
if (hexclock[i] != ' ') {
if (hexclock[i] == '\n') {
printf("\n\033[%dG", col);
} else {
printf("%c", ' ');
i++;
i++;
}
usleep(7000);
} else {
printf("%c", ' ');
}
fflush(stdout);
}
}
void move(void) {
switch (offset) {
case 0:
x += 1;
offset = 1;
break;
case 1:
y += 1;
offset = 2;
break;
case 2:
x += 1;
offset = 3;
break;
case 3:
y += 1;
offset = 4;
break;
case 4:
x -= 1;
offset = 5;
break;
case 5:
y -= 1;
offset = 6;
break;
case 6:
x -= 1;
offset = 7;
break;
case 7:
y -= 1;
offset = 0;
break;
}
}
void clear(struct CHARS *chars) {
chars->line0[0] = '\000';
chars->line1[0] = '\000';
chars->line2[0] = '\000';
chars->line3[0] = '\000';
chars->line4[0] = '\000';
}
void add_number(struct CHARS *chars, int num) {
struct character character;
switch (num) {
case 0:
character = zero;
break;
case 1:
character = one;
break;
case 2:
character = two;
break;
case 3:
character = three;
break;
case 4:
character = four;
break;
case 5:
character = five;
break;
case 6:
character = six;
break;
case 7:
character = seven;
break;
case 8:
character = eight;
break;
case 9:
character = nine;
break;
case 10:
character = hash;
break;
}
strcat(chars->line0, " ");
strcat(chars->line0, character.line0);
strcat(chars->line1, " ");
strcat(chars->line1, character.line1);
strcat(chars->line2, " ");
strcat(chars->line2, character.line2);
strcat(chars->line3, " ");
strcat(chars->line3, character.line3);
strcat(chars->line4, " ");
strcat(chars->line4, character.line4);
}
void print_chars(struct CHARS *chars) {
printf("%s", chars->init);
printf("%s\n", chars->line0);
printf("%s\n", chars->line1);
printf("%s\n", chars->line2);
printf("%s\n", chars->line3);
printf("%s\n", chars->line4);
fflush(stdout);
}
void init_chars(struct CHARS *chars, struct tm *now) {
char buf[128] = {0};
int tmp = 0;
strcpy(chars->init, "\033[1;38;2;");
tmp = now->tm_hour % 10 * 2.6;
tmp += (now->tm_hour / 10) * 16 * 2.6;
buf[0] = 0;
sprintf(buf, "%d", tmp);
strcat(chars->init, buf);
strcat(chars->init, ";");
tmp = now->tm_min % 10 * 2.6;
tmp += (now->tm_min / 10) * 16 * 2.6;
buf[0] = 0;
sprintf(buf, "%d", tmp);
strcat(chars->init, buf);
strcat(chars->init, ";");
tmp = now->tm_sec % 10 * 2.6;
tmp += (now->tm_sec / 10) * 16 * 2.6;
buf[0] = 0;
sprintf(buf, "%d", tmp);
strcat(chars->init, buf);
strcat(chars->init, "m");
buf[0] = 0;
sprintf(buf, "\033[%dH", y);
strcat(chars->init, buf);
buf[0] = 0;
sprintf(buf, "\033[%dG", x);
strcat(chars->line0, buf);
strcpy(chars->line1, chars->line0);
strcpy(chars->line2, chars->line0);
strcpy(chars->line3, chars->line0);
strcpy(chars->line4, chars->line0);
}
void init(void) {
struct winsize size;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
if (size.ws_col < 42 || size.ws_row < 6) {
fprintf(stderr, "\033[31mWindow size too small!\033[0m\n");
exit(1);
}
signal(SIGINT, on_exit);
x = size.ws_col;
x /= 2;
x -= 23;
y = size.ws_row;
y /= 2;
y -= 1;
show_logo();
}
int main(int argc, char **argv) {
init();
struct CHARS chars;
time_t timer;
struct tm *now;
while (true) {
time(&timer);
now = localtime(&timer);
clear(&chars);
init_chars(&chars, now);
add_number(&chars, 10);
add_number(&chars, now->tm_hour / 10);
add_number(&chars, now->tm_hour % 10);
add_number(&chars, now->tm_min / 10);
add_number(&chars, now->tm_min % 10);
add_number(&chars, now->tm_sec / 10);
add_number(&chars, now->tm_sec % 10);
print_chars(&chars);
sleep(1);
if (now->tm_sec % 10 == 0) {
printf("\ec\033[?25l");
move();
}
}
}