paleofetch

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

commit cbbe1a7d190c71904c9067f1f3647f3726afdf31
parent 9544cfe6be9cc9ca3d2e8360cdec173b45617433
Author: sam-barr <samfbarr@outlook.com>
Date:   Sun,  3 May 2020 13:50:47 -0500

Merge pull request #41 from dwzg/replace_substring

Add configurable way to replace strings of CPU and GPU name
Diffstat:
MREADME.md | 9+++++++++
Mconfig.h | 4++--
Mpaleofetch.c | 40+++++++++++++++++++++++++++++++++++-----
Mpaleofetch.h | 3++-
4 files changed, 48 insertions(+), 8 deletions(-)

diff --git a/README.md b/README.md @@ -85,6 +85,15 @@ The booleans in `CONFIG` tell paleofetch whether you want to cache an entry. When cached, paleofetch will save the value and not recompute it whenever you run paleofetch (unless you specify the `--recache` option). +The CPU and GPU name can be configured as well. This is done under the CPU_CONFIG and GPU_CONFIG section +in the config.h file. Two macros are provided to customize and tidy up the model names: + +* `REMOVE(string)`: removes the first occurence of `string` +* `REPLACE(string1, string2)`: replaces the first occurence of `string1` with `string2` + +Don't forget to run paleofetch with the --recache flag after compiling it with your new +configuration, otherwise it will still show the old name for already cached entries. + FAQ --- diff --git a/config.h b/config.h @@ -24,7 +24,7 @@ { "", get_colors2, false }, \ } -#define CPU_REMOVE \ +#define CPU_CONFIG \ { \ REMOVE("(R)"), \ REMOVE("(TM)"), \ @@ -36,7 +36,7 @@ REMOVE("CPU"), \ } -#define GPU_REMOVE \ +#define GPU_CONFIG \ { \ REMOVE("Corporation"), \ } diff --git a/paleofetch.c b/paleofetch.c @@ -37,8 +37,10 @@ struct conf { struct { char *substring; + char *repl_str; size_t length; -} cpu_remove[] = CPU_REMOVE, gpu_remove[] = GPU_REMOVE; + size_t repl_len; +} cpu_config[] = CPU_CONFIG, gpu_config[] = GPU_CONFIG; Display *display; struct statvfs file_stats; @@ -89,6 +91,26 @@ void remove_substring(char *str, const char* substring, size_t len) { while(*(sub+(++i)) != '\0'); } +/* + * Replaces the first sub_len characters of sub_str from str + * with the first repl_len characters of repl_str + */ +void replace_substring(char *str, const char *sub_str, const char *repl_str, size_t sub_len, size_t repl_len) { + char buffer[BUF_SIZE / 2]; + char *start = strstr(str, sub_str); + if (start == NULL) return; // substring not found + + /* check if we have enough space for new substring */ + if (strlen(str) - sub_len + repl_len >= BUF_SIZE / 2) { + status = -1; + halt_and_catch_fire("new substring too long to replace"); + } + + strcpy(buffer, start + sub_len); + strncpy(start, repl_str, repl_len); + strcpy(start + repl_len, buffer); +} + 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]; @@ -402,8 +424,12 @@ cpufreq_fallback: } /* remove unneeded information */ - for (int i = 0; i < COUNT(cpu_remove); ++i) { - remove_substring(cpu_model, cpu_remove[i].substring, cpu_remove[i].length); + for (int i = 0; i < COUNT(cpu_config); ++i) { + if (cpu_config[i].repl_str == NULL) { + remove_substring(cpu_model, cpu_config[i].substring, cpu_config[i].length); + } else { + replace_substring(cpu_model, cpu_config[i].substring, cpu_config[i].repl_str, cpu_config[i].length, cpu_config[i].repl_len); + } } char *cpu = malloc(BUF_SIZE); @@ -449,8 +475,12 @@ static char *find_gpu(int index) { pci_cleanup(pacc); /* remove unneeded information */ - for (int i = 0; i < COUNT(gpu_remove); ++i) { - remove_substring(gpu, gpu_remove[i].substring, gpu_remove[i].length); + for (int i = 0; i < COUNT(gpu_config); ++i) { + if (gpu_config[i].repl_str == NULL) { + remove_substring(gpu, gpu_config[i].substring, gpu_config[i].length); + } else { + replace_substring(gpu, gpu_config[i].substring, gpu_config[i].repl_str, gpu_config[i].length, gpu_config[i].repl_len); + } } truncate_spaces(gpu); diff --git a/paleofetch.h b/paleofetch.h @@ -22,4 +22,5 @@ static char *get_title(), *spacer(); #define SPACER {"", spacer, false}, -#define REMOVE(A) { (A), sizeof(A) - 1} +#define REMOVE(A) { (A), NULL, sizeof(A) - 1 , 0 } +#define REPLACE(A, B) { (A), (B), sizeof(A) - 1, sizeof(B) - 1 }