fordon_greeman / sfetch

Branch: d872459
Switch branches/tags
main
theidioticdev feat: toggle ascii d872459 2026-09-18
104 lines · 3.2 KB Raw Blame History
1 #include <stdio.h>
2 #include <sys/utsname.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6
7 #define C_RESET "\033[0m"
8 #define C_BOLD "\033[1m"
9 #define C_BLUE "\033[1;34m"
10 #define C_YELLOW "\033[1;33m"
11 #define C_CYAN "\033[1;36m"
12 #define C_GREEN "\033[1;32m"
13 #define C_RED "\033[1;31m"
14 #define C_PURPLE "\033[1;35m"
15
16 #define ENABLE_ASCII 1
17
18 const char *version = "0.1.5";
19
20 void printAscii(void) {
21 #if ENABLE_ASCII
22 static const char asciiArt[] =
23 " ████████ \n"
24 " ██████████ \n"
25 " █████████ \n"
26 " ███████ \n"
27 " ██████ ██ ██ ██ ██ ██ ██ ██ \n"
28 " ███████ \n"
29 " █████████ \n"
30 " ██████████ \n"
31 " ████████ ";
32 printf(C_YELLOW "%s" C_RESET "\n\n", asciiArt);
33 #endif
34 }
35 void printOS(void) {
36 #ifdef __linux__
37 FILE *fp = fopen("/etc/os-release", "r");
38 if (!fp) {
39 printf(C_CYAN "OS: " C_RESET "Generic Linux\n");
40 return;
41 }
42 char line[512];
43 while (fgets(line, sizeof(line), fp)) {
44 if(strncmp(line, "PRETTY_NAME=", 12) == 0) {
45 char *name = line + 12;
46 if (*name == '"') name++;
47 size_t len = strlen(name);
48 if (len > 0 && name[len - 1] == '\n') name[--len] = '\0';
49 if (len > 0 && name[len - 1] == '"') name[--len] = '\0';
50 printf(C_CYAN "OS: " C_RESET "%s\n", name);
51 fclose(fp);
52 return;
53 }
54 }
55 fclose(fp);
56 printf(C_CYAN "OS: " C_RESET "Generic Linux\n");
57 #else
58 struct utsname otherOS;
59 if (uname(&otherOS) != 0) {
60 perror("uname");
61 return;
62 }
63 printf(C_CYAN "OS: " C_RESET "%s\n", otherOS.sysname);
64 #endif
65 }
66 int printStats(void) {
67 char *term = getenv("TERM_PROGRAM");
68 if (!term) {
69 term = getenv("TERM");
70 }
71 char *wm = getenv("XDG_SESSION_DESKTOP");
72 if (!wm) {
73 wm = getenv("DESKTOP_SESSION");
74 }
75 char *session = getenv("XDG_SESSION_TYPE");
76 struct utsname info;
77 if (uname(&info) != 0) {
78 perror("uname");
79 return 1;
80 }
81 printOS();
82 printf(C_GREEN "Kernel: " C_RESET "%s %s\n",info.sysname, info.release);
83 printf(C_BLUE "Host: " C_RESET "%s\n", info.nodename);
84 printf(C_RED "WM: " C_RESET "%s (%s)\n", wm ? wm : "unknown / tty", session ? session : "unknown");
85 printf(C_PURPLE "Term: " C_RESET "%s\n", term ? term : "unknown");
86 return 0;
87 }
88 int main(int argc, char **argv) {
89 for (int i = 1; i < argc; i++) {
90 if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
91 printf("usage: %s [-v/-h]\n", argv[0]);
92 return 0;
93 }
94 if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
95 printf("sfetch-%s\n", version);
96 return 0;
97 }
98 fprintf(stderr, "Unknown argument: %s\nUsage: %s [-v/-h]\n", argv[i], argv[0]);
99 return 1;
100 }
101 printAscii();
102 printStats();
103 return 0;
104 }