diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2015-06-22 22:02:53 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2015-06-22 22:02:53 -0400 |
commit | d53c16e42c6ae47cadcbf9f401d42421d871befc (patch) | |
tree | 767f76978913462c1c8172716ec68c1caaa2a174 /color.c | |
parent | 1033a017c16ac7f239c2436fbd9cad433672a011 (diff) | |
download | bfs-d53c16e42c6ae47cadcbf9f401d42421d871befc.tar.xz |
Add support for per-extension colors.
Diffstat (limited to 'color.c')
-rw-r--r-- | color.c | 35 |
1 files changed, 33 insertions, 2 deletions
@@ -16,6 +16,14 @@ #include <stdlib.h> #include <string.h> +typedef struct ext_color ext_color; + +struct ext_color { + const char *ext; + const char *color; + ext_color *next; +}; + struct color_table { const char *reset; const char *normal; @@ -36,6 +44,8 @@ struct color_table { const char *ow; const char *sticky; const char *exec; + + ext_color *ext_list; }; color_table *parse_colors(char *ls_colors) { @@ -64,6 +74,7 @@ color_table *parse_colors(char *ls_colors) { colors->ow = "34;42"; colors->sticky = "37;44"; colors->exec = "01;32"; + colors->ext_list = NULL; if (!ls_colors) { goto done; @@ -71,6 +82,7 @@ color_table *parse_colors(char *ls_colors) { char *start = ls_colors; char *end; + ext_color *ext; for (end = strchr(start, ':'); *start && end; start = end + 1, end = strchr(start, ':')) { char *equals = strchr(start, '='); if (!equals) { @@ -167,9 +179,16 @@ color_table *parse_colors(char *ls_colors) { colors->sticky_ow = value; } break; - } - // TODO: Handle file extensions + case '*': + ext = malloc(sizeof(ext_color)); + if (ext) { + ext->ext = key + 1; + ext->color = value; + ext->next = colors->ext_list; + colors->ext_list = ext; + } + } } done: @@ -194,6 +213,18 @@ static const char *file_color(const color_table *colors, const char *filename, c } if (!color) { + size_t namelen = strlen(filename); + + for (ext_color *ext = colors->ext_list; ext; ext = ext->next) { + size_t extlen = strlen(ext->ext); + if (namelen >= extlen && strncmp(filename + namelen - extlen, ext->ext, extlen) == 0) { + color = ext->color; + break; + } + } + } + + if (!color) { color = colors->file; } |