From 0d7d966da3ddbd144564df9563028f9a996c66eb Mon Sep 17 00:00:00 2001 From: jmattaa Date: Tue, 5 Nov 2024 21:43:06 +0100 Subject: [PATCH 1/3] feat: add environment variable to set colors colors can be set through environment variable now Signed-off-by: jmattaa --- Makefile | 4 +- include/colors.h | 66 ++++++++++++++++++++----------- src/colors.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++ src/laser.c | 30 +++++++------- src/main.c | 5 +++ 5 files changed, 166 insertions(+), 40 deletions(-) create mode 100644 src/colors.c diff --git a/Makefile b/Makefile index 0d7f8f6..b5ca79a 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,6 @@ CC := gcc -LASER_DEFINES := -DLASER_NF_SYMBOLS - -CFLAGS := $(LASER_DEFINES) +CFLAGS := CFLAGS_DEBUG := $(CFLAGS) -g -DDEBUG -fsanitize=address -Wall -Wextra -pedantic CFLAGS_RELEASE := $(CFLAGS) -O2 -DNDEBUG diff --git a/include/colors.h b/include/colors.h index 9dd834a..87e5f6f 100644 --- a/include/colors.h +++ b/include/colors.h @@ -1,28 +1,50 @@ #ifndef LASER_COLORS_H #define LASER_COLORS_H -#ifdef LASER_NF_SYMBOLS - -#define RESET_COLOR "\x1b[0m" -#define DIR_COLOR "\x1b[34m " -#define SYMLINK_COLOR "\x1b[36m " -#define FILE_COLOR "\x1b[38m " -#define HIDDEN_COLOR "\x1b[90m " -#define EXEC_COLOR "\x1b[32m " -#define ARCHIVE_COLOR "\x1b[31m " -#define MEDIA_COLOR "\x1b[33m " - -#else - -#define RESET_COLOR "\x1b[0m " -#define DIR_COLOR "\x1b[34m " -#define SYMLINK_COLOR "\x1b[36m " -#define FILE_COLOR "\x1b[38m " -#define HIDDEN_COLOR "\x1b[90m " -#define EXEC_COLOR "\x1b[32m " -#define ARCHIVE_COLOR "\x1b[31m " -#define MEDIA_COLOR "\x1b[33m " +#include +#include +#include -#endif +#define LASER_DEFAULT_RESET_COLOR "\x1b[0m" +#define LASER_DEFAULT_DIR_COLOR "\x1b[34m" +#define LASER_DEFAULT_SYMLINK_COLOR "\x1b[36m" +#define LASER_DEFAULT_FILE_COLOR "\x1b[38m" +#define LASER_DEFAULT_HIDDEN_COLOR "\x1b[90m" +#define LASER_DEFAULT_EXEC_COLOR "\x1b[32m" +#define LASER_DEFAULT_ARCHIVE_COLOR "\x1b[31m" +#define LASER_DEFAULT_MEDIA_COLOR "\x1b[33m" + +#define NUM_KEYS 8 // update this to be as many colors there is!!!! + +typedef struct laser_colors +{ + const char *reset; + const char *dir; + const char *symlink; + const char *file; + const char *hidden; + const char *exec; + const char *archive; + const char *media; +} laser_colors; + +typedef enum +{ + LASER_COLORKEY_RESET, + LASER_COLORKEY_DIR, + LASER_COLORKEY_SYMLINK, + LASER_COLORKEY_FILE_KEY, + LASER_COLORKEY_HIDDEN, + LASER_COLORKEY_EXEC, + LASER_COLORKEY_ARCHIVE, + LASER_COLORKEY_MEDIA, + LASER_COLORKEY_UNKNOWN +} laser_colorkey; + +void laser_colors_init(void); +void laser_colors_parseToken(const char *token); +void laser_colors_destroy(void); + +extern laser_colors *LASER_COLORS; #endif diff --git a/src/colors.c b/src/colors.c new file mode 100644 index 0000000..444a129 --- /dev/null +++ b/src/colors.c @@ -0,0 +1,101 @@ +#include "colors.h" + +laser_colors *LASER_COLORS; + +void laser_colors_init(void) +{ + LASER_COLORS = malloc(sizeof(laser_colors)); + + LASER_COLORS->reset = strdup(LASER_DEFAULT_RESET_COLOR); + LASER_COLORS->dir = strdup(LASER_DEFAULT_DIR_COLOR); + LASER_COLORS->symlink = strdup(LASER_DEFAULT_SYMLINK_COLOR); + LASER_COLORS->file = strdup(LASER_DEFAULT_FILE_COLOR); + LASER_COLORS->hidden = strdup(LASER_DEFAULT_HIDDEN_COLOR); + LASER_COLORS->exec = strdup(LASER_DEFAULT_EXEC_COLOR); + LASER_COLORS->archive = strdup(LASER_DEFAULT_ARCHIVE_COLOR); + LASER_COLORS->media = strdup(LASER_DEFAULT_MEDIA_COLOR); + + const char *env_colors = getenv("LSR_COLORS"); + + if (env_colors) + { + char *colors_copy = strdup(env_colors); + char *token = strtok(colors_copy, ":"); + + while (token != NULL) + { + laser_colors_parseToken(token); + token = strtok(NULL, ":"); + } + } +} + +void laser_colors_parseToken(const char *token) +{ + char *separator = strchr(token, '='); + + if (separator != NULL) + { + *separator = '\0'; + const char *key = token; + const char *value = separator + 1; + + char *processed_value = malloc(strlen(value) + 1); + char *dest = processed_value; + + // change a literal "\x1b" to '\x1b' + while (*value) + { + if (value[0] == '\\' && value[1] == 'x' && value[2] == '1' && + value[3] == 'b') + { + *dest++ = '\x1b'; + value += 4; + } + else + *dest++ = *value++; + } + *dest = '\0'; + value = processed_value; + + // TODO: DO SOMETHING INSTEAD OF THE IF-ELSE AND STRCMP + if (strcmp(key, "RESET") == 0) + LASER_COLORS->reset = value; + else if (strcmp(key, "DIR") == 0) + LASER_COLORS->dir = value; + else if (strcmp(key, "SYMLINK") == 0) + LASER_COLORS->symlink = value; + else if (strcmp(key, "FILE") == 0) + LASER_COLORS->file = value; + else if (strcmp(key, "HIDDEN") == 0) + LASER_COLORS->hidden = value; + else if (strcmp(key, "EXEC") == 0) + LASER_COLORS->exec = value; + else if (strcmp(key, "ARCHIVE") == 0) + LASER_COLORS->archive = value; + else if (strcmp(key, "MEDIA") == 0) + LASER_COLORS->media = value; + } +} + +void laser_colors_destroy(void) +{ + if (LASER_COLORS->reset) + free(LASER_COLORS->reset); + if (LASER_COLORS->dir) + free(LASER_COLORS->dir); + if (LASER_COLORS->symlink) + free(LASER_COLORS->symlink); + if (LASER_COLORS->file) + free(LASER_COLORS->file); + if (LASER_COLORS->hidden) + free(LASER_COLORS->hidden); + if (LASER_COLORS->exec) + free(LASER_COLORS->exec); + if (LASER_COLORS->archive) + free(LASER_COLORS->archive); + if (LASER_COLORS->media) + free(LASER_COLORS->media); + + free(LASER_COLORS); +} diff --git a/src/laser.c b/src/laser.c index 2844951..d5ac4f4 100644 --- a/src/laser.c +++ b/src/laser.c @@ -54,7 +54,7 @@ void laser_print_entry(const char *name, const char *color, char *indent, if (depth > 0) strcpy(branch, is_last ? "└─ " : "├─ "); - printf("%s%s%s%s" RESET_COLOR "\n", indent, branch, color, name); + printf("%s%s%s%s%s\n", indent, branch, color, name, LASER_COLORS->reset); } void laser_process_entries(laser_opts opts, int depth, char *indent, @@ -126,8 +126,8 @@ void laser_process_entries(laser_opts opts, int depth, char *indent, if (S_ISDIR(file_stat.st_mode)) { - laser_print_entry(entries[i]->d_name, DIR_COLOR, indent, depth, - is_last); + laser_print_entry(entries[i]->d_name, LASER_COLORS->dir, indent, + depth, is_last); if (opts.show_tree) { @@ -149,34 +149,34 @@ void laser_process_entries(laser_opts opts, int depth, char *indent, char res_string[PATH_MAX * 2 + 4]; // 4 is " -> " snprintf(res_string, sizeof(res_string), "%s -> %s", entries[i]->d_name, symlink_target); - laser_print_entry(res_string, SYMLINK_COLOR, indent, depth, - is_last); + laser_print_entry(res_string, LASER_COLORS->symlink, indent, + depth, is_last); } } else if (laser_is_filestat_exec(&file_stat)) { - laser_print_entry(entries[i]->d_name, EXEC_COLOR, indent, depth, - is_last); + laser_print_entry(entries[i]->d_name, LASER_COLORS->exec, + indent, depth, is_last); } else if (laser_checktype(full_path, laser_archiveformats)) { - laser_print_entry(entries[i]->d_name, ARCHIVE_COLOR, indent, - depth, is_last); + laser_print_entry(entries[i]->d_name, LASER_COLORS->archive, + indent, depth, is_last); } else if (laser_checktype(full_path, laser_mediaformats)) { - laser_print_entry(entries[i]->d_name, MEDIA_COLOR, indent, - depth, is_last); + laser_print_entry(entries[i]->d_name, LASER_COLORS->media, + indent, depth, is_last); } else if (entries[i]->d_name[0] == '.') { - laser_print_entry(entries[i]->d_name, HIDDEN_COLOR, indent, - depth, is_last); + laser_print_entry(entries[i]->d_name, LASER_COLORS->hidden, + indent, depth, is_last); } else if (S_ISREG(file_stat.st_mode)) { - laser_print_entry(entries[i]->d_name, FILE_COLOR, indent, depth, - is_last); + laser_print_entry(entries[i]->d_name, LASER_COLORS->file, + indent, depth, is_last); } } diff --git a/src/main.c b/src/main.c index 92da1c9..93f4e1b 100644 --- a/src/main.c +++ b/src/main.c @@ -1,9 +1,14 @@ +#include "colors.h" #include "laser.h" #include "utils.h" int main(int argc, char **argv) { + laser_colors_init(); + laser_opts opts = laser_utils_parsecmd(argc, argv); laser_list_directory(opts, 0); + + laser_colors_destroy(); return 0; } From 63cc66be0f7f1c566b3f753dbfe65ea352951646 Mon Sep 17 00:00:00 2001 From: jmattaa Date: Tue, 5 Nov 2024 21:48:12 +0100 Subject: [PATCH 2/3] docs: update readme to use LSR_COLORS Signed-off-by: jmattaa --- README.md | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a75202f..6e20c32 100644 --- a/README.md +++ b/README.md @@ -17,11 +17,6 @@ It can even display the directory contents in a tree-like structure! :fire: ### Building from source -> [!NOTE] -> If you want to disable the nerdfonts symbols remove the -`-DLASER_NF_SYMBOLS` flag from the `Makefile` under the variable `LASER_DEFINES` -before compiling. - Clone the repository: ```sh @@ -51,6 +46,26 @@ lsr lsr some-directory ``` +### Change colors or add icons + +If you want to change the colors of the output you can set an environment +variable called `LSR_COLORS` for example if you want to add nerd font icons to +the existing colors you can use: + +```sh +export LSR_COLORS="RESET=\x1b[0m:\ +DIR=\x1b[34m :\ +SYMLINK=\x1b[36m :\ +FILE=\x1b[38m :\ +HIDDEN=\x1b[90m :\ +EXEC=\x1b[32m :\ +ARCHIVE=\x1b[31m :\ +MEDIA=\x1b[33m" +``` + +You don't have to change all of these if there is only one you want to change +you can do that, to leave the rest on default + ### Command-line options The command-line options can be added by passing flags. These flags can be put From 2f97dee813d07cd407c3ee304180b122e39f57d8 Mon Sep 17 00:00:00 2001 From: jmattaa Date: Tue, 5 Nov 2024 21:53:23 +0100 Subject: [PATCH 3/3] fix: format code --- include/colors.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/colors.h b/include/colors.h index 87e5f6f..d0c7e07 100644 --- a/include/colors.h +++ b/include/colors.h @@ -38,7 +38,7 @@ typedef enum LASER_COLORKEY_EXEC, LASER_COLORKEY_ARCHIVE, LASER_COLORKEY_MEDIA, - LASER_COLORKEY_UNKNOWN + LASER_COLORKEY_UNKNOWN } laser_colorkey; void laser_colors_init(void);