diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2017-04-16 18:01:07 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2017-04-16 18:02:41 -0400 |
commit | 8df078ada9045ffceb541e224985a4e4191e1526 (patch) | |
tree | 4452e27e1c9865e9a960bac4eb9179f211712a29 /color.c | |
parent | f32f83727bd2cef39998bcc9e74653d3a5ccd18f (diff) | |
download | bfs-8df078ada9045ffceb541e224985a4e4191e1526.tar.xz |
Make -help output prettier and more complete
Diffstat (limited to 'color.c')
-rw-r--r-- | color.c | 66 |
1 files changed, 53 insertions, 13 deletions
@@ -32,6 +32,15 @@ struct ext_color { struct colors { const char *reset; + const char *bold; + const char *gray; + const char *red; + const char *green; + const char *yellow; + const char *blue; + const char *magenta; + const char *cyan; + const char *normal; const char *file; const char *dir; @@ -69,20 +78,27 @@ struct color_name { static const struct color_name color_names[] = { COLOR_NAME("bd", block), + COLOR_NAME("bld", bold), + COLOR_NAME("blu", blue), COLOR_NAME("ca", capable), COLOR_NAME("cd", chardev), + COLOR_NAME("cyn", cyan), COLOR_NAME("di", dir), COLOR_NAME("do", door), COLOR_NAME("er", error), COLOR_NAME("ex", exec), COLOR_NAME("fi", file), + COLOR_NAME("grn", green), + COLOR_NAME("gry", gray), COLOR_NAME("ln", link), + COLOR_NAME("mag", magenta), COLOR_NAME("mh", multi_hard), COLOR_NAME("mi", missing), COLOR_NAME("no", normal), COLOR_NAME("or", orphan), COLOR_NAME("ow", ow), COLOR_NAME("pi", pipe), + COLOR_NAME("red", red), COLOR_NAME("rs", reset), COLOR_NAME("sg", setgid), COLOR_NAME("so", socket), @@ -90,14 +106,11 @@ static const struct color_name color_names[] = { COLOR_NAME("su", setuid), COLOR_NAME("tw", sticky_ow), COLOR_NAME("wr", warning), + COLOR_NAME("ylw", yellow), {0}, }; static const char **look_up_color(const struct colors *colors, const char *name) { - if (!colors) { - return NULL; - } - for (const struct color_name *entry = color_names; entry->name; ++entry) { if (strcmp(name, entry->name) == 0) { return (const char **)((char *)colors + entry->offset); @@ -129,8 +142,18 @@ struct colors *parse_colors(const char *ls_colors) { goto done; } + // From man console_codes + colors->reset = "0"; + colors->bold = "01"; + colors->gray = "01;30"; + colors->red = "01;31"; + colors->green = "01;32"; + colors->yellow = "01;33"; + colors->blue = "01;34"; + colors->magenta = "01;35"; + colors->cyan = "01;36"; + // Defaults generated by dircolors --print-database - colors->reset = "0"; colors->normal = NULL; colors->file = NULL; colors->dir = "01;34"; @@ -449,9 +472,6 @@ int cfprintf(CFILE *cfile, const char *format, ...) { va_list args; va_start(args, format); - char name[3] = {0}; - const char *esc; - for (const char *i = format; *i; ++i) { if (*i == '%') { switch (*++i) { @@ -482,16 +502,36 @@ int cfprintf(CFILE *cfile, const char *format, ...) { } break; - case '{': - memcpy(name, i + 1, 2); - esc = get_color(colors, name); - if (esc && print_esc(esc, file) != 0) { + case '{': { + ++i; + const char *end = strchr(i, '}'); + if (!end) { + goto invalid; + } + if (!colors) { + i = end; + break; + } + + size_t len = end - i; + char name[len + 1]; + memcpy(name, i, len); + name[len] = '\0'; + + const char *esc = get_color(colors, name); + if (!esc) { + goto invalid; + } + if (print_esc(esc, file) != 0) { goto done; } - i += 3; + + i = end; break; + } default: + invalid: errno = EINVAL; goto done; } |