fordon_greeman / sfetch

Branch: d5133e6
Switch branches/tags
main
theidioticdev feat: colored text, first argument -v and new ascii d5133e6 2026-09-12
82 lines · 2.5 KB Raw Blame History
1 #include <stdio.h>
2 #include <sys/utsname.h>
3 #include <string.h>
4 #include <stdlib.h>
5
6 #define C_RESET "\033[0m"
7 #define C_BOLD "\033[1m"
8 #define C_BLUE "\033[1;34m"
9 #define C_YELLOW "\033[1;33m"
10 #define C_CYAN "\033[1;36m"
11 #define C_GREEN "\033[1;32m"
12 #define C_RED "\033[1;31m"
13 #define C_BLACK "\033[1;30m"
14
15 static char *version = "0.1.3";
16
17 static const char asciiArt[] =
18 " ████████ \n"
19 " ██████████ \n"
20 " █████████ \n"
21 " ███████ \n"
22 " ██████ ██ ██ ██ ██ ██ ██ ██ \n"
23 " ███████ \n"
24 " █████████ \n"
25 " ██████████ \n"
26 " ████████ ";
27
28 void printAscii(void) {
29 printf(C_YELLOW "%s" C_RESET "\n\n", asciiArt);
30 }
31 void printOS(void) {
32 FILE *fp = fopen("/etc/os-release", "r");
33 if (!fp) {
34 printf(C_CYAN "OS: " C_RESET "Generic Linux\n");
35 return;
36 }
37 char line[512];
38 while (fgets(line, sizeof(line), fp)) {
39 if(strncmp(line, "PRETTY_NAME=", 12) == 0) {
40 char *name = line + 12;
41 if (*name == '"') name++;
42 size_t len = strlen(name);
43 if (len > 0 && name[len - 1] == '\n') name[--len] = '\0';
44 if (len > 0 && name[len - 1] == '"') name[--len] = '\0';
45 printf(C_CYAN "OS: " C_RESET "%s\n", name);
46 fclose(fp);
47 return;
48 }
49 }
50 fclose(fp);
51 printf(C_CYAN "OS: " C_RESET "Generic Linux\n");
52 }
53 int printStats(void) {
54 char *term = getenv("TERM_PROGRAM");
55 if (!term) {
56 term = getenv("TERM");
57 }
58 char *wm = getenv("XDG_SESSION_DESKTOP");
59 if (!wm) {
60 wm = getenv("DESKTOP_SESSION");
61 }
62 struct utsname info;
63 if (uname(&info) != 0) {
64 perror("uname");
65 return 1;
66 }
67 printOS();
68 printf(C_GREEN "Kernel: " C_RESET "%s\n", info.release);
69 printf(C_BLUE "Host: " C_RESET "%s\n", info.nodename);
70 printf(C_RED "WM: " C_RESET "%s\n", wm ? wm : "Unknown / TTY");
71 printf(C_BLACK "Term: " C_RESET "%s\n", term ? term : "couldn't read terminal name");
72 return 0;
73 }
74 int main(int argc, char **argv) {
75 if (argc > 1 && strcmp("-v", argv[1]) == 0) {
76 printf("sfetch v%s\n", version);
77 return 0;
78 }
79 printAscii();
80 printStats();
81 return 0;
82 }