aboutsummaryrefslogtreecommitdiffstats
path: root/common.c
diff options
context:
space:
mode:
authorCameron Katri <me@cameronkatri.com>2021-05-30 15:59:03 -0400
committerCameron Katri <me@cameronkatri.com>2021-05-30 15:59:03 -0400
commitbba67091ac18bfcf886dc1221dea218aa119618e (patch)
treeb70347dc27e0afa933f184425d04b681ed9155b3 /common.c
parentc94bbb169889ec8827c373ebd6cfadf9f330218e (diff)
downloadopendircolors-bba67091ac18bfcf886dc1221dea218aa119618e.tar.gz
opendircolors-bba67091ac18bfcf886dc1221dea218aa119618e.tar.zst
opendircolors-bba67091ac18bfcf886dc1221dea218aa119618e.zip
Revamp to improve everything, and support D30547
https://reviews.freebsd.org/D30547
Diffstat (limited to 'common.c')
-rw-r--r--common.c65
1 files changed, 29 insertions, 36 deletions
diff --git a/common.c b/common.c
index afbb27e..bbabfc9 100644
--- a/common.c
+++ b/common.c
@@ -30,48 +30,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <err.h>
#include "common.h"
-char
-numtocol(char c, bool bold)
+struct color *
+parseansi(char *ansi, struct color *color)
{
- char buf = col[strtol(&c, NULL, 10)];
- if (bold)
- return (toupper(buf));
- else
- return (buf);
-}
+ long long num;
+ char *token;
+ const char *errstr;
+ bool bold = false, underline = false;
-char *
-tolscolors(char *dircolor)
-{
- char *ent, *buf, *val, *val2;
- char out[22] = "xxxxxxxxxxxxxxxxxxxxxx";
- bool bold = false;
+ color->fg = 'x';
+ color->bg = 'x';
- while ((ent = buf = strsep(&dircolor, ":")) != NULL) {
- for (int i = 0; i < 11; i++) {
- if (strncmp(ent, types[i], strlen(types[i])) == 0) {
- bold = false;
- while ((val = strsep(&buf, "=")) != NULL) {
- while ((val2 = strsep(&val, ";")) !=
- NULL) {
- if (strcmp(val2, "01") == 0) {
- bold = true;
- } else if (val2[0] == '3') {
- out[2 * i] = numtocol(
- val2[1], bold);
- } else if (val2[0] == '4') {
- out[2 * i + 1] =
- numtocol(
- val2[1], NULL);
- }
- }
- }
- }
+ while ((token = strsep(&ansi, ";")) != NULL) {
+ num = strtonum(token, 0, 47, NULL);
+ switch (num) {
+ case 1:
+ bold = true;
+ break;
+ case 4:
+ underline = true;
+ break;
+ default:
+ if (num <= 37)
+ color->fg = (char)((num - 30) + 97);
+ else if (num <= 47)
+ color->bg = (char)((num - 40) + 97);
}
}
- char *ret = strdup(out);
- return (ret);
+ if (bold)
+ color->fg = toupper(color->fg);
+ if (underline)
+ color->bg = toupper(color->bg);
+
+ return color;
}