Skip to content

Commit

Permalink
refactor(colors): restructure color definitions
Browse files Browse the repository at this point in the history
using less boilerplate to work with the different colors

Signed-off-by: jmattaa <mattajonathan1@gmail.com>
  • Loading branch information
jmattaa committed Nov 6, 2024
1 parent 6bf2e35 commit ac8ca04
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 88 deletions.
63 changes: 27 additions & 36 deletions include/colors.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,38 @@
#include <stdlib.h>
#include <string.h>

#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[32;4m"
#define LASER_DEFAULT_ARCHIVE_COLOR "\x1b[31m"
#define LASER_DEFAULT_MEDIA_COLOR "\x1b[33m"
#define LASER_DEFAULT_DOCUMENT_COLOR "\x1b[35;3m"

typedef struct laser_colors
#define COLOR_COUNT 9

typedef struct
{
const char *reset;
const char *dir;
const char *symlink;
const char *file;
const char *hidden;
const char *exec;
const char *archive;
const char *media;
const char *documents;
} laser_colors;

typedef enum
const char *key;
const char *value;
} laser_color;

static const laser_color LASER_COLORS_DEFAULTS[COLOR_COUNT] = {
{"RESET", "\x1b[0m"}, {"DIR", "\x1b[34m"},
{"SYMLINK", "\x1b[36m"}, {"FILE", "\x1b[38m"},
{"HIDDEN", "\x1b[90m"}, {"EXEC", "\x1b[32;4m"},
{"ARCHIVE", "\x1b[31m"}, {"MEDIA", "\x1b[33m"},
{"DOCUMENT", "\x1b[35;3m"}};

enum laser_color_type
{
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_DOCUMENTS,
LASER_COLORKEY_UNKNOWN
} laser_colorkey;
LASER_COLOR_RESET,
LASER_COLOR_DIR,
LASER_COLOR_SYMLINK,
LASER_COLOR_FILE,
LASER_COLOR_HIDDEN,
LASER_COLOR_EXEC,
LASER_COLOR_ARCHIVE,
LASER_COLOR_MEDIA,
LASER_COLOR_DOCUMENT
};

extern laser_color *LASER_COLORS;

void laser_colors_init(void);
void laser_colors_parseToken(const char *token);
void laser_colors_destroy(void);

extern laser_colors *LASER_COLORS;

#endif
60 changes: 21 additions & 39 deletions src/colors.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
#include "colors.h"
#include <stdio.h>

laser_colors *LASER_COLORS;
laser_color *LASER_COLORS;

void laser_colors_init(void)
{
LASER_COLORS = malloc(sizeof(laser_colors));
LASER_COLORS = malloc(sizeof(laser_color) * COLOR_COUNT);

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);
LASER_COLORS->documents = strdup(LASER_DEFAULT_DOCUMENT_COLOR);
for (int i = 0; i < COLOR_COUNT; i++)
{
LASER_COLORS[i].key = strdup(LASER_COLORS_DEFAULTS[i].key);
LASER_COLORS[i].value = strdup(LASER_COLORS_DEFAULTS[i].value);
}

const char *env_colors = getenv("LSR_COLORS");

Expand Down Expand Up @@ -59,39 +56,24 @@ void laser_colors_parseToken(const char *token)
*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;
else if (strcmp(key, "DOCUMENT") == 0)
LASER_COLORS->documents = value;
for (int i = 0; i < COLOR_COUNT; i++)
{
if (strcmp(key, LASER_COLORS[i].key) == 0)
{
LASER_COLORS[i].value = value;
break;
}
}
}
}

void laser_colors_destroy(void)
{
free((void *)LASER_COLORS->reset);
free((void *)LASER_COLORS->dir);
free((void *)LASER_COLORS->symlink);
free((void *)LASER_COLORS->file);
free((void *)LASER_COLORS->hidden);
free((void *)LASER_COLORS->exec);
free((void *)LASER_COLORS->archive);
free((void *)LASER_COLORS->media);
free((void *)LASER_COLORS->documents);
for (int i = 0; i < COLOR_COUNT; i++)
{
free((void *)LASER_COLORS[i].value);
free((void *)LASER_COLORS[i].key);
}

free(LASER_COLORS);
}
36 changes: 23 additions & 13 deletions src/laser.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "laser.h"
#include "colors.h"
#include "filetypes/checktypes.h"
#include "git/lgit.h"
#include "utils.h"
Expand Down Expand Up @@ -54,7 +55,8 @@ void laser_print_entry(const char *name, const char *color, char *indent,
if (depth > 0)
strcpy(branch, is_last ? "└─ " : "├─ ");

printf("%s%s%s%s%s\n", indent, branch, color, name, LASER_COLORS->reset);
printf("%s%s%s%s%s\n", indent, branch, color, name,
LASER_COLORS[LASER_COLOR_RESET].value);
}

void laser_process_entries(laser_opts opts, int depth, char *indent,
Expand Down Expand Up @@ -126,7 +128,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, LASER_COLORS->dir, indent,
laser_print_entry(entries[i]->d_name,
LASER_COLORS[LASER_COLOR_DIR].value, indent,
depth, is_last);

if (opts.show_tree)
Expand All @@ -149,33 +152,40 @@ 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, LASER_COLORS->symlink, indent,
depth, is_last);
laser_print_entry(res_string,
LASER_COLORS[LASER_COLOR_SYMLINK].value,
indent, depth, is_last);
}
}
else if (laser_is_filestat_exec(&file_stat))
laser_print_entry(entries[i]->d_name, LASER_COLORS->exec,
indent, depth, is_last);
laser_print_entry(entries[i]->d_name,
LASER_COLORS[LASER_COLOR_EXEC].value, indent,
depth, is_last);

else if (laser_checktype(full_path, laser_archiveformats))
laser_print_entry(entries[i]->d_name, LASER_COLORS->archive,
laser_print_entry(entries[i]->d_name,
LASER_COLORS[LASER_COLOR_ARCHIVE].value,
indent, depth, is_last);

else if (laser_checktype(full_path, laser_mediaformats))
laser_print_entry(entries[i]->d_name, LASER_COLORS->media,
indent, depth, is_last);
laser_print_entry(entries[i]->d_name,
LASER_COLORS[LASER_COLOR_MEDIA].value, indent,
depth, is_last);

else if (laser_checktype(full_path, laser_documentformats))
laser_print_entry(entries[i]->d_name, LASER_COLORS->documents,
laser_print_entry(entries[i]->d_name,
LASER_COLORS[LASER_COLOR_DOCUMENT].value,
indent, depth, is_last);

else if (entries[i]->d_name[0] == '.')
laser_print_entry(entries[i]->d_name, LASER_COLORS->hidden,
laser_print_entry(entries[i]->d_name,
LASER_COLORS[LASER_COLOR_HIDDEN].value,
indent, depth, is_last);

else if (S_ISREG(file_stat.st_mode))
laser_print_entry(entries[i]->d_name, LASER_COLORS->file,
indent, depth, is_last);
laser_print_entry(entries[i]->d_name,
LASER_COLORS[LASER_COLOR_FILE].value, indent,
depth, is_last);
}

free(entries[i]);
Expand Down

0 comments on commit ac8ca04

Please sign in to comment.