Skip to content

Commit

Permalink
Use LSR_COLORS environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
jmattaa authored Nov 5, 2024
2 parents cedb065 + 2f97dee commit f824413
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 45 deletions.
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
66 changes: 44 additions & 22 deletions include/colors.h
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>

#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
101 changes: 101 additions & 0 deletions src/colors.c
Original file line number Diff line number Diff line change
@@ -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);
}
30 changes: 15 additions & 15 deletions src/laser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit f824413

Please sign in to comment.