fordon_greeman / sfetch

feat: new penguin, quotes, mem fetching, update readme Browse files
theidioticdev committed 4d ago parent bfb63c6 commit 5e2bd5d50312f21a0431fdb8f4f2219d6cd32def
Showing 5 changed files with 146 additions and 169 deletions. UnifiedSplit
+0 −96 bs.c deleted file mode 100644
@@ -1,96 +0,0 @@
1 -#include <stdio.h>
2 -#include <unistd.h>
3 -#include <sys/wait.h>
4 -#include <stdlib.h>
5 -#include <string.h>
6 -
7 -#define CC "cc"
8 -#define PROG_NAME "sfetch"
9 -#define DEST "/usr/local/bin"
10 -
11 -int
12 -runCmd (char *const argv[])
13 -{
14 - pid_t pid = fork();
15 - if (pid == 0)
16 - {
17 - execvp(argv[0], argv);
18 - perror("execvp failed");
19 - _exit(1);
20 - } else if (pid > 0) {
21 - int status;
22 - waitpid(pid, &status, 0);
23 - return WIFEXITED(status) && WEXITSTATUS(status) == 0;
24 - }
25 - return 0;
26 -}
27 -
28 -int
29 -build (void)
30 -{
31 - char *const buildCmd[] = {
32 - CC, "-O3", "sfetch.c", "-o", PROG_NAME, NULL
33 - };
34 -
35 - printf ("[BUILD]: " CC " -O3 sfetch.c -o %s\n", PROG_NAME);
36 - if (!runCmd (buildCmd))
37 - {
38 - fprintf (stderr, "[ERROR]: compilation failed\n");
39 - return 0;
40 - }
41 - printf ("[SUCCESS]: compiled successfully\n");
42 - return 1;
43 -}
44 -
45 -int
46 -install (void)
47 -{
48 - if (!build ())
49 - return 0;
50 -
51 - char *const installCmd[] = {
52 - "cp", PROG_NAME, DEST "/" PROG_NAME, NULL
53 - };
54 -
55 - printf ("[INSTALL]: cp %s %s/%s\n", PROG_NAME, DEST, PROG_NAME);
56 - if (!runCmd (installCmd))
57 - {
58 - fprintf (stderr, "[ERROR]: install failed\n");
59 - return 0;
60 - }
61 - printf ("[SUCCESS]: installed successfully\n");
62 - return 1;
63 -}
64 -
65 -int
66 -uninstall (void)
67 -{
68 - if (remove(DEST "/" PROG_NAME) != 0)
69 -{
70 - fprintf(stderr, "[ERROR]: uninstall failed.\n");
71 - return 0;
72 - }
73 - printf("[SUCCESS]: uninstalled successfully\n");
74 - return 1;
75 -}
76 -
77 -int
78 -main (int argc, char **argv)
79 -{
80 - if (argc < 2 || strcmp(argv[1], "--build-only") == 0)
81 - {
82 - return build() ? 0 : 1;
83 - }
84 -
85 - if (strcmp(argv[1], "--install") == 0)
86 - {
87 - return install() ? 0 : 1;
88 - }
89 -
90 - if (strcmp(argv[1], "--uninstall") == 0)
91 - {
92 - return uninstall() ? 0 : 1;
93 - }
94 - fprintf(stderr, "Unknown argument: %s\n", argv[1]);
95 - return 1;
96 -}
+0 −0 build deleted file mode 100755, Binary files a/build and /dev/null differ
+3 −5 readme.org
@@ -14,12 +14,10 @@ just like suckless.org's projects (dwm, st, surf, etc...), sfetch is configured
14 2. a c lib (glibc, musl, etc...) 14 2. a c lib (glibc, musl, etc...)
15 ** second: 15 ** second:
16 #+BEGIN_SRC sh :eval no 16 #+BEGIN_SRC sh :eval no
17 -git clone https://codeberg.org/theidioticdev/sfetch 17 +git clone https://gitbtw.com/fordon_greeman/sfetch
18 cd sfetch 18 cd sfetch
19 -gcc bs.c -o build 19 +cc -o sfetch sfetch.c
20 -sudo/doas ./build --install # to install system-wide, tweak bs.c and recompile it if u want to install to .local/bin 20 +sudo cp sfetch /usr/local/bin # or cp sfetch ~/.local/bin if u have it in PATH
21 -./build # to build only
22 -sudo/doas ./build --uninstall # uninstall it
23 #+END_SRC 21 #+END_SRC
24 22
25 note: if you use gentoo, you can do this instead: 23 note: if you use gentoo, you can do this instead:
+0 −0 sfetch new file mode 100755, Binary files /dev/null and b/sfetch differ
+143 −68 sfetch.c
@@ -1,11 +1,13 @@
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <sys/utsname.h> 2 #include <sys/utsname.h>
3 #include <string.h> 3 #include <string.h>
4 +#include <libgen.h>
4 #include <unistd.h> 5 #include <unistd.h>
5 #include <stdlib.h> 6 #include <stdlib.h>
6 7
7 #define C_RESET "\033[0m" 8 #define C_RESET "\033[0m"
8 #define C_BOLD "\033[1m" 9 #define C_BOLD "\033[1m"
10 +#define C_ORANGE "\033[38;5;208m"
9 #define C_BLUE "\033[1;34m" 11 #define C_BLUE "\033[1;34m"
10 #define C_YELLOW "\033[1;33m" 12 #define C_YELLOW "\033[1;33m"
11 #define C_CYAN "\033[1;36m" 13 #define C_CYAN "\033[1;36m"
@@ -14,98 +16,171 @@
14 #define C_PURPLE "\033[1;35m" 16 #define C_PURPLE "\033[1;35m"
15 17
16 #define ENABLE_ASCII 1 18 #define ENABLE_ASCII 1
19 +#define ENABLE_QUOTE 1
17 20
18 -const char *version = "0.1.5"; 21 +const char *version = "0.1.6";
19 22
20 void 23 void
21 -printAscii(void) 24 +print_text (void)
25 +{
26 +#if ENABLE_QUOTE
27 + printf ("\n " C_PURPLE "\"the quick fox jumps somewhere, idk.\"\n\n" C_RESET);
28 +#endif
29 +}
30 +void
31 +print_ascii (void)
22 { 32 {
23 #if ENABLE_ASCII 33 #if ENABLE_ASCII
24 - static const char asciiArt[] = 34 +
25 - " ___ \n" 35 + static const char asciiArt[] =
26 - " (.. | \n" 36 + "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣀⣀⣀⠤⣤⣀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ \n"
27 - " (<> | \n" 37 + "⠀⠀⠀⠀⠀⠀⠀⠀⣤⣶⠿⠉⠉⠉⠀⠉⠉⠿⣶⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ \n"
28 - " / __ \\ \n" 38 + "⠀⠀⠀⠀⠀⠀⣀⣿⠛⠀⠀⠀⠀⠀⠀⠀⠀⠀⠿⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀ \n"
29 - " ( / \\ /| \n" 39 + "⠀⠀⠀⠀⠀⠀⣿⠉⠀⠀⠀⠀⠶⣶⣶⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀ \n"
30 - " _/\\ __)/_) \n" 40 + "⠀⠀⠀⠀⠀⣿⠛⠀⠀⠀⠀⠀⠀⠀⣀⣀⣀⣤⣤⣿⣿⣤⣀⠀⠀⠀⠀⠀⠀ \n"
31 - " \\/-____\\/ "; 41 + "⠀⠀⠀⠀⣀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠿⣿⣉⣉⠀⠀⠀⠀⠉⣶⠀⠀⠀⠀⠀ \n"
32 - printf(C_YELLOW "%s" C_RESET "\n\n", asciiArt); 42 + "⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠛⣿⣿⣤⣤⣤⠛⠀⠀⠀⠀⠀ \n"
43 + "⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⣀⣶⠶⠶⠿⠉⠉⠿⠿⣶⠀⠀⠀⠀⠀⠀⠀⠀⠀ \n"
44 + "⠀⠀⠀⠀⣿⠀⠀⠀⠀⣤⣿⠉⠀⠀⠀⠀⠀⠀⠀⠛⣿⠀⠀⠀⠀⠀⠀⠀⠀ \n"
45 + "⠀⠀⠀⠀⣿⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⣿⠀⠀⠀⠀⠀⠀⠀ \n"
46 + "⠀⠀⠀⠀⣿⣶⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣤⠛⣤⣤⠀⠀⠀⠀⠀ \n"
47 + "⠀⣶⣿⠛⠛⠀⠛⠶⠶⣿⠶⠶⠶⠶⠶⠶⠶⠶⣛⣛⠛⠀⠀⠛⠿⣶⠀⠀⠀ \n"
48 + "⠀⠉⠛⠒⣶⣤⣤⣶⠒⠛⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⠒⣶⣤⣤⣶⠛⠀⠀⠀";
49 +
50 + printf (C_GREEN "%s" C_RESET "\n\n", asciiArt);
33 #endif 51 #endif
34 } 52 }
53 +
54 +/* generic way of getting ram, sadly sysinfo sucks ass */
55 +void
56 +get_mem (unsigned long *total, unsigned long *avail)
57 +{
58 + FILE *fp = fopen ("/proc/meminfo", "r");
59 + if (!fp)
60 + return;
61 + char line[255];
62 + while (fgets (line, sizeof (line), fp))
63 + {
64 + if (strncmp (line, "MemTotal:", 9) == 0)
65 + sscanf (line + 9, "%lu", total);
66 + else if (strncmp (line, "MemAvailable:", 13) == 0)
67 + sscanf (line + 13, "%lu", avail);
68 +
69 + if (*total && *avail)
70 + break;
71 + }
72 + fclose (fp);
73 +}
74 +
75 +/* ty "bugs in nixmania" on tonybtw discord */
35 void 76 void
36 -printOS(void) 77 +print_os (void)
37 { 78 {
38 #ifdef __linux__ 79 #ifdef __linux__
39 - FILE *fp = fopen("/etc/os-release", "r"); 80 + FILE *fp = fopen ("/etc/os-release", "r");
40 - if (!fp) 81 + if (fp)
82 + {
83 + static const char key[] = "PRETTY_NAME=";
84 + const size_t klen = sizeof key - 1;
85 + char buf[256];
86 + while (fgets (buf, sizeof buf, fp))
41 { 87 {
42 - printf(C_CYAN "OS: " C_RESET "Generic Linux\n"); 88 + if (strncmp (buf, key, klen) != 0)
43 - return; 89 + continue;
44 - } 90 + buf[strcspn (buf, "\n")] = '\0';
45 - char line[512]; 91 + char *val = buf + klen;
46 - while (fgets(line, sizeof(line), fp)) 92 + size_t len = strlen (val);
47 - { 93 + if (len >= 2 && (val[0] == '"' || val[0] == '\'') &&
48 - if(strncmp(line, "PRETTY_NAME=", 12) == 0) 94 + val[len - 1] == val[0])
49 - { 95 + {
50 - char *name = line + 12; 96 + val[len - 1] = '\0';
51 - if (*name == '"') name++; 97 + val++;
52 - size_t len = strlen(name); 98 + }
53 - if (len > 0 && name[len - 1] == '\n') name[--len] = '\0'; 99 + printf (C_CYAN "OS: " C_RESET "%s\n", val);
54 - if (len > 0 && name[len - 1] == '"') name[--len] = '\0'; 100 + fclose (fp);
55 - printf(C_CYAN "OS: " C_RESET "%s\n", name); 101 + return;
56 - fclose(fp); 102 + }
57 - return; 103 + fclose (fp);
58 } 104 }
59 - } 105 + printf (C_CYAN "OS: " C_RESET "Generic Linux\n");
60 - fclose(fp);
61 - printf(C_CYAN "OS: " C_RESET "Generic Linux\n");
62 #else 106 #else
63 - struct utsname otherOS; 107 + struct utsname other_os;
64 - if (uname(&otherOS) != 0) 108 + if (uname (&other_os) != 0)
65 - { 109 + {
66 - perror("uname"); 110 + perror ("uname");
67 - return; 111 + return;
68 - } 112 + }
69 - printf(C_CYAN "OS: " C_RESET "%s\n", otherOS.sysname); 113 + printf (C_CYAN "OS: " C_RESET "%s\n", other_os.sysname);
70 #endif 114 #endif
71 } 115 }
72 116
73 -int 117 +int
74 -printStats(void) 118 +print_stats (void)
75 { 119 {
76 - char *wm = getenv("XDG_CURRENT_DESKTOP"); 120 + /* preparing the remaining stats */
77 - if (!wm) 121 + char *shell = getenv ("SHELL");
78 - { 122 +
79 - wm = getenv("DESKTOP_SESSION"); 123 + char *term = getenv ("TERM_PROGRAM");
80 - } 124 + if (!term)
81 - char *session = getenv("XDG_SESSION_TYPE"); 125 + {
126 + term = getenv ("TERM");
127 + }
128 +
129 + char *wm = getenv ("XDG_CURRENT_DESKTOP");
130 + if (!wm)
131 + {
132 + wm = getenv ("DESKTOP_SESSION");
133 + }
134 +
82 struct utsname info; 135 struct utsname info;
83 - if (uname(&info) != 0) 136 + if (uname (&info) != 0)
84 - { 137 + {
85 - perror("uname"); 138 + perror ("uname");
86 return 1; 139 return 1;
87 - } 140 + }
88 - printOS(); 141 +
89 - printf(C_GREEN "Kernel: " C_RESET "%s %s\n",info.sysname, info.release); 142 + /* actual printing */
90 - printf(C_BLUE "Host: " C_RESET "%s\n", info.nodename); 143 + print_os ();
91 - printf(C_RED "WM: " C_RESET "%s (%s)\n", wm ? wm : "unknown / tty", session ? session : "unknown"); 144 +
145 + printf (C_RED "Shell: " C_RESET "%s\n", basename (shell));
146 +
147 + printf (C_ORANGE "Kernel: " C_RESET "%s %s\n", info.sysname, info.release);
148 +
149 + printf (C_YELLOW "WM: " C_RESET "%s\n", wm ? wm : "unknown/tty" );
150 +
151 + printf (C_GREEN "Host: " C_RESET "%s\n", info.nodename);
152 +
153 + printf (C_BLUE "Term: " C_RESET "%s\n", term ? term : "unknown");
154 +
155 + unsigned long total_kb = 0, avail_kb = 0;
156 + get_mem (&total_kb, &avail_kb);
157 +
158 + if (total_kb > 0)
159 + {
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);
164 + }
165 +
92 return 0; 166 return 0;
93 } 167 }
94 168
95 -int 169 +int
96 -main(int argc, char **argv) 170 +main (int argc, char **argv)
97 { 171 {
98 for (int i = 1; i < argc; i++) 172 for (int i = 1; i < argc; i++)
99 - { 173 + {
100 - if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) 174 + if (strcmp (argv[i], "-v") == 0 || strcmp (argv[i], "--version") == 0)
101 - { 175 + {
102 - printf("sfetch-%s\n", version); 176 + printf ("sfetch-%s\n", version);
103 - return 0; 177 + return 0;
178 + }
179 + fprintf (stderr, "Unknown argument: %s\nUsage: %s [-v]\n", argv[i], argv[0]);
180 + return 1;
104 } 181 }
105 - fprintf(stderr, "unknown argument: %s\nusage: %s [-v]\n", argv[i], argv[0]); 182 + print_text ();
106 - return 1; 183 + print_ascii ();
107 - } 184 + print_stats ();
108 - printAscii();
109 - printStats();
110 return 0; 185 return 0;
111 } 186 }