-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathfetch.c
More file actions
2327 lines (2136 loc) · 69.8 KB
/
fetch.c
File metadata and controls
2327 lines (2136 loc) · 69.8 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <dirent.h>
#include <math.h>
#include <poll.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/statvfs.h>
#include <sys/utsname.h>
#include <termios.h>
#include <unistd.h>
static struct termios orig_termios;
static int termios_saved = 0;
static void cleanup(void) {
if (termios_saved)
tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios);
printf("\033[?25h");
fflush(stdout);
}
static void handle_signal(int sig) {
(void)sig;
cleanup();
_exit(0);
}
static volatile int term_resized = 0;
static void handle_winch(int sig) {
(void)sig;
term_resized = 1;
}
static int get_term_rows(void) {
struct winsize ws;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0 && ws.ws_row > 0)
return ws.ws_row;
return 0;
}
#define ANIM_WIDTH 60
#define MAX_HEIGHT 200
#define GAP 2
static int render_height = 36;
#define PI 3.14159265f
// --- UTF-8 helpers ---
// Returns byte length of a UTF-8 sequence from its leading byte
static int utf8_char_len(unsigned char c) {
if (c < 0x80) return 1;
if ((c & 0xE0) == 0xC0) return 2;
if ((c & 0xF0) == 0xE0) return 3;
if ((c & 0xF8) == 0xF0) return 4;
return 1; // invalid, treat as 1
}
// Skip past an ANSI escape sequence (ESC [ ... letter)
// Returns number of bytes to skip, or 0 if not an escape
static int skip_ansi(const char *p) {
if (p[0] != '\033' || p[1] != '[')
return 0;
int i = 2;
while (p[i] && ((p[i] >= '0' && p[i] <= '9') || p[i] == ';'))
i++;
if (p[i])
i++; // skip the final letter
return i;
}
// Parse a UTF-8 string into individual codepoints
#define MAX_SHADING 64
static char shading_chars[MAX_SHADING][5];
static int shading_count = 0;
static void parse_shading(const char *str) {
shading_count = 0;
const char *p = str;
while (*p && shading_count < MAX_SHADING) {
int len = utf8_char_len((unsigned char)*p);
if (len > 4) len = 4;
// Make sure we don't read past end of string
int actual = 0;
while (actual < len && p[actual])
actual++;
memcpy(shading_chars[shading_count], p, actual);
shading_chars[shading_count][actual] = '\0';
shading_count++;
p += actual;
}
if (shading_count == 0) {
// Fallback
strcpy(shading_chars[0], ".");
shading_count = 1;
}
}
// --- Logo storage (codepoint-aware) ---
#define MAX_LOGO_ROWS 64
#define MAX_LOGO_COLS 128
// Raw byte data
static char logo_data[MAX_LOGO_ROWS][512];
// Parsed per-cell codepoints
static char logo_cells[MAX_LOGO_ROWS][MAX_LOGO_COLS][5];
static int logo_cell_color[MAX_LOGO_ROWS][MAX_LOGO_COLS]; // ANSI fg color per cell
static int logo_cell_counts[MAX_LOGO_ROWS];
static int logo_rows = 0;
static int logo_cols = 0;
static int logo_has_ansi = 0;
// Process a logo row: split into codepoints, extracting ANSI colors
static void process_logo_row(int row) {
const char *p = logo_data[row];
int col = 0;
int cur_color = 0;
while (*p && col < MAX_LOGO_COLS) {
// Parse ANSI escapes for color info
if (p[0] == '\033' && p[1] == '[') {
int i = 2;
// Extract foreground color from SGR params
int num = 0, has_num = 0;
while (p[i] && ((p[i] >= '0' && p[i] <= '9') || p[i] == ';')) {
if (p[i] >= '0' && p[i] <= '9') {
num = num * 10 + (p[i] - '0');
has_num = 1;
} else if (p[i] == ';') {
if (has_num && ((num >= 30 && num <= 37) || num == 39 || (num >= 90 && num <= 97)))
cur_color = num;
if (has_num && num == 0)
cur_color = 0;
num = 0;
has_num = 0;
}
i++;
}
if (has_num && ((num >= 30 && num <= 37) || num == 39 || (num >= 90 && num <= 97)))
cur_color = num;
if (has_num && num == 0)
cur_color = 0;
if (p[i])
i++;
if (cur_color > 0)
logo_has_ansi = 1;
p += i;
continue;
}
int len = utf8_char_len((unsigned char)*p);
int actual = 0;
while (actual < len && p[actual])
actual++;
memcpy(logo_cells[row][col], p, actual);
logo_cells[row][col][actual] = '\0';
logo_cell_color[row][col] = cur_color;
col++;
p += actual;
}
logo_cell_counts[row] = col;
if (col > logo_cols)
logo_cols = col;
}
// Process all loaded logo rows
static void process_logo(void) {
logo_cols = 0;
for (int r = 0; r < logo_rows; r++)
process_logo_row(r);
}
// --- char_weight for UTF-8 codepoints ---
static float char_weight_utf8(const char *ch) {
// Single-byte ASCII
if ((unsigned char)ch[0] < 0x80) {
switch (ch[0]) {
case 'M': return 1.00f;
case 'N': return 0.88f;
case 'm': return 0.76f;
case 'd': return 0.66f;
case 'h': return 0.56f;
case 'b': return 0.56f;
case 'y': return 0.46f;
case 'o': return 0.38f;
case 'n': return 0.38f;
case 's': return 0.30f;
case '+': return 0.22f;
case ':': return 0.18f;
case '=': return 0.22f;
case '-': return 0.14f;
case '`': return 0.08f;
case '.': return 0.10f;
case '/': return 0.12f;
case '\'': return 0.06f;
case ' ': return 0.0f;
default:
// Generic: uppercase heavy, lowercase medium, punct light
if (ch[0] >= 'A' && ch[0] <= 'Z') return 0.80f;
if (ch[0] >= 'a' && ch[0] <= 'z') return 0.50f;
if (ch[0] >= '0' && ch[0] <= '9') return 0.40f;
return 0.15f;
}
}
// Multi-byte UTF-8: compare raw bytes for common block elements
// Full block U+2588: E2 96 88
if (memcmp(ch, "\xe2\x96\x88", 3) == 0) return 1.00f;
// Dark shade U+2593: E2 96 93
if (memcmp(ch, "\xe2\x96\x93", 3) == 0) return 0.75f;
// Medium shade U+2592: E2 96 92
if (memcmp(ch, "\xe2\x96\x92", 3) == 0) return 0.50f;
// Light shade U+2591: E2 96 91
if (memcmp(ch, "\xe2\x96\x91", 3) == 0) return 0.25f;
// Half blocks (U+2580-258F)
// Upper half U+2580: E2 96 80
if (memcmp(ch, "\xe2\x96\x80", 3) == 0) return 0.50f;
// Lower half U+2584: E2 96 84
if (memcmp(ch, "\xe2\x96\x84", 3) == 0) return 0.50f;
// Left half U+258C: E2 96 8C
if (memcmp(ch, "\xe2\x96\x8c", 3) == 0) return 0.50f;
// Right half U+2590: E2 96 90
if (memcmp(ch, "\xe2\x96\x90", 3) == 0) return 0.50f;
// 3/4 blocks
// U+259B ▛: E2 96 9B
if (memcmp(ch, "\xe2\x96\x9b", 3) == 0) return 0.75f;
// U+259C ▜: E2 96 9C
if (memcmp(ch, "\xe2\x96\x9c", 3) == 0) return 0.75f;
// U+2599 ▙: E2 96 99
if (memcmp(ch, "\xe2\x96\x99", 3) == 0) return 0.75f;
// U+259F ▟: E2 96 9F
if (memcmp(ch, "\xe2\x96\x9f", 3) == 0) return 0.75f;
// 1/4 blocks
// U+2596 ▖: E2 96 96
if (memcmp(ch, "\xe2\x96\x96", 3) == 0) return 0.25f;
// U+2597 ▗: E2 96 97
if (memcmp(ch, "\xe2\x96\x97", 3) == 0) return 0.25f;
// U+2598 ▘: E2 96 98
if (memcmp(ch, "\xe2\x96\x98", 3) == 0) return 0.25f;
// U+259D ▝: E2 96 9D
if (memcmp(ch, "\xe2\x96\x9d", 3) == 0) return 0.25f;
// Box drawing chars (U+2500-257F): E2 94 xx / E2 95 xx
if ((unsigned char)ch[0] == 0xe2 &&
((unsigned char)ch[1] == 0x94 || (unsigned char)ch[1] == 0x95))
return 0.20f;
// Braille (U+2800-28FF): E2 A0-A3 xx
if ((unsigned char)ch[0] == 0xe2 &&
(unsigned char)ch[1] >= 0xa0 && (unsigned char)ch[1] <= 0xa3) {
// Weight by number of dots (popcount of last byte)
unsigned char b = (unsigned char)ch[2];
int dots = 0;
while (b) { dots += b & 1; b >>= 1; }
return dots / 8.0f;
}
// Default for unknown multi-byte: treat as medium fill
return 0.30f;
}
static char file_distro[64] = "";
static int load_logo_file(void) {
char path[512];
const char *home = getenv("HOME");
if (!home)
return 0;
snprintf(path, sizeof(path), "%s/.config/fetch/logo.txt", home);
FILE *fp = fopen(path, "r");
if (!fp)
return 0;
char buf[512];
while (logo_rows < MAX_LOGO_ROWS && fgets(buf, sizeof(buf), fp)) {
int len = strlen(buf);
while (len > 0 && (buf[len - 1] == '\n' || buf[len - 1] == '\r'))
buf[--len] = '\0';
if (logo_rows == 0 && strncmp(buf, "# distro:", 9) == 0) {
char *val = buf + 9;
while (*val == ' ')
val++;
strncpy(file_distro, val, sizeof(file_distro) - 1);
continue;
}
if (len == 0 && logo_rows == 0)
continue;
memcpy(logo_data[logo_rows], buf, len + 1);
logo_rows++;
}
fclose(fp);
while (logo_rows > 0 && logo_data[logo_rows - 1][0] == '\0')
logo_rows--;
return logo_rows > 0;
}
// Check if an ANSI escape is a cursor movement (not a color/SGR escape)
static int is_cursor_escape(const char *p) {
if (p[0] != '\033' || p[1] != '[')
return 0;
int i = 2;
while (p[i] && ((p[i] >= '0' && p[i] <= '9') || p[i] == ';'))
i++;
return (p[i] && p[i] != 'm');
}
// Try loading a logo from fastfetch colored output
static int load_logo_ff_colored(const char *name) {
char cmd[256];
snprintf(cmd, sizeof(cmd),
"fastfetch -l %s --structure \"\" --pipe false 2>/dev/null", name);
FILE *fp = popen(cmd, "r");
if (!fp)
return 0;
char buf[512];
while (logo_rows < MAX_LOGO_ROWS && fgets(buf, sizeof(buf), fp)) {
int len = strlen(buf);
while (len > 0 && (buf[len - 1] == '\n' || buf[len - 1] == '\r'))
buf[--len] = '\0';
// Truncate at first cursor movement escape (marks end of logo content)
int truncated = 0;
for (int i = 0; i < len - 2; i++) {
if (is_cursor_escape(&buf[i])) {
int cut = i;
if (cut >= 3 && buf[cut - 1] == 'm' && buf[cut - 2] == '[' &&
buf[cut - 3] == '\033')
cut -= 3;
else if (cut >= 4 && buf[cut - 1] == 'm' && buf[cut - 2] == '0' &&
buf[cut - 3] == '[' && buf[cut - 4] == '\033')
cut -= 4;
buf[cut] = '\0';
len = cut;
truncated = 1;
break;
}
}
if (len == 0 && logo_rows == 0)
continue;
if (len == 0 && truncated)
break;
memcpy(logo_data[logo_rows], buf, len + 1);
logo_rows++;
}
pclose(fp);
while (logo_rows > 0 && logo_data[logo_rows - 1][0] == '\0')
logo_rows--;
return logo_rows > 0;
}
// Fallback: load from --print-logos (no colors, but works on older fastfetch)
static int load_logo_ff_plain(const char *name) {
FILE *fp = popen("fastfetch --print-logos 2>/dev/null", "r");
if (!fp)
return 0;
char buf[512];
int found = 0;
int name_len = strlen(name);
while (fgets(buf, sizeof(buf), fp)) {
int len = strlen(buf);
while (len > 0 && (buf[len - 1] == '\n' || buf[len - 1] == '\r'))
buf[--len] = '\0';
if (!found) {
if (len > 0 && len <= name_len + 1 && buf[len - 1] == ':') {
buf[len - 1] = '\0';
if (strcasecmp(buf, name) == 0)
found = 1;
}
continue;
}
// Detect next logo header
if (len > 1 && len < 40 && buf[len - 1] == ':' && logo_rows > 0 &&
((buf[0] >= 'A' && buf[0] <= 'Z') || (buf[0] >= 'a' && buf[0] <= 'z'))) {
int is_header = 1;
for (int i = 0; i < len; i++) {
if (buf[i] == '\033') {
is_header = 0;
break;
}
}
if (is_header)
break;
}
if (logo_rows >= MAX_LOGO_ROWS)
break;
memcpy(logo_data[logo_rows], buf, len + 1);
logo_rows++;
}
pclose(fp);
while (logo_rows > 0 && logo_data[logo_rows - 1][0] == '\0')
logo_rows--;
return logo_rows > 0;
}
static int load_logo_fastfetch(const char *name) {
// Try colored output first (modern fastfetch)
if (load_logo_ff_colored(name))
return 1;
// Fall back to --print-logos (older fastfetch, no colors)
return load_logo_ff_plain(name);
}
// Parse a value from os-release, stripping quotes and newlines
static int parse_os_release_val(const char *buf, int prefix_len, char *out,
int maxlen) {
int len = strlen(buf);
char tmp[256];
if (len - prefix_len >= (int)sizeof(tmp))
return 0;
memcpy(tmp, buf + prefix_len, len - prefix_len + 1);
len = strlen(tmp);
while (len > 0 && (tmp[len - 1] == '\n' || tmp[len - 1] == '\r'))
tmp[--len] = '\0';
char *val = tmp;
if (*val == '"')
val++;
len = strlen(val);
if (len > 0 && val[len - 1] == '"')
val[--len] = '\0';
if (len > 0 && len < maxlen) {
memcpy(out, val, len + 1);
return 1;
}
return 0;
}
// Try to detect distro using fastfetch --json first (it's smarter than
// os-release, e.g. it detects Proxmox even though ID=debian).
// Falls back to /etc/os-release if fastfetch isn't available.
static char distro_id_like[64] = "";
static int detect_distro_fastfetch(char *out, int maxlen) {
FILE *fp = popen("fastfetch --json 2>/dev/null", "r");
if (!fp)
return 0;
char buf[1024];
int found_os = 0;
while (fgets(buf, sizeof(buf), fp)) {
// Look for "id": "..." after "type": "OS"
if (strstr(buf, "\"OS\""))
found_os = 1;
if (found_os) {
char *id_pos = strstr(buf, "\"id\"");
if (id_pos) {
// Extract value: "id": "gentoo"
char *colon = strchr(id_pos, ':');
if (colon) {
char *q1 = strchr(colon, '"');
if (q1) {
q1++;
char *q2 = strchr(q1, '"');
if (q2 && q2 - q1 > 0 && q2 - q1 < maxlen) {
memcpy(out, q1, q2 - q1);
out[q2 - q1] = '\0';
pclose(fp);
return 1;
}
}
}
}
// Also grab idLike
char *like_pos = strstr(buf, "\"idLike\"");
if (like_pos) {
char *colon = strchr(like_pos, ':');
if (colon) {
char *q1 = strchr(colon, '"');
if (q1) {
q1++;
char *q2 = strchr(q1, '"');
if (q2 && q2 - q1 > 0 && q2 - q1 < (int)sizeof(distro_id_like)) {
memcpy(distro_id_like, q1, q2 - q1);
distro_id_like[q2 - q1] = '\0';
}
}
}
}
}
}
pclose(fp);
return 0;
}
static int detect_distro_os_release(char *out, int maxlen) {
FILE *fp = fopen("/etc/os-release", "r");
if (!fp)
return 0;
char buf[256];
int found_id = 0;
while (fgets(buf, sizeof(buf), fp)) {
if (!found_id && strncmp(buf, "ID=", 3) == 0) {
found_id = parse_os_release_val(buf, 3, out, maxlen);
} else if (strncmp(buf, "ID_LIKE=", 8) == 0) {
parse_os_release_val(buf, 8, distro_id_like, sizeof(distro_id_like));
}
}
fclose(fp);
return found_id;
}
static int detect_distro(char *out, int maxlen) {
if (detect_distro_fastfetch(out, maxlen))
return 1;
return detect_distro_os_release(out, maxlen);
}
static void load_default_logo(void) {
static const char *gentoo[] = {
" -/oyddmdhs+:. ",
" -odNMMMMMMMMNNmhy+-` ",
" -yNMMMMMMMMMMMNNNmmdhy+- ",
" `omMMMMMMMMMMMMNmdmmmmddhhy/` ",
" omMMMMMMMMMMMNhhyyyohmdddhhhdo` ",
".ydMMMMMMMMMMdhs++so/smdddhhhhdm+`",
" oyhdmNMMMMMMMNdyooydMddddhhhhyhNd.",
" :oyhhdNNMMMMMMMNNMMMdddhhhhhyymMh",
" .:+sydNMMMMMNNMMMMdddhhhhhhmMmy",
" /mMMMMMMNNNMMMdddhhhhhmMNhs:",
" `oNMMMMMMMNNNMMMddddhhdmMNhs+` ",
" `sNMMMMMMMMNNNMMMdddddmNMmhs/. ",
" /NMMMMMMMMNNNNMMMdddmNMNdso:` ",
"+MMMMMMMNNNNNMMMMdMNMNdso/- ",
"yMMNNNNNNNMMMMMNNMmhs+/-` ",
"/hMMNNNNNNNNMNdhs++/-` ",
"`/ohdmmddhys+++/:.` ",
" `-//////:--. ",
};
logo_rows = 18;
for (int i = 0; i < logo_rows; i++) {
int len = strlen(gentoo[i]);
memcpy(logo_data[i], gentoo[i], len + 1);
}
}
#define MAX_POINTS 80000
static float PX[MAX_POINTS], PY[MAX_POINTS], PZ[MAX_POINTS];
static float NX[MAX_POINTS], NY[MAX_POINTS], NZ[MAX_POINTS];
static float PWEIGHT[MAX_POINTS];
static int PCOLOR[MAX_POINTS];
static int POINT_COUNT = 0;
// Fastfetch output storage
#define MAX_FETCH_LINES 32
#define MAX_LINE_LEN 512
static char fetch_lines[MAX_FETCH_LINES][MAX_LINE_LEN];
static int fetch_line_count = 0;
// --- Config ---
enum {
F_OS, F_HOST, F_KERNEL, F_UPTIME, F_PACKAGES, F_SHELL, F_DISPLAY, F_WM,
F_THEME, F_ICONS, F_FONT, F_TERMINAL, F_CPU, F_GPU, F_MEMORY, F_SWAP,
F_DISK, F_IP, F_BATTERY, F_LOCALE, F_COLORS, F_COUNT
};
static int field_enabled[F_COUNT];
static int field_order[F_COUNT];
static int field_line[F_COUNT]; // line index for each field (-1 if not shown)
static int current_field = -1; // which field is currently being gathered
static int field_count = 0;
static int is_refresh_pass = 0; // 1 during the animation refresh tick
static char label_color[16] = "35"; // default magenta
static int config_height = 0; // 0 = auto (match info lines)
static float size_scale = 1.0f;
static float config_speed = 0.0f; // 0 = use flag/default
static int config_spin_x = -1; // -1 = use flag/default
static int config_spin_y = -1;
static char config_shading[128] = "";
static char config_separator[8] = "-";
// Light direction presets
static float light_x = 0.4082f, light_y = 0.8165f, light_z = -0.4082f;
static const struct {
const char *name;
int id;
} field_map[] = {
{"os", F_OS}, {"host", F_HOST},
{"kernel", F_KERNEL}, {"uptime", F_UPTIME},
{"packages", F_PACKAGES}, {"shell", F_SHELL},
{"display", F_DISPLAY}, {"wm", F_WM},
{"theme", F_THEME}, {"icons", F_ICONS},
{"font", F_FONT}, {"terminal", F_TERMINAL},
{"cpu", F_CPU}, {"gpu", F_GPU},
{"memory", F_MEMORY}, {"swap", F_SWAP},
{"disk", F_DISK}, {"ip", F_IP},
{"battery", F_BATTERY}, {"locale", F_LOCALE},
{"colors", F_COLORS}, {NULL, 0}
};
static void config_defaults(void) {
// Default order
int defaults[] = {F_OS, F_HOST, F_KERNEL, F_UPTIME, F_PACKAGES, F_SHELL,
F_DISPLAY, F_WM, F_THEME, F_ICONS, F_FONT, F_TERMINAL,
F_CPU, F_GPU, F_MEMORY, F_SWAP, F_DISK, F_IP, F_BATTERY,
F_LOCALE, F_COLORS};
field_count = sizeof(defaults) / sizeof(defaults[0]);
for (int i = 0; i < field_count; i++) {
field_order[i] = defaults[i];
field_enabled[defaults[i]] = 1;
}
}
static void load_config(void) {
const char *home = getenv("HOME");
if (!home)
return;
char path[512];
snprintf(path, sizeof(path), "%s/.config/fetch/config", home);
FILE *fp = fopen(path, "r");
if (!fp)
return;
// Config file exists — reset defaults, use file order
for (int i = 0; i < F_COUNT; i++)
field_enabled[i] = 0;
field_count = 0;
char buf[256];
while (fgets(buf, sizeof(buf), fp)) {
int len = strlen(buf);
while (len > 0 && (buf[len - 1] == '\n' || buf[len - 1] == '\r' ||
buf[len - 1] == ' '))
buf[--len] = '\0';
// Skip comments and empty lines
char *line = buf;
while (*line == ' ' || *line == '\t')
line++;
if (*line == '#' || *line == '\0')
continue;
// Check for key=value settings
if (strncmp(line, "label_color=", 12) == 0) {
char *val = line + 12;
// Accept color names or numbers
if (strcmp(val, "red") == 0) strcpy(label_color, "31");
else if (strcmp(val, "green") == 0) strcpy(label_color, "32");
else if (strcmp(val, "yellow") == 0) strcpy(label_color, "33");
else if (strcmp(val, "blue") == 0) strcpy(label_color, "34");
else if (strcmp(val, "magenta") == 0) strcpy(label_color, "35");
else if (strcmp(val, "cyan") == 0) strcpy(label_color, "36");
else if (strcmp(val, "white") == 0) strcpy(label_color, "37");
else strncpy(label_color, val, sizeof(label_color) - 1);
continue;
}
if (strncmp(line, "height=", 7) == 0) {
config_height = atoi(line + 7);
if (config_height > MAX_HEIGHT)
config_height = MAX_HEIGHT;
continue;
}
if (strncmp(line, "size=", 5) == 0) {
size_scale = atof(line + 5);
if (size_scale < 0.5f) size_scale = 0.5f;
if (size_scale > 5.0f) size_scale = 5.0f;
continue;
}
if (strncmp(line, "speed=", 6) == 0) {
config_speed = atof(line + 6);
continue;
}
if (strncmp(line, "spin=", 5) == 0) {
char *val = line + 5;
config_spin_x = (strchr(val, 'x') || strchr(val, 'X')) ? 1 : 0;
config_spin_y = (strchr(val, 'y') || strchr(val, 'Y')) ? 1 : 0;
continue;
}
if (strncmp(line, "shading=", 8) == 0) {
strncpy(config_shading, line + 8, sizeof(config_shading) - 1);
continue;
}
if (strncmp(line, "separator=", 10) == 0) {
strncpy(config_separator, line + 10, sizeof(config_separator) - 1);
continue;
}
if (strncmp(line, "light=", 6) == 0) {
char *val = line + 6;
if (strcmp(val, "top-left") == 0) {
light_x = 0.41f; light_y = 0.82f; light_z = -0.41f;
} else if (strcmp(val, "top-right") == 0) {
light_x = -0.41f; light_y = 0.82f; light_z = -0.41f;
} else if (strcmp(val, "top") == 0) {
light_x = 0.0f; light_y = 0.89f; light_z = -0.45f;
} else if (strcmp(val, "left") == 0) {
light_x = 0.82f; light_y = 0.41f; light_z = -0.41f;
} else if (strcmp(val, "right") == 0) {
light_x = -0.82f; light_y = 0.41f; light_z = -0.41f;
} else if (strcmp(val, "front") == 0) {
light_x = 0.0f; light_y = 0.0f; light_z = -1.0f;
} else if (strcmp(val, "bottom-left") == 0) {
light_x = 0.41f; light_y = -0.82f; light_z = -0.41f;
} else if (strcmp(val, "bottom-right") == 0) {
light_x = -0.41f; light_y = -0.82f; light_z = -0.41f;
}
continue;
}
// Match field name
for (int i = 0; field_map[i].name; i++) {
if (strcasecmp(line, field_map[i].name) == 0) {
int id = field_map[i].id;
if (!field_enabled[id] && field_count < F_COUNT) {
field_enabled[id] = 1;
field_order[field_count++] = id;
}
break;
}
}
}
fclose(fp);
}
static void add_line(const char *line) {
if (fetch_line_count >= MAX_FETCH_LINES)
return;
strncpy(fetch_lines[fetch_line_count], line, MAX_LINE_LEN - 1);
fetch_lines[fetch_line_count][MAX_LINE_LEN - 1] = '\0';
fetch_line_count++;
}
// Format a labeled info line using the configured label color.
// If the current field already has a line index, update it in place.
static void add_info(const char *label, const char *fmt, ...) {
char val[MAX_LINE_LEN];
va_list ap;
va_start(ap, fmt);
vsnprintf(val, sizeof(val), fmt, ap);
va_end(ap);
char line[MAX_LINE_LEN];
snprintf(line, sizeof(line), "\033[1;%sm%s\033[0m: %s", label_color, label,
val);
// Refresh tick: replace the field's line in place. Initial pass: always
// append a new line (so gathers that emit multiple rows, like multi-GPU,
// don't overwrite themselves).
if (is_refresh_pass && current_field >= 0 && field_line[current_field] >= 0) {
int idx = field_line[current_field];
strncpy(fetch_lines[idx], line, MAX_LINE_LEN - 1);
fetch_lines[idx][MAX_LINE_LEN - 1] = '\0';
return;
}
if (current_field >= 0)
field_line[current_field] = fetch_line_count;
add_line(line);
}
static void gather_title(void) {
char user[64] = "";
char host[64] = "";
char *login = getlogin();
if (login)
strncpy(user, login, sizeof(user) - 1);
else {
char *env = getenv("USER");
if (env)
strncpy(user, env, sizeof(user) - 1);
}
gethostname(host, sizeof(host));
char line[MAX_LINE_LEN];
snprintf(line, sizeof(line), "\033[1;%sm%s\033[0m@\033[1;%sm%s\033[0m",
label_color, user, label_color, host);
add_line(line);
// separator
int title_len = strlen(user) + 1 + strlen(host);
char sep[MAX_LINE_LEN];
int sep_char_len = strlen(config_separator);
if (sep_char_len == 0) sep_char_len = 1;
int pos = 0;
for (int i = 0; i < title_len && pos + sep_char_len < MAX_LINE_LEN; i++) {
memcpy(sep + pos, config_separator, sep_char_len);
pos += sep_char_len;
}
sep[pos] = '\0';
add_line(sep);
}
static void gather_os(void) {
char pretty[128] = "";
FILE *fp = fopen("/etc/os-release", "r");
if (fp) {
char buf[256];
while (fgets(buf, sizeof(buf), fp)) {
if (strncmp(buf, "PRETTY_NAME=", 12) == 0) {
char *val = buf + 12;
int len = strlen(val);
while (len > 0 && (val[len - 1] == '\n' || val[len - 1] == '\r'))
val[--len] = '\0';
if (*val == '\'' || *val == '"')
val++;
len = strlen(val);
if (len > 0 && (val[len - 1] == '\'' || val[len - 1] == '"'))
val[--len] = '\0';
strncpy(pretty, val, sizeof(pretty) - 1);
break;
}
}
fclose(fp);
}
if (!pretty[0])
strcpy(pretty, "Linux");
struct utsname u;
uname(&u);
add_info("OS", "%s %s", pretty, u.machine);
}
static void gather_host(void) {
char model[128] = "";
// Try device-tree first (ARM/Apple Silicon), then DMI (x86)
FILE *fp = fopen("/proc/device-tree/model", "r");
if (!fp)
fp = fopen("/sys/class/dmi/id/product_name", "r");
if (fp) {
if (fgets(model, sizeof(model), fp)) {
int len = strlen(model);
while (len > 0 && (model[len - 1] == '\n' || model[len - 1] == '\r' ||
model[len - 1] == '\0'))
len--;
model[len] = '\0';
}
fclose(fp);
}
if (model[0])
add_info("Host", "%s", model);
}
static void gather_kernel(void) {
struct utsname u;
uname(&u);
add_info("Kernel", "%s %s", u.sysname, u.release);
}
static void gather_uptime(void) {
FILE *fp = fopen("/proc/uptime", "r");
if (!fp)
return;
double secs = 0;
if (fscanf(fp, "%lf", &secs) != 1)
secs = 0;
fclose(fp);
int total = (int)secs;
int days = total / 86400;
int hours = (total % 86400) / 3600;
int mins = (total % 3600) / 60;
char val[128];
if (days > 0)
snprintf(val, sizeof(val), "%d day%s, %d hour%s, %d min%s", days,
days == 1 ? "" : "s", hours, hours == 1 ? "" : "s", mins,
mins == 1 ? "" : "s");
else if (hours > 0)
snprintf(val, sizeof(val), "%d hour%s, %d min%s", hours,
hours == 1 ? "" : "s", mins, mins == 1 ? "" : "s");
else
snprintf(val, sizeof(val), "%d min%s", mins, mins == 1 ? "" : "s");
add_info("Uptime", "%s", val);
}
static int count_dir_entries(const char *path) {
int count = 0;
FILE *fp = popen(path, "r");
if (!fp)
return 0;
char buf[512];
while (fgets(buf, sizeof(buf), fp))
count++;
pclose(fp);
return count;
}
static void gather_packages(void) {
char val[128] = "";
int n;
// emerge (Gentoo)
n = count_dir_entries("ls -d /var/db/pkg/*/* 2>/dev/null");
if (n > 0) {
snprintf(val, sizeof(val), "%d (emerge)", n);
}
// pacman (Arch)
if (!val[0]) {
n = count_dir_entries(
"ls -d /var/lib/pacman/local/*-* 2>/dev/null");
if (n > 0)
snprintf(val, sizeof(val), "%d (pacman)", n);
}
// dpkg (Debian/Ubuntu)
if (!val[0]) {
n = count_dir_entries("dpkg-query -f '.\n' -W 2>/dev/null");
if (n > 0)
snprintf(val, sizeof(val), "%d (dpkg)", n);
}
// rpm (Fedora/RHEL)
if (!val[0]) {
n = count_dir_entries("rpm -qa 2>/dev/null");
if (n > 0)
snprintf(val, sizeof(val), "%d (rpm)", n);
}
// xbps (Void)
if (!val[0]) {
n = count_dir_entries("xbps-query -l 2>/dev/null");
if (n > 0)
snprintf(val, sizeof(val), "%d (xbps)", n);
}
// apk (Alpine)
if (!val[0]) {
n = count_dir_entries("apk list --installed 2>/dev/null");
if (n > 0)
snprintf(val, sizeof(val), "%d (apk)", n);
}
if (val[0])
add_info("Packages", "%s", val);
}
static void gather_shell(void) {
char *shell = getenv("SHELL");
if (!shell)
return;
// Get just the basename
char *name = strrchr(shell, '/');
name = name ? name + 1 : shell;
// Try to get version
char version[128] = "";
char cmd[256];
snprintf(cmd, sizeof(cmd), "%s --version 2>/dev/null", shell);
FILE *fp = popen(cmd, "r");
if (fp) {
char buf[256];
if (fgets(buf, sizeof(buf), fp)) {
// Extract version number from first line
// e.g. "zsh 5.9.0.3-test (aarch64...)" or "bash 5.2.26(1)-release"
char *p = buf;
// Find the version part after the shell name
char *ver = strstr(buf, name);
if (ver) {
ver += strlen(name);
while (*ver == ' ')
ver++;
} else {
// Try to find first digit
ver = buf;
while (*ver && !(*ver >= '0' && *ver <= '9'))
ver++;
}
if (*ver) {
int len = 0;
while (ver[len] && ver[len] != ' ' && ver[len] != '(' &&
ver[len] != '\n' && len < 30)
len++;
memcpy(version, ver, len);
version[len] = '\0';
}
}
pclose(fp);
}
if (version[0])
add_info("Shell", "%s %s", name, version);
else
add_info("Shell", "%s", name);
}
static void gather_display(void) {
DIR *d = opendir("/sys/class/drm");
if (!d) return;
struct dirent *ent;
int emitted = 0;
while ((ent = readdir(d))) {
// Pattern: cardN-CONNECTOR (skip bare cardN and renderD*)
if (strncmp(ent->d_name, "card", 4) != 0) continue;
const char *dash = strchr(ent->d_name + 4, '-');
if (!dash) continue;
char path[256];
snprintf(path, sizeof(path), "/sys/class/drm/%s/status", ent->d_name);
FILE *fp = fopen(path, "r");