Thumbnail

rani/cscroll.git

Clone URL: https://git.buni.party/rani/cscroll.git

commit 5ead889b512e194075af631924fa96cd284bd288 Author: Raniconduh <clagv.randomgames@gmail.com> Date: Mon Jan 31 17:24:55 2022 +0000 added support for custom colors in config file diff --git a/Makefile b/Makefile index 03e4474..dd62d29 100644 --- a/Makefile +++ b/Makefile @@ -77 +77 @@ ICONS ?= 1    CC ?= cc  CFLAGS += -DICONS=$(ICONS) -Wall -Wextra -pedantic $(shell pkg-config --cflags ncurses) -LIBS += $(shell pkg-config --libs ncurses) -ltinfo +LIBS += $(shell pkg-config --libs ncurses) -ltinfo -lm    PREFIX ?= /usr/local   diff --git a/src/opts.c b/src/opts.c index 87231a4..35a2067 100644 --- a/src/opts.c +++ b/src/opts.c @@ -12710 +12722 @@ void read_config(void) {   // remove trailing white space after '='   while (*val && *val != '=' && isspace(*val)) val++;   + void * ptr_val = NULL; +   bool bool_val = false; - if (!strcmp(val, "true")) bool_val = true; + if (!strcmp(val, "true")) { + bool_val = true; + ptr_val = &bool_val; + } else if (!strcmp(val, "false")) { + bool_val = false; + ptr_val = &bool_val; + } else if (val[0] == '"' && val[strlen(val) - 1] == '"') { + val++; + val[strlen(val) - 1] = 0; + ptr_val = val; + }   - var_set(var, &bool_val); + var_set(var, ptr_val);   }     fclose(fp); diff --git a/src/var.c b/src/var.c index 7f1b855..46f5ea9 100644 --- a/src/var.c +++ b/src/var.c @@ -15 +18 @@  #include <stdbool.h>  #include <stddef.h> +#include <stdint.h> +#include <ctype.h> +#include <math.h>    #include "hash.h"  #include "opts.h" @@ -96 +1237 @@  static map * var_map = NULL;     +// must be 6 characters long +static uint32_t hextorgb(char * hex) { + char * p = hex; + if (*p == '#') p++; + + char htod[] = { + ['A'] = 10, ['B'] = 11, ['C'] = 12, + ['D'] = 13, ['E'] = 14, ['F'] = 15 + }; + + uint16_t r, g, b; + uint32_t dec = 0; + int cpow = strlen(p) - 1; + + for (; *p; p++) { + char c = toupper(*p); + if (c >= 'A' && c <= 'F') c = htod[(int)c]; + else c -= '0'; + dec += c * pow(16, cpow); + cpow--; + } + + // convert to 10 bit color over 8 bit color + r = (float)((dec >> 16) & 0xFF) / 255.0 * 1000; + g = (float)((dec >> 8) & 0xFF) / 255.0 * 1000; + b = (float)(dec & 0xFF) / 255.0 * 1000; + + return RGB(r, g, b); +} + +  // void * p -> bool p  static void set_icons(void * p) {   show_icons = *(bool*)p; @@ -2612 +6083 @@ static void set_long(void * p) {  }     +static void set_dir(void * p) { + dir_color = hextorgb((char*)p); + set_color(); +} + + +static void set_reg(void * p) { + reg_color = hextorgb((char*)p); + set_color(); +} + + +static void set_fifo(void * p) { + fifo_color = hextorgb((char*)p); + set_color(); +} + + +static void set_link(void * p) { + link_color = hextorgb((char*)p); + set_color(); +} + + +static void set_block(void * p) { + blk_color = hextorgb((char*)p); + set_color(); +} + + +static void set_sock(void * p) { + sock_color = hextorgb((char*)p); + set_color(); +} + + +static void set_unknown(void * p) { + unknown_color = hextorgb((char*)p); + set_color(); +} + + +static void set_exec(void * p) { + exec_color = hextorgb((char*)p); + set_color(); +} + + +static void set_media(void * p) { + media_color = hextorgb((char*)p); + set_color(); +} + + +static void set_archive(void * p) { + archive_color = hextorgb((char*)p); + set_color(); +} + +  void var_init(void) { - var_map = map_new(4); + var_map = map_new(11);     map_insert(var_map, ICONS_VAR, set_icons);   map_insert(var_map, COLOR_VAR, set_color_f);   map_insert(var_map, LONG_VAR, set_long); + + map_insert(var_map, DIR_COLOR_VAR, set_dir); + map_insert(var_map, REG_COLOR_VAR, set_reg); + map_insert(var_map, FIFO_COLOR_VAR, set_fifo); + map_insert(var_map, LINK_COLOR_VAR, set_link); + map_insert(var_map, BLK_COLOR_VAR, set_block); + map_insert(var_map, SOCK_COLOR_VAR, set_sock); + map_insert(var_map, UNKNOWN_COLOR_VAR, set_unknown); + map_insert(var_map, EXEC_COLOR_VAR, set_exec); + map_insert(var_map, MEDIA_COLOR_VAR, set_media); + map_insert(var_map, ARCHIVE_COLOR_VAR, set_archive);  }