fordon_greeman / sfetch

config & ascii parser Browse files
theidioticdev committed 1d ago parent 1348320 commit 84c134e5588faffcb64411ccd58136551b141836
Showing 6 changed files with 208 additions and 86 deletions. UnifiedSplit
+13 −0 ascii.txt new file mode 100644
@@ -0,0 +1,13 @@
1 +⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣀⣀⣀⠤⣤⣀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
2 +⠀ ⠀⠀⠀⠀⠀⠀⠀⣤⣶⠿⠉⠉⠉⠀⠉⠉⠿⣶⠀⠀⠀⠀⠀⠀⠀⠀⠀
3 +⠀ ⠀⠀⠀⠀⠀⣀⣿⠛⠀⠀⠀⠀⠀⠀⠀⠀⠀⠿⣀⠀⠀⠀⠀⠀⠀⠀⠀
4 +⠀ ⠀⠀⠀⠀⠀⣿⠉⠀⠀⠀⠀⠶⣶⣶⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀
5 +⠀ ⠀⠀⠀⠀⣿⠛⠀⠀⠀⠀⠀⠀⠀⣀⣀⣀⣤⣤⣿⣿⣤⣀⠀⠀⠀⠀⠀
6 +⠀ ⠀⠀⠀⣀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠿⣿⣉⣉⠀⠀⠀⠀⠉⣶⠀⠀⠀⠀
7 +⠀ ⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠛⣿⣿⣤⣤⣤⠛⠀⠀⠀⠀
8 +⠀ ⠀⠀⠀⣿⠀⠀⠀⠀⠀⣀⣶⠶⠶⠿⠉⠉⠿⠿⣶⠀⠀⠀⠀⠀⠀⠀⠀⠀
9 +⠀ ⠀⠀⠀⣿⠀⠀⠀⠀⣤⣿⠉⠀⠀⠀⠀⠀⠀⠀⠛⣿⠀⠀⠀⠀⠀⠀⠀⠀
10 +⠀ ⠀⠀⠀⣿⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⣿⠀⠀⠀⠀⠀⠀⠀
11 +⠀ ⠀⠀⠀⣿⣶⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣤⠛⣤⣤⠀⠀⠀⠀⠀
12 +⠀ ⣶⣿⠛⠛⠀⠛⠶⠶⣿⠶⠶⠶⠶⠶⠶⠶⠶⣛⣛⠛⠀⠀⠛⠿⣶⠀⠀⠀
13 +⠀ ⠉⠛⠒⣶⣤⣤⣶⠒⠛⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⠒⣶⣤⣤⣶⠛
+0 −0 assets/screenshot.png Binary files a/assets/screenshot.png and b/assets/screenshot.png differ
+5 −12 readme.org
@@ -11,19 +11,12 @@ just like suckless.org's projects (dwm, st, surf, etc...), sfetch is configured
11 ** first: 11 ** first:
12 make sure you have the dependencies required: 12 make sure you have the dependencies required:
13 1. a compiler (=gcc=, =clang=) 13 1. a compiler (=gcc=, =clang=)
14 - 2. a c lib (glibc, musl, etc...) 14 + 2. gnumake (make)
15 + 3. a c lib (glibc, musl, etc...)
15 ** second: 16 ** second:
16 #+BEGIN_SRC sh :eval no 17 #+BEGIN_SRC sh :eval no
17 -git clone https://gitbtw.com/fordon_greeman/sfetch 18 +git clone https://codeberg.org/theidioticdev/sfetch
18 cd sfetch 19 cd sfetch
19 -cc -o sfetch sfetch.c 20 +make PREFIX=$HOME/.local install # for your specific user
20 -sudo cp sfetch /usr/local/bin # or cp sfetch ~/.local/bin if u have it in PATH 21 +sudo make install # system-wide
21 -#+END_SRC
22 -
23 -note: if you use gentoo, you can do this instead:
24 -#+BEGIN_SRC sh :eval no
25 - # you must have eselect-repository package
26 - eselect repository add acooliodude-overlay git https://codeberg.org/ACoolioDude/acooliodude-overlay
27 - emerge --sync acooliodude-overlay | emiant sync -r acooliodude-overlay
28 - emerge --ask sfetch
29 #+END_SRC 22 #+END_SRC
+0 −0 sfetch Binary files a/sfetch and b/sfetch differ
+181 −74 sfetch.c
@@ -1,5 +1,9 @@
1 +/* the unsuckless update */
2 +
1 #include <stdio.h> 3 #include <stdio.h>
2 #include <sys/utsname.h> 4 #include <sys/utsname.h>
5 +#include <sys/stat.h>
6 +#include <errno.h>
3 #include <string.h> 7 #include <string.h>
4 #include <libgen.h> 8 #include <libgen.h>
5 #include <unistd.h> 9 #include <unistd.h>
@@ -15,40 +19,115 @@
15 #define C_RED "\033[1;31m" 19 #define C_RED "\033[1;31m"
16 #define C_PURPLE "\033[1;35m" 20 #define C_PURPLE "\033[1;35m"
17 21
18 -#define ENABLE_ASCII 1 22 +const char *version = "0.1.7";
19 -#define ENABLE_QUOTE 1 23 +
24 +typedef struct {
25 + int ascii;
26 + int quote;
27 + int krn_mod;
28 + int os_mod;
29 + int sh_mod;
30 + int wm_mod;
31 + int hn_mod;
32 + int term_mod;
33 + int mem_mod;
34 +} Config;
35 +
36 +Config config = {
37 + .ascii = 1,
38 + .quote = 1,
39 + .os_mod = 1,
40 + .sh_mod = 1,
41 + .krn_mod = 1,
42 + .wm_mod = 1,
43 + .hn_mod = 1,
44 + .term_mod = 1,
45 + .mem_mod = 1,
46 +};
47 +
48 +int
49 +config_parse (const char *fp)
50 +{
51 + FILE *f = fopen (fp, "r");
52 + if (!f)
53 + {
54 + return -1;
55 + }
56 + char line[1024];
20 57
21 -const char *version = "0.1.6"; 58 + while (fgets(line, sizeof(line), f))
59 + {
60 + char key[64], val[512];
61 +
62 + if (sscanf (line, " %63[^ =] = %511s", key, val) == 2)
63 + {
64 + if (strcmp(key, "ascii") == 0)
65 + {
66 + config.ascii = (strcmp(val, "true") == 0);
67 + }
68 + else if (strcmp(key, "quote") == 0)
69 + {
70 + config.quote = (strcmp(val, "true") == 0);
71 + }
72 + else if (strcmp(key, "os_mod") == 0)
73 + {
74 + config.os_mod = (strcmp(val, "true") == 0);
75 + }
76 + else if (strcmp(key, "sh_mod") == 0)
77 + {
78 + config.sh_mod = (strcmp(val, "true") == 0);
79 + }
80 + else if (strcmp(key, "krn_mod") == 0)
81 + {
82 + config.krn_mod = (strcmp(val, "true") == 0);
83 + }
84 + else if (strcmp(key, "wm_mod") == 0)
85 + {
86 + config.wm_mod = (strcmp(val, "true") == 0);
87 + }
88 + else if (strcmp(key, "hn_mod") == 0)
89 + {
90 + config.hn_mod = (strcmp(val, "true") == 0);
91 + }
92 + else if (strcmp(key, "term_mod") == 0)
93 + {
94 + config.term_mod = (strcmp(val, "true") == 0);
95 + }
96 + else if (strcmp(key, "mem_mod") == 0)
97 + {
98 + config.mem_mod = (strcmp(val, "true") == 0);
99 + }
100 + }
101 + }
102 + fclose(f);
103 + return 0;
104 +}
22 105
23 void 106 void
24 print_text (void) 107 print_text (void)
25 { 108 {
26 -#if ENABLE_QUOTE 109 + if (!config.quote)
110 + return;
27 printf ("\n " C_PURPLE "\"the quick fox jumps somewhere, idk.\"\n\n" C_RESET); 111 printf ("\n " C_PURPLE "\"the quick fox jumps somewhere, idk.\"\n\n" C_RESET);
28 -#endif
29 } 112 }
113 +
30 void 114 void
31 -print_ascii (void) 115 +print_ascii (const char *ascii_file)
32 { 116 {
33 -#if ENABLE_ASCII 117 + if (!config.ascii)
34 - 118 + return;
35 - static const char asciiArt[] = 119 + FILE *f = fopen(ascii_file, "r");
36 - "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣀⣀⣀⠤⣤⣀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ \n" 120 + if (!f)
37 - "⠀⠀⠀⠀⠀⠀⠀⠀⣤⣶⠿⠉⠉⠉⠀⠉⠉⠿⣶⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ \n" 121 + {
38 - "⠀⠀⠀⠀⠀⠀⣀⣿⠛⠀⠀⠀⠀⠀⠀⠀⠀⠀⠿⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀ \n" 122 + printf (C_GREEN "No Ascii :(" C_RESET "\n\n");
39 - "⠀⠀⠀⠀⠀⠀⣿⠉⠀⠀⠀⠀⠶⣶⣶⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀ \n" 123 + return;
40 - "⠀⠀⠀⠀⠀⣿⠛⠀⠀⠀⠀⠀⠀⠀⣀⣀⣀⣤⣤⣿⣿⣤⣀⠀⠀⠀⠀⠀⠀ \n" 124 + }
41 - "⠀⠀⠀⠀⣀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠿⣿⣉⣉⠀⠀⠀⠀⠉⣶⠀⠀⠀⠀⠀ \n" 125 + char buf[2048];
42 - "⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠛⣿⣿⣤⣤⣤⠛⠀⠀⠀⠀⠀ \n" 126 + size_t len = fread (buf, 1, sizeof (buf) - 1, f);
43 - "⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⣀⣶⠶⠶⠿⠉⠉⠿⠿⣶⠀⠀⠀⠀⠀⠀⠀⠀⠀ \n" 127 + buf[len] = '\0';
44 - "⠀⠀⠀⠀⣿⠀⠀⠀⠀⣤⣿⠉⠀⠀⠀⠀⠀⠀⠀⠛⣿⠀⠀⠀⠀⠀⠀⠀⠀ \n" 128 + fclose (f);
45 - "⠀⠀⠀⠀⣿⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⣿⠀⠀⠀⠀⠀⠀⠀ \n" 129 +
46 - "⠀⠀⠀⠀⣿⣶⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣤⠛⣤⣤⠀⠀⠀⠀⠀ \n" 130 + printf (C_GREEN "%s" C_RESET "\n\n", buf);
47 - "⠀⣶⣿⠛⠛⠀⠛⠶⠶⣿⠶⠶⠶⠶⠶⠶⠶⠶⣛⣛⠛⠀⠀⠛⠿⣶⠀⠀⠀ \n"
48 - "⠀⠉⠛⠒⣶⣤⣤⣶⠒⠛⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⠒⣶⣤⣤⣶⠛⠀⠀⠀";
49 -
50 - printf (C_GREEN "%s" C_RESET "\n\n", asciiArt);
51 -#endif
52 } 131 }
53 132
54 /* generic way of getting ram, sadly sysinfo sucks ass */ 133 /* generic way of getting ram, sadly sysinfo sucks ass */
@@ -62,12 +141,12 @@ get_mem (unsigned long *total, unsigned long *avail)
62 while (fgets (line, sizeof (line), fp)) 141 while (fgets (line, sizeof (line), fp))
63 { 142 {
64 if (strncmp (line, "MemTotal:", 9) == 0) 143 if (strncmp (line, "MemTotal:", 9) == 0)
65 - sscanf (line + 9, "%lu", total); 144 + sscanf (line + 9, "%lu", total);
66 else if (strncmp (line, "MemAvailable:", 13) == 0) 145 else if (strncmp (line, "MemAvailable:", 13) == 0)
67 - sscanf (line + 13, "%lu", avail); 146 + sscanf (line + 13, "%lu", avail);
68 147
69 if (*total && *avail) 148 if (*total && *avail)
70 - break; 149 + break;
71 } 150 }
72 fclose (fp); 151 fclose (fp);
73 } 152 }
@@ -84,22 +163,22 @@ print_os (void)
84 const size_t klen = sizeof key - 1; 163 const size_t klen = sizeof key - 1;
85 char buf[256]; 164 char buf[256];
86 while (fgets (buf, sizeof buf, fp)) 165 while (fgets (buf, sizeof buf, fp))
87 - { 166 + {
88 - if (strncmp (buf, key, klen) != 0) 167 + if (strncmp (buf, key, klen) != 0)
89 - continue; 168 + continue;
90 - buf[strcspn (buf, "\n")] = '\0'; 169 + buf[strcspn (buf, "\n")] = '\0';
91 - char *val = buf + klen; 170 + char *val = buf + klen;
92 - size_t len = strlen (val); 171 + size_t len = strlen (val);
93 - if (len >= 2 && (val[0] == '"' || val[0] == '\'') && 172 + if (len >= 2 && (val[0] == '"' || val[0] == '\'') &&
94 - val[len - 1] == val[0]) 173 + val[len - 1] == val[0])
95 - { 174 + {
96 - val[len - 1] = '\0'; 175 + val[len - 1] = '\0';
97 - val++; 176 + val++;
98 - } 177 + }
99 - printf (C_CYAN "OS: " C_RESET "%s\n", val); 178 + printf (C_CYAN "OS: " C_RESET "%s\n", val);
100 - fclose (fp); 179 + fclose (fp);
101 - return; 180 + return;
102 - } 181 + }
103 fclose (fp); 182 fclose (fp);
104 } 183 }
105 printf (C_CYAN "OS: " C_RESET "Generic Linux\n"); 184 printf (C_CYAN "OS: " C_RESET "Generic Linux\n");
@@ -117,7 +196,7 @@ print_os (void)
117 int 196 int
118 print_stats (void) 197 print_stats (void)
119 { 198 {
120 - /* preparing the remaining stats */ 199 + /* preparing stats */
121 char *shell = getenv ("SHELL"); 200 char *shell = getenv ("SHELL");
122 201
123 char *term = getenv ("TERM_PROGRAM"); 202 char *term = getenv ("TERM_PROGRAM");
@@ -139,48 +218,76 @@ print_stats (void)
139 return 1; 218 return 1;
140 } 219 }
141 220
142 - /* actual printing */ 221 + /* actual printing guarded by config toggles */
143 - print_os (); 222 + if (config.os_mod)
144 - 223 + {
145 - printf (C_RED "Shell: " C_RESET "%s\n", basename (shell)); 224 + print_os ();
225 + }
146 226
147 - printf (C_ORANGE "Kernel: " C_RESET "%s %s\n", info.sysname, info.release); 227 + if (config.sh_mod && shell)
228 + {
229 + printf (C_RED "Shell: " C_RESET "%s\n", basename (shell));
230 + }
148 231
149 - printf (C_YELLOW "WM: " C_RESET "%s\n", wm ? wm : "unknown/tty" ); 232 + if (config.krn_mod)
233 + {
234 + printf (C_ORANGE "Kernel: " C_RESET "%s %s\n", info.sysname, info.release);
235 + }
150 236
151 - printf (C_GREEN "Host: " C_RESET "%s\n", info.nodename); 237 + if (config.wm_mod)
238 + {
239 + printf (C_YELLOW "WM: " C_RESET "%s\n", wm ? wm : "unknown/tty");
240 + }
152 241
153 - printf (C_BLUE "Term: " C_RESET "%s\n", term ? term : "unknown"); 242 + if (config.hn_mod)
243 + {
244 + printf (C_GREEN "Host: " C_RESET "%s\n", info.nodename);
245 + }
154 246
155 - unsigned long total_kb = 0, avail_kb = 0; 247 + if (config.term_mod)
156 - get_mem (&total_kb, &avail_kb); 248 + {
249 + printf (C_BLUE "Term: " C_RESET "%s\n", term ? term : "unknown");
250 + }
157 251
158 - if (total_kb > 0) 252 + if (config.mem_mod)
159 { 253 {
160 - unsigned long used_mb = (total_kb - avail_kb) / 1024; 254 + unsigned long total_kb = 0, avail_kb = 0;
161 - unsigned long total_mb = total_kb / 1024; 255 + get_mem (&total_kb, &avail_kb);
162 - printf (C_PURPLE "Mem: " C_RESET "%luMiB / %luMiB\n", used_mb, 256 +
163 - total_mb); 257 + if (total_kb > 0)
258 + {
259 + unsigned long used_mb = (total_kb - avail_kb) / 1024;
260 + unsigned long total_mb = total_kb / 1024;
261 + printf (C_PURPLE "Mem: " C_RESET "%luMiB / %luMiB\n", used_mb, total_mb);
262 + }
164 } 263 }
165 264
166 return 0; 265 return 0;
167 } 266 }
168 267
169 int 268 int
170 -main (int argc, char **argv) 269 +main()
171 { 270 {
172 - for (int i = 1; i < argc; i++) 271 + char *xdg = getenv("XDG_CONFIG_HOME");
173 - { 272 + char config_dir[256];
174 - if (strcmp (argv[i], "-v") == 0 || strcmp (argv[i], "--version") == 0) 273 +
175 - { 274 + if (xdg == NULL || xdg[0] == '\0') {
176 - printf ("sfetch-%s\n", version); 275 + snprintf(config_dir, sizeof(config_dir), "%s/.config", getenv("HOME"));
177 - return 0; 276 + } else {
178 - } 277 + snprintf(config_dir, sizeof(config_dir), "%s", xdg);
179 - fprintf (stderr, "Unknown argument: %s\nUsage: %s [-v]\n", argv[i], argv[0]); 278 + }
180 - return 1; 279 +
181 - } 280 + char file_path[512];
182 - print_text (); 281 + snprintf(file_path, sizeof(file_path), "%s/sfetch/sfetch.conf", config_dir);
183 - print_ascii (); 282 +
184 - print_stats (); 283 + config_parse(file_path);
284 +
285 + print_text();
286 +
287 + snprintf(file_path, sizeof(file_path), "%s/sfetch/ascii.txt", config_dir);
288 + print_ascii(file_path);
289 +
290 + print_stats();
291 +
185 return 0; 292 return 0;
186 } 293 }
+9 −0 sfetch.conf new file mode 100644
@@ -0,0 +1,9 @@
1 +ascii = true
2 +quote = false
3 +os_mod = true
4 +sh_mod = true
5 +krn_mod = true
6 +wm_mod = true
7 +hn_mod = true
8 +term_mod = true
9 +mem_mod = true