paleofetch

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 6ea3250b6e62b49afe17877bf68016d51b4c1967
parent 011d98ef0e08abb5e983733595280db411ee600f
Author: dwzg <dennis@wtzg.de>
Date:   Tue, 28 Apr 2020 22:25:06 +0200

Merge remote-tracking branch 'upstream/master' into replace_substring

Diffstat:
MMakefile | 5+++--
Mconfig.h | 38+++++++++++++++++++-------------------
Alogos/arch3.h | 22++++++++++++++++++++++
Mpaleofetch.c | 109++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
Mpaleofetch.h | 36+++++++++++++++++++-----------------
5 files changed, 135 insertions(+), 75 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,10 +1,11 @@ -CFLAGS=-O3 -Wall -Wextra -lX11 -lpci +CFLAGS=-O2 -Wall -Wextra -lX11 -lpci PREFIX=$(HOME)/.local +CACHE=$(shell if [ "$$XDG_CACHE_HOME" ]; then echo "$$XDG_CACHE_HOME"; else echo "$$HOME"/.cache; fi) all: paleofetch clean: - rm -f paleofetch + rm -f paleofetch $(CACHE)/paleofetch paleofetch: paleofetch.c paleofetch.h config.h $(CC) paleofetch.c -o paleofetch $(CFLAGS) diff --git a/config.h b/config.h @@ -3,26 +3,26 @@ #define CONFIG \ { \ - /* name function cached */\ - { "", get_title, false }, \ - { "", get_bar, false }, \ - { "OS: ", get_os, true }, \ - { "Host: ", get_host, true }, \ - { "Kernel: ", get_kernel, true }, \ - { "Uptime: ", get_uptime, false }, \ + /* name function cached */\ + { "", get_title, false }, \ + { "", get_bar, false }, \ + { "OS: ", get_os, true }, \ + { "Host: ", get_host, true }, \ + { "Kernel: ", get_kernel, true }, \ + { "Uptime: ", get_uptime, false }, \ SPACER \ - { "Packages: ", get_packages, false }, \ - { "Shell: ", get_shell, false }, \ - { "Resolution: ", get_resolution, false }, \ - { "Terminal: ", get_terminal, false }, \ + { "Packages: ", get_packages_pacman, false }, \ + { "Shell: ", get_shell, false }, \ + { "Resolution: ", get_resolution, false }, \ + { "Terminal: ", get_terminal, false }, \ SPACER \ - { "CPU: ", get_cpu, true }, \ - { "GPU: ", get_gpu1, true }, \ - { "Memory: ", get_memory, false }, \ + { "CPU: ", get_cpu, true }, \ + { "GPU: ", get_gpu1, true }, \ + { "Memory: ", get_memory, false }, \ SPACER \ - { "", get_colors1, false }, \ - { "", get_colors2, false }, \ -}; + { "", get_colors1, false }, \ + { "", get_colors2, false }, \ +} #define CPU_CONFIG \ { \ @@ -34,9 +34,9 @@ REMOVE("Eight-Core"), \ REMOVE("Core"), \ REMOVE("CPU"), \ -}; +} #define GPU_CONFIG \ { \ REMOVE("Corporation"), \ -}; +} diff --git a/logos/arch3.h b/logos/arch3.h @@ -0,0 +1,22 @@ +// made by Reddit user LnLcFlx2 +char *LOGO[] = { +" ▄ ", +" ▟█▙ ", +" ▟███▙ ", +" ▟█████▙ ", +" ▟███████▙ ", +" ▂▔▀▜██████▙ ", +" ▟██▅▂▝▜█████▙ ", +" ▟█████████████▙ ", +" ▟███████████████▙ ", +" ▟█████████████████▙ ", +" ▟███████████████████▙ ", +" ▟█████████▛▀▀▜████████▙ ", +" ▟████████▛ ▜███████▙ ", +" ▟█████████ ████████▙ ", +" ▟██████████ █████▆▅▄▃▂ ", +" ▟██████████▛ ▜█████████▙ ", +" ▟██████▀▀▀ ▀▀██████▙ ", +" ▟███▀▘ ▝▀███▙ ", +" ▟▛▀ ▀▜▙ " +}; diff --git a/paleofetch.c b/paleofetch.c @@ -1,3 +1,4 @@ +#pragma GCC diagnostic ignored "-Wunused-function" #include <unistd.h> #include <stdio.h> #include <stdlib.h> @@ -8,6 +9,8 @@ #include <sys/utsname.h> #include <sys/sysinfo.h> +#include <sys/statvfs.h> + #include <pci/pci.h> #include <X11/Xlib.h> @@ -32,21 +35,18 @@ struct conf { bool cached; } config[] = CONFIG; -typedef struct { +struct { char *substring; char *repl_str; size_t length; size_t repl_len; -} STRING_MODIFY; - -STRING_MODIFY cpu_config[] = CPU_CONFIG; -STRING_MODIFY gpu_config[] = GPU_CONFIG; +} cpu_config[] = CPU_CONFIG, gpu_config[] = GPU_CONFIG; Display *display; +struct statvfs file_stats; struct utsname uname_info; struct sysinfo my_sysinfo; -int title_length; -int status; +int title_length, status; /* * Replaces the first newline character with null terminator @@ -59,9 +59,11 @@ void remove_newline(char *s) { /* * Cleans up repeated spaces in a string + * Trim spaces at the front of a string */ void truncate_spaces(char *str) { int src = 0, dst = 0; + while(*(str + dst) == ' ') dst++; while(*(str + dst) != '\0') { *(str + src) = *(str + dst); @@ -71,7 +73,7 @@ void truncate_spaces(char *str) { src++; } - *(str +src) = '\0'; + *(str + src) = '\0'; } /* @@ -105,7 +107,7 @@ void replace_substring(char *str, const char *sub_str, const char *repl_str, siz strcpy(start + repl_len, buffer); } -char *get_title() { +static char *get_title() { // reduce the maximum size for these, so that we don't over-fill the title string char hostname[BUF_SIZE / 3]; status = gethostname(hostname, BUF_SIZE / 3); @@ -122,7 +124,7 @@ char *get_title() { return title; } -char *get_bar() { +static char *get_bar() { char *bar = malloc(BUF_SIZE); char *s = bar; for(int i = 0; i < title_length; i++) *(s++) = '-'; @@ -130,7 +132,7 @@ char *get_bar() { return bar; } -char *get_os() { +static char *get_os() { char *os = malloc(BUF_SIZE), *name = malloc(BUF_SIZE), *line = NULL; @@ -153,13 +155,13 @@ char *get_os() { return os; } -char *get_kernel() { +static char *get_kernel() { char *kernel = malloc(BUF_SIZE); strncpy(kernel, uname_info.release, BUF_SIZE); return kernel; } -char *get_host() { +static char *get_host() { FILE *product_name = fopen("/sys/devices/virtual/dmi/id/product_name", "r"); if(product_name == NULL) { @@ -192,7 +194,7 @@ char *get_host() { return host; } -char *get_uptime() { +static char *get_uptime() { long seconds = my_sysinfo.uptime; struct { char *name; int secs; } units[] = { { "day", 60 * 60 * 24 }, @@ -214,35 +216,36 @@ char *get_uptime() { return uptime; } -// full disclosure: I don't know if this is a good idea -char *get_packages() { +static char *get_packages(const char* dirname, const char* pacname, int num_extraneous) { int num_packages = 0; DIR * dirp; struct dirent *entry; - dirp = opendir("/var/lib/pacman/local"); + dirp = opendir(dirname); if(dirp == NULL) { status = -1; - halt_and_catch_fire("Do you not have pacman installed? How did you find this?\n" - "Please email samfbarr@outlook.com with the details of how you got here.\n" - "This information will be very useful for my upcoming demographics survey."); + halt_and_catch_fire("You may not have %s installed", dirname); } while((entry = readdir(dirp)) != NULL) { if(entry->d_type == DT_DIR) num_packages++; } - num_packages -= 2; // accounting for . and .. + num_packages -= (2 + num_extraneous); // accounting for . and .. status = closedir(dirp); char *packages = malloc(BUF_SIZE); - snprintf(packages, BUF_SIZE, "%d (pacman)", num_packages); + snprintf(packages, BUF_SIZE, "%d (%s)", num_packages, pacname); return packages; } -char *get_shell() { +static char *get_packages_pacman() { + return get_packages("/var/lib/pacman/local", "pacman", 0); +} + +static char *get_shell() { char *shell = malloc(BUF_SIZE); char *shell_path = getenv("SHELL"); char *shell_name = strrchr(getenv("SHELL"), '/'); @@ -255,7 +258,7 @@ char *get_shell() { return shell; } -char *get_resolution() { +static char *get_resolution() { int screen, width, height; char *resolution = malloc(BUF_SIZE); @@ -311,7 +314,7 @@ char *get_resolution() { return resolution; } -char *get_terminal() { +static char *get_terminal() { unsigned char *prop; char *terminal = malloc(BUF_SIZE); @@ -330,6 +333,7 @@ char *get_terminal() { GetProp(active); window = (prop[3] << 24) + (prop[2] << 16) + (prop[1] << 8) + prop[0]; free(prop); + if(!window) goto terminal_fallback; GetProp(class); #undef GetProp @@ -337,13 +341,14 @@ char *get_terminal() { snprintf(terminal, BUF_SIZE, "%s", prop); free(prop); } else { +terminal_fallback: strncpy(terminal, getenv("TERM"), BUF_SIZE); /* fallback to old method */ } return terminal; } -char *get_cpu() { +static char *get_cpu() { FILE *cpuinfo = fopen("/proc/cpuinfo", "r"); /* read from cpu info */ if(cpuinfo == NULL) { status = -1; @@ -353,7 +358,7 @@ char *get_cpu() { char *cpu_model = malloc(BUF_SIZE / 2); char *line = NULL; size_t len; /* unused */ - int num_cores = 0; + int num_cores = 0, cpu_freq, prec = 3; double freq; /* read the model name into cpu_model, and increment num_cores every time model name is found */ @@ -373,8 +378,14 @@ char *get_cpu() { line = NULL; if (getline(&line, &len, cpufreq) != -1) { - sscanf(line, "%lf", &freq); - freq /= 1e6; // convert kHz to GHz + sscanf(line, "%d", &cpu_freq); + cpu_freq /= 1000; // convert kHz to MHz + freq = cpu_freq / 1000.0; // convert MHz to GHz and cast to double + while (cpu_freq % 10 == 0) { + --prec; + cpu_freq /= 10; + } + if (prec == 0) prec = 1; // we don't want zero decimal places } else { freq = 0.0; // cpuinfo_max_freq not available? } @@ -392,14 +403,14 @@ char *get_cpu() { } char *cpu = malloc(BUF_SIZE); - snprintf(cpu, BUF_SIZE, "%s (%d) @ %.1fGHz", cpu_model, num_cores, freq); + snprintf(cpu, BUF_SIZE, "%s (%d) @ %.*fGHz", cpu_model, num_cores, prec, freq); free(cpu_model); truncate_spaces(cpu); return cpu; } -char *find_gpu(int index) { +static char *find_gpu(int index) { // inspired by https://github.com/pciutils/pciutils/edit/master/example.c /* it seems that pci_lookup_name needs to be given a buffer, but I can't for the life of my figure out what its for */ char buffer[BUF_SIZE], *device_class, *gpu = malloc(BUF_SIZE); @@ -447,15 +458,15 @@ char *find_gpu(int index) { return gpu; } -char *get_gpu1() { +static char *get_gpu1() { return find_gpu(0); } -char *get_gpu2() { +static char *get_gpu2() { return find_gpu(1); } -char *get_memory() { +static char *get_memory() { int total_memory, used_memory; int total, shared, memfree, buffers, cached, reclaimable; @@ -495,7 +506,31 @@ char *get_memory() { return memory; } -char *get_colors1() { +static char *get_disk_usage(const char *folder) { + char *disk_usage = malloc(BUF_SIZE); + long total, used, free; + int percentage; + status = statvfs(folder, &file_stats); + halt_and_catch_fire("Error getting disk usage for %s", folder); + total = file_stats.f_blocks * file_stats.f_frsize; + free = file_stats.f_bfree * file_stats.f_frsize; + used = total - free; + percentage = (used / (double) total) * 100; +#define TO_GB(A) ((A) / (1024.0 * 1024 * 1024)) + snprintf(disk_usage, BUF_SIZE, "%.1fGiB / %.1fGiB (%d%%)", TO_GB(used), TO_GB(total), percentage); +#undef TO_GB + return disk_usage; +} + +static char *get_disk_usage_root() { + return get_disk_usage("/"); +} + +static char *get_disk_usage_home() { + return get_disk_usage("/home"); +} + +static char *get_colors1() { char *colors1 = malloc(BUF_SIZE); char *s = colors1; @@ -508,7 +543,7 @@ char *get_colors1() { return colors1; } -char *get_colors2() { +static char *get_colors2() { char *colors2 = malloc(BUF_SIZE); char *s = colors2; @@ -521,7 +556,7 @@ char *get_colors2() { return colors2; } -char *spacer() { +static char *spacer() { return calloc(1, 1); // freeable, null-terminated string of length 1 } diff --git a/paleofetch.h b/paleofetch.h @@ -1,23 +1,25 @@ /* Forward-declare our functions so users can mention them in their * configs at the top of the file rather than near the bottom. */ -char *get_title(), - *get_bar(), - *get_os(), - *get_kernel(), - *get_host(), - *get_uptime(), - *get_packages(), - *get_shell(), - *get_resolution(), - *get_terminal(), - *get_cpu(), - *get_gpu1(), - *get_gpu2(), - *get_memory(), - *get_colors1(), - *get_colors2(), - *spacer(); +static char *get_title(), + *get_bar(), + *get_os(), + *get_kernel(), + *get_host(), + *get_uptime(), + *get_packages_pacman(), + *get_shell(), + *get_resolution(), + *get_terminal(), + *get_cpu(), + *get_gpu1(), + *get_gpu2(), + *get_memory(), + *get_disk_usage_root(), + *get_disk_usage_home(), + *get_colors1(), + *get_colors2(), + *spacer(); #define SPACER {"", spacer, false}, #define REMOVE(A) { (A), NULL, sizeof(A) - 1 , 0 }