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 11 ** first:
12 12 make sure you have the dependencies required:
13 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 16 ** second:
16 17 #+BEGIN_SRC sh :eval no
17 -git clone https://gitbtw.com/fordon_greeman/sfetch
18 +git clone https://codeberg.org/theidioticdev/sfetch
18 19 cd sfetch
19 -cc -o sfetch sfetch.c
20 -sudo cp sfetch /usr/local/bin # or cp sfetch ~/.local/bin if u have it in PATH
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
20 +make PREFIX=$HOME/.local install # for your specific user
21 +sudo make install # system-wide
29 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 3 #include <stdio.h>
2 4 #include <sys/utsname.h>
5 +#include <sys/stat.h>
6 +#include <errno.h>
3 7 #include <string.h>
4 8 #include <libgen.h>
5 9 #include <unistd.h>
@@ -15,40 +19,115 @@
15 19 #define C_RED "\033[1;31m"
16 20 #define C_PURPLE "\033[1;35m"
17 21
18 -#define ENABLE_ASCII 1
19 -#define ENABLE_QUOTE 1
22 +const char *version = "0.1.7";
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 106 void
24 107 print_text (void)
25 108 {
26 -#if ENABLE_QUOTE
109 + if (!config.quote)
110 + return;
27 111 printf ("\n " C_PURPLE "\"the quick fox jumps somewhere, idk.\"\n\n" C_RESET);
28 -#endif
29 112 }
113 +
30 114 void
31 -print_ascii (void)
115 +print_ascii (const char *ascii_file)
32 116 {
33 -#if ENABLE_ASCII
34 -
35 - static const char asciiArt[] =
36 - "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣀⣀⣀⠤⣤⣀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ \n"
37 - "⠀⠀⠀⠀⠀⠀⠀⠀⣤⣶⠿⠉⠉⠉⠀⠉⠉⠿⣶⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ \n"
38 - "⠀⠀⠀⠀⠀⠀⣀⣿⠛⠀⠀⠀⠀⠀⠀⠀⠀⠀⠿⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀ \n"
39 - "⠀⠀⠀⠀⠀⠀⣿⠉⠀⠀⠀⠀⠶⣶⣶⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀ \n"
40 - "⠀⠀⠀⠀⠀⣿⠛⠀⠀⠀⠀⠀⠀⠀⣀⣀⣀⣤⣤⣿⣿⣤⣀⠀⠀⠀⠀⠀⠀ \n"
41 - "⠀⠀⠀⠀⣀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠿⣿⣉⣉⠀⠀⠀⠀⠉⣶⠀⠀⠀⠀⠀ \n"
42 - "⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠛⣿⣿⣤⣤⣤⠛⠀⠀⠀⠀⠀ \n"
43 - "⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⣀⣶⠶⠶⠿⠉⠉⠿⠿⣶⠀⠀⠀⠀⠀⠀⠀⠀⠀ \n"
44 - "⠀⠀⠀⠀⣿⠀⠀⠀⠀⣤⣿⠉⠀⠀⠀⠀⠀⠀⠀⠛⣿⠀⠀⠀⠀⠀⠀⠀⠀ \n"
45 - "⠀⠀⠀⠀⣿⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⣿⠀⠀⠀⠀⠀⠀⠀ \n"
46 - "⠀⠀⠀⠀⣿⣶⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣤⠛⣤⣤⠀⠀⠀⠀⠀ \n"
47 - "⠀⣶⣿⠛⠛⠀⠛⠶⠶⣿⠶⠶⠶⠶⠶⠶⠶⠶⣛⣛⠛⠀⠀⠛⠿⣶⠀⠀⠀ \n"
48 - "⠀⠉⠛⠒⣶⣤⣤⣶⠒⠛⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⠒⣶⣤⣤⣶⠛⠀⠀⠀";
49 -
50 - printf (C_GREEN "%s" C_RESET "\n\n", asciiArt);
51 -#endif
117 + if (!config.ascii)
118 + return;
119 + FILE *f = fopen(ascii_file, "r");
120 + if (!f)
121 + {
122 + printf (C_GREEN "No Ascii :(" C_RESET "\n\n");
123 + return;
124 + }
125 + char buf[2048];
126 + size_t len = fread (buf, 1, sizeof (buf) - 1, f);
127 + buf[len] = '\0';
128 + fclose (f);
129 +
130 + printf (C_GREEN "%s" C_RESET "\n\n", buf);
52 131 }
53 132
54 133 /* generic way of getting ram, sadly sysinfo sucks ass */
@@ -62,12 +141,12 @@ get_mem (unsigned long *total, unsigned long *avail)
62 141 while (fgets (line, sizeof (line), fp))
63 142 {
64 143 if (strncmp (line, "MemTotal:", 9) == 0)
65 - sscanf (line + 9, "%lu", total);
144 + sscanf (line + 9, "%lu", total);
66 145 else if (strncmp (line, "MemAvailable:", 13) == 0)
67 - sscanf (line + 13, "%lu", avail);
146 + sscanf (line + 13, "%lu", avail);
68 147
69 148 if (*total && *avail)
70 - break;
149 + break;
71 150 }
72 151 fclose (fp);
73 152 }
@@ -84,22 +163,22 @@ print_os (void)
84 163 const size_t klen = sizeof key - 1;
85 164 char buf[256];
86 165 while (fgets (buf, sizeof buf, fp))
87 - {
88 - if (strncmp (buf, key, klen) != 0)
89 - continue;
90 - buf[strcspn (buf, "\n")] = '\0';
91 - char *val = buf + klen;
92 - size_t len = strlen (val);
93 - if (len >= 2 && (val[0] == '"' || val[0] == '\'') &&
94 - val[len - 1] == val[0])
95 - {
96 - val[len - 1] = '\0';
97 - val++;
98 - }
99 - printf (C_CYAN "OS: " C_RESET "%s\n", val);
100 - fclose (fp);
101 - return;
102 - }
166 + {
167 + if (strncmp (buf, key, klen) != 0)
168 + continue;
169 + buf[strcspn (buf, "\n")] = '\0';
170 + char *val = buf + klen;
171 + size_t len = strlen (val);
172 + if (len >= 2 && (val[0] == '"' || val[0] == '\'') &&
173 + val[len - 1] == val[0])
174 + {
175 + val[len - 1] = '\0';
176 + val++;
177 + }
178 + printf (C_CYAN "OS: " C_RESET "%s\n", val);
179 + fclose (fp);
180 + return;
181 + }
103 182 fclose (fp);
104 183 }
105 184 printf (C_CYAN "OS: " C_RESET "Generic Linux\n");
@@ -117,7 +196,7 @@ print_os (void)
117 196 int
118 197 print_stats (void)
119 198 {
120 - /* preparing the remaining stats */
199 + /* preparing stats */
121 200 char *shell = getenv ("SHELL");
122 201
123 202 char *term = getenv ("TERM_PROGRAM");
@@ -139,48 +218,76 @@ print_stats (void)
139 218 return 1;
140 219 }
141 220
142 - /* actual printing */
143 - print_os ();
144 -
145 - printf (C_RED "Shell: " C_RESET "%s\n", basename (shell));
221 + /* actual printing guarded by config toggles */
222 + if (config.os_mod)
223 + {
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;
156 - get_mem (&total_kb, &avail_kb);
247 + if (config.term_mod)
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;
161 - unsigned long total_mb = total_kb / 1024;
162 - printf (C_PURPLE "Mem: " C_RESET "%luMiB / %luMiB\n", used_mb,
163 - total_mb);
254 + unsigned long total_kb = 0, avail_kb = 0;
255 + get_mem (&total_kb, &avail_kb);
256 +
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 265 return 0;
167 266 }
168 267
169 268 int
170 -main (int argc, char **argv)
269 +main()
171 270 {
172 - for (int i = 1; i < argc; i++)
173 - {
174 - if (strcmp (argv[i], "-v") == 0 || strcmp (argv[i], "--version") == 0)
175 - {
176 - printf ("sfetch-%s\n", version);
177 - return 0;
178 - }
179 - fprintf (stderr, "Unknown argument: %s\nUsage: %s [-v]\n", argv[i], argv[0]);
180 - return 1;
181 - }
182 - print_text ();
183 - print_ascii ();
184 - print_stats ();
271 + char *xdg = getenv("XDG_CONFIG_HOME");
272 + char config_dir[256];
273 +
274 + if (xdg == NULL || xdg[0] == '\0') {
275 + snprintf(config_dir, sizeof(config_dir), "%s/.config", getenv("HOME"));
276 + } else {
277 + snprintf(config_dir, sizeof(config_dir), "%s", xdg);
278 + }
279 +
280 + char file_path[512];
281 + snprintf(file_path, sizeof(file_path), "%s/sfetch/sfetch.conf", config_dir);
282 +
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 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