From bba67091ac18bfcf886dc1221dea218aa119618e Mon Sep 17 00:00:00 2001 From: Cameron Katri Date: Sun, 30 May 2021 15:59:03 -0400 Subject: Revamp to improve everything, and support D30547 https://reviews.freebsd.org/D30547 --- opendircolors.c | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'opendircolors.c') diff --git a/opendircolors.c b/opendircolors.c index 21855ba..9be81f6 100644 --- a/opendircolors.c +++ b/opendircolors.c @@ -26,10 +26,12 @@ * SUCH DAMAGE. */ +#include #include #include #include #include +#include #include #include #include @@ -37,9 +39,7 @@ #include "common.h" -void usage(const char *); - -#define MAXKEYLEN 21 +static void usage(const char *); static const struct option long_options[] = { /* no clang-format */ { "help", no_argument, NULL, 'h' }, /**/ @@ -62,6 +62,11 @@ static const char *short_types[38] = { "no", "no", "fi", "rs", "di", "ln", "ln", "lc", "lc", "rc", "rc", "ec", "ec", "su", "su", "sg", "sg", "st", "ow", "ow", "tw", "tw", "ca", "mh", "cl", NULL }; +static const int indexes[37] = { -1, -1, -1, -1, 0, 1, 1, + 1, -1, -1, 3, 3, 2, 5, 5, 6, 6, -1, 4, + -1, -1, -1, -1, -1, -1, 7, 7, 8, 8, -1, 10, + 10, 9, 9, -1, -1, -1 }; + int main(int argc, char **argv) { @@ -110,39 +115,46 @@ main(int argc, char **argv) char *line = NULL; size_t linecap = 0; ssize_t linelen; - char *out = strdup(""); + char *ls_out = strdup(""); + char lsout[22] = "xxxxxxxxxxxxxxxxxxxxxx"; + struct color color; + while ((linelen = getline(&line, &linecap, fd)) > 0) { if (*line == '#' || *line == '\n') continue; char fmttype[MAXKEYLEN] = "", val[MAXKEYLEN] = ""; sscanf(line, "%s %s\n", fmttype, val); if (*line == '.') { - sprintf(out + strlen(out), "*%s=%s:", fmttype, val); + sprintf(ls_out + strlen(ls_out), "*%s=%s:", fmttype, val); continue; } else if (*line == '*') { - sprintf(out + strlen(out), "%s=%s:", fmttype, val); + sprintf(ls_out + strlen(ls_out), "%s=%s:", fmttype, val); continue; } for (int i = 0; i < 37; i++) { if (strcmp(fmttype, long_types[i]) == 0) { - sprintf(out + strlen(out), - "%s=%s:", short_types[i], val); + sprintf(ls_out + strlen(ls_out), "%s=%s:", short_types[i], val); + parseansi(val, &color); + if (indexes[i] >= 0) { + lsout[2 * indexes[i]] = color.fg; + lsout[2 * indexes[i] + 1] = color.bg; + } break; } } } fclose(fd); - fprintf(stdout, "%s%s%s\n", prefix, out, suffix); - fprintf(stdout, "%s%s%s\n", lsprefix, tolscolors(out), lssuffix); + fprintf(stdout, "%s%s%s\n", prefix, ls_out, suffix); + fprintf(stdout, "%s%s%s\n", lsprefix, lsout, lssuffix); free(line); - free(out); + free(ls_out); return (0); } -void +static void usage(const char *progname) { char *path; -- cgit v1.2.3-56-ge451