-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
186 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |