From 88c146a3951a650da73a52cc0c9ba1a4a7dd751f Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Sun, 7 May 2006 15:49:08 +0200 Subject: [PATCH] plain_renderer: cg-log -c | elinks works. The code needs cleanup --- src/document/plain/renderer.c | 57 +++++++++++++++++++++++++++++++++++ src/terminal/color.c | 2 +- src/terminal/color.h | 3 ++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/document/plain/renderer.c b/src/document/plain/renderer.c index a38bed96f..bc00fff09 100644 --- a/src/document/plain/renderer.c +++ b/src/document/plain/renderer.c @@ -6,6 +6,7 @@ #include #include +#include #include "elinks.h" @@ -226,6 +227,55 @@ print_document_link(struct plain_renderer *renderer, int lineno, return len; } +static inline int +change_colors(struct screen_char *template, unsigned char *text, struct document *document) +{ + unsigned char fg, bg, bold; + unsigned char *start = text; + +#if defined(CONFIG_88_COLORS) || defined(CONFIG_256_COLORS) + fg = template->color[0]; + bg = template->color[1]; +#else + fg = template->color[0] & 15; + bg = template->color[0] >> 4; +#endif + bold = template->attr & SCREEN_ATTR_BOLD; + for (start = text; *text && *text != 'm'; text++) { + switch (*text) { + case ';': + break; + case '3': + text++; + if (*text >= '0' && *text < '9') + fg = *text - '0'; + else + fg = 7; + break; + case '4': + text++; + if (*text >= '0' && *text < '9') + bg = *text - '0'; + else + bg = 0; + break; + case '1': + bold = SCREEN_ATTR_BOLD; + break; + case '0': + bold = 0; + break; + default: + break; + + } + } + fg |= bold; + set_term_color16(template, document->options.color_flags, fg, bg); + return (int)(text - start); + +} + static inline int add_document_line(struct plain_renderer *renderer, unsigned char *line, int line_width) @@ -292,6 +342,12 @@ add_document_line(struct plain_renderer *renderer, : '\0'; switch (line_char) { + case 27: + if (next_char != '[') goto normal; + line_pos += 2; + line_pos += change_colors(&saved_renderer_template, &line[line_pos], document); + *template = saved_renderer_template; + break; case ASCII_BS: if (!(expanded + line_pos)) { /* We've backspaced to the start of the line */ @@ -371,6 +427,7 @@ add_document_line(struct plain_renderer *renderer, break; } default: +normal: { int added_chars = 0; diff --git a/src/terminal/color.c b/src/terminal/color.c index 51b5011b3..aaeb69b98 100644 --- a/src/terminal/color.c +++ b/src/terminal/color.c @@ -225,7 +225,7 @@ static unsigned char fg_color[16][8] = { #define CMPCODE(c) (((c) << 1 | (c) >> 2) & TERM_COLOR_MASK) #define use_inverse(bg, fg) CMPCODE(fg & TERM_COLOR_MASK) < CMPCODE(bg) -static inline void +inline void set_term_color16(struct screen_char *schar, enum color_flags flags, unsigned char fg, unsigned char bg) { diff --git a/src/terminal/color.h b/src/terminal/color.h index f44995f64..e72066d15 100644 --- a/src/terminal/color.h +++ b/src/terminal/color.h @@ -55,6 +55,9 @@ enum color_mode { COLOR_MODES, /* XXX: Keep last */ }; +inline void set_term_color16(struct screen_char *schar, enum color_flags flags, + unsigned char fg, unsigned char bg); + /* Mixes the color pair and attributes to a terminal text color. */ /* If @flags has masked in the COLOR_INCREASE_CONTRAST the foreground color will * be adjusted. */