diff --git a/Makefile b/Makefile index 064c7e0..b4762c0 100644 --- a/Makefile +++ b/Makefile @@ -18,9 +18,11 @@ all: aha aha: aha.c $(CC) $(CFLAGS) $(LDFLAGS) $(CPPFLAGS) aha.c -o $@ +test_print: test_print.c + $(CC) $(CFLAGS) $(LDFLAGS) $(CPPFLAGS) test_print.c -o $@ clean: - rm -f aha + rm -f aha aha_test install: aha ifeq ($(platform), Darwin) diff --git a/aha.1 b/aha.1 index 290c7e0..a957484 100644 --- a/aha.1 +++ b/aha.1 @@ -21,6 +21,10 @@ The options are as follows: .Bl -tag -width Ds .It Fl b , Fl Fl black Style HTML output to use a black background with white text. +.It Fl Fl colors +Style HTML output to use a black background with white text. +Set a custom color scheme. X must be a comma-separated list of 18 CSS colors representing the default colors and all 16 4-bit color codes: +"foreground, background, black, red, green, yellow, blue, magenta, cyan, white, (+ bright versions of those eight)" .It Fl c Ar file , Fl Fl css Ar file Adds the given css .Ar file @@ -96,6 +100,12 @@ Create an HTML file with a white background using the output of .Pp .Dl $ diff -u --color=always oldfile.c newfile.c | aha > diff.html .Pp +Create an HTML file with a white foreground, blue background, and mapping every 4-bit color code to red using +.Nm Ap s +help: +.Pp +.Dl $ aha -h | aha --colors '#fff,#00f,red,red,red,red,red,red,red,red,red,red,red,red,red,red,red,red' > aha-help.html +.Pp Create an HTML file with a black background from the output of .Xr htop 1 . You have to use option diff --git a/aha.c b/aha.c index cdffb11..6601c22 100644 --- a/aha.c +++ b/aha.c @@ -148,6 +148,63 @@ void deleteParse(pelem elem) } } +typedef struct ColorScheme_t { + char* fg_default; + char* bg_default; + char* colors[18]; // last two elements are fg, bg + char* data; +} ColorScheme; + +ColorScheme *parseColorScheme(const char* text) +{ + ColorScheme *scheme = malloc(sizeof(struct ColorScheme_t)); + scheme->data = strdup(text); + int i = -2; + + char *token = scheme->data; + while(1) + { + if(i == 16) // too many elements + { + free(scheme->data); + free(scheme); + return NULL; + } + + if(i == -2) + scheme->fg_default = token; + else if(i == -1) + scheme->bg_default = token; + else + scheme->colors[i] = token; + + i++; + + token += strcspn(token, ","); + if(*token != 0) + *token++ = 0; // insert null terminator + else + break; // we're at the end of the string + } + if(i < 16) // too few elements + { + free(scheme->data); + free(scheme); + return NULL; + } + + scheme->colors[16] = scheme->fg_default; + scheme->colors[17] = scheme->bg_default; + + return scheme; +} + +void deleteColorScheme(ColorScheme *style) +{ + free(style->data); + free(style); +} + void printHtml(char *text) { while(1) { switch(*text) { @@ -165,14 +222,8 @@ void printHtml(char *text) { } } -enum ColorScheme { - SCHEME_WHITE, - SCHEME_BLACK, - SCHEME_PINK -}; - struct Options { - enum ColorScheme colorscheme; + ColorScheme *scheme; char* filename; FILE *fp; int htop_fix; @@ -235,10 +286,34 @@ void make_rgb (int color_id, char str_rgb[12]){ #define VERSION_PRINTF_MAKRO \ printf("\033[1;31mAnsi Html Adapter\033[0m Version "AHA_VERSION"\n"); +const char* white_scheme = + // Foreground, background + "black,white," + // Black, red, green, yellow, blue, magenta, cyan, white + "dimgray,red,green,olive,blue,purple,teal,gray," + // Bright black, red, green, yellow, blue, magenta, cyan, white + "dimgray,red,green,olive,blue,purple,cyan,white"; + +const char* black_scheme = + // Foreground, background + "white,black," + // Black, red, green, yellow, blue, magenta, cyan, white + "black,red,lime,yellow,#3333FF,fuchsia,aqua,white," + // Bright black, red, green, yellow, blue, magenta, cyan, white + "black,red,lime,yellow,#3333FF,fuchsia,aqua,white"; + +const char* pink_scheme = + // Foreground, background + "black,pink," + // Black, red, green, yellow, blue, magenta, cyan, white + "dimgray,red,green,olive,blue,purple,teal,gray," + // Bright black, red, green, yellow, blue, magenta, cyan, white + "dimgray,red,green,olive,blue,purple,cyan,white"; + struct Options parseArgs(int argc, char* args[]) { struct Options opts = (struct Options){ - .colorscheme = SCHEME_WHITE, + .scheme = parseColorScheme(white_scheme), .filename = NULL, .fp = stdin, .htop_fix = 0, @@ -267,6 +342,11 @@ struct Options parseArgs(int argc, char* args[]) printf("\033[4mOptions\033[0m:\n"); printf(" --black, -b: \033[1;30m\033[1;47mBlack\033[0m Background and \033[1;37mWhite\033[0m \"standard color\"\n"); printf(" --pink, -p: \033[1;35mPink\033[0m Background\n"); + printf(" --colors X : Set a custom color scheme. X must be a comma-separated\n"); + printf(" list of 18 CSS colors representing the default colors\n"); + printf(" and all 16 4-bit color codes: \"\033[1mforeground,background,\n"); + printf(" black,red,green,yellow,blue,magenta,cyan,white,\n"); + printf(" (+ bright versions of those eight)\033[0m\"\n"); printf(" --style X, -y X: Set the style used in the
element\n"); printf(" --stylesheet, -s: Use a stylesheet instead of inline styles\n"); printf(" --iso X, -i X: Uses ISO 8859-X instead of utf-8. X must be 1..16\n"); @@ -297,6 +377,12 @@ struct Options parseArgs(int argc, char* args[]) printf("\n"); printf(" \033[1;3;33m$\033[0m diff -u --color=always oldfile.c newfile.c | \033[1maha\033[0m > diff.html\n"); printf("\n"); + printf(" Create an HTML file with a white foreground, blue background, and\n"); + printf(" mapping every 4-bit color code to red using \033[1maha\033[0m's help:\n"); + printf("\n"); + printf(" \033[1;3;33m$\033[0m \033[1maha\033[0m -h | \033[1maha\033[0m --colors \"#fff,#00f,red,red,red,red,red,red,red,\\\n"); + printf(" red,red,red,red,red,red,red,red,red\" > \033[1maha\033[0m-help.html\n"); + printf("\n"); printf(" Create an HTML file with a black background from the output of \033[3mhtop\033[0m.\n"); printf(" You have to use option -l due the other new-line-commands \033[3mhtop\033[0m uses:\n"); printf("\n"); @@ -345,10 +431,33 @@ struct Options parseArgs(int argc, char* args[]) opts.word_wrap=1; else if ((strcmp(args[p],"--black")==0) || (strcmp(args[p],"-b")==0)) - opts.colorscheme=SCHEME_BLACK; + { + deleteColorScheme(opts.scheme); + opts.scheme = parseColorScheme(black_scheme); + } else if ((strcmp(args[p],"--pink")==0) || (strcmp(args[p],"-p")==0)) - opts.colorscheme=SCHEME_PINK; + { + deleteColorScheme(opts.scheme); + opts.scheme = parseColorScheme(pink_scheme); + } + else + if ((strcmp(args[p],"--colors")==0)) + { + if (p+1>=argc) + { + fprintf(stderr,"No colors given!\n"); + exit(EXIT_FAILURE); + } + deleteColorScheme(opts.scheme); + opts.scheme = parseColorScheme(args[p + 1]); + if (opts.scheme == NULL) + { + fprintf(stderr,"Invalid color scheme '%s'. Scheme must be exactly 18 comma-separated values.\n", args[p+1]); + exit(EXIT_FAILURE); + } + p++; + } else if ((strcmp(args[p],"--stylesheet")==0) || (strcmp(args[p],"-s")==0)) opts.stylesheet=1; @@ -436,7 +545,7 @@ struct Options parseArgs(int argc, char* args[]) } enum ColorMode { - MODE_3BIT, + MODE_4BIT, MODE_8BIT, MODE_24BIT }; @@ -450,15 +559,14 @@ struct State { int crossedout; enum ColorMode fc_colormode; enum ColorMode bc_colormode; - int highlighted; //for fc AND bc although not correct... }; void swapColors(struct State *const state) { - if (state->bc_colormode == MODE_3BIT && state->bc == -1) - state->bc = 8; + if (state->bc_colormode == MODE_4BIT && state->bc == -1) + state->bc = 17; - if (state->fc_colormode == MODE_3BIT && state->fc == -1) - state->fc = 9; + if (state->fc_colormode == MODE_4BIT && state->fc == -1) + state->fc = 16; int temp = state->bc; state->bc = state->fc; @@ -478,9 +586,8 @@ const struct State default_state = { .underline = 0, .blink = 0, .crossedout = 0, - .fc_colormode = MODE_3BIT, - .bc_colormode = MODE_3BIT, - .highlighted = 0, + .fc_colormode = MODE_4BIT, + .bc_colormode = MODE_4BIT, }; int statesDiffer(const struct State *const old, const struct State *const new) { @@ -493,11 +600,9 @@ int statesDiffer(const struct State *const old, const struct State *const new) { (old->blink != new->blink) || (old->crossedout != new->crossedout) || (old->fc_colormode != new->fc_colormode) || - (old->bc_colormode != new->bc_colormode) || - (old->highlighted != new->highlighted); + (old->bc_colormode != new->bc_colormode); } - void printHeader(const struct Options *opts) { char encoding[16] = "UTF-8"; @@ -553,69 +658,52 @@ void printHeader(const struct Options *opts) printf("