degesch: simplify "attribute_printer"

Now that the line wrapper took over some of the state.
This commit is contained in:
Přemysl Eric Janouch 2016-10-29 05:33:22 +02:00
parent b6528c73e3
commit 742d590b8d
Signed by: p
GPG Key ID: B715679E3A361BE6
1 changed files with 17 additions and 45 deletions

View File

@ -2794,15 +2794,13 @@ enum
struct attribute_printer
{
struct app_context *ctx; ///< Application context
char **attrs; ///< Named attributes
FILE *stream; ///< Output stream
bool dirty; ///< Attributes are set
int want; ///< Desired attributes
int want_foreground; ///< Desired foreground color
int want_background; ///< Desired background color
};
#define ATTRIBUTE_PRINTER_INIT(ctx, stream) { ctx->attrs, stream, true }
static void
attribute_printer_tputs (struct attribute_printer *self, const char *attr)
{
@ -2819,31 +2817,18 @@ static void
attribute_printer_reset (struct attribute_printer *self)
{
if (self->dirty)
attribute_printer_tputs (self, self->ctx->attrs[ATTR_RESET]);
attribute_printer_tputs (self, self->attrs[ATTR_RESET]);
self->dirty = false;
}
static void
attribute_printer_init (struct attribute_printer *self,
struct app_context *ctx, FILE *stream)
{
self->ctx = ctx;
self->stream = stream;
self->dirty = true;
self->want = 0;
self->want_foreground = -1;
self->want_background = -1;
}
static void
attribute_printer_apply (struct attribute_printer *self, int attribute)
attribute_printer_apply_named (struct attribute_printer *self, int attribute)
{
attribute_printer_reset (self);
if (attribute != ATTR_RESET)
{
attribute_printer_tputs (self, self->ctx->attrs[attribute]);
attribute_printer_tputs (self, self->attrs[attribute]);
self->dirty = true;
}
}
@ -2905,16 +2890,14 @@ attribute_printer_decode_color (int color, bool *is_bright)
}
static void
attribute_printer_update (struct attribute_printer *self)
attribute_printer_apply (struct attribute_printer *self,
int attributes, int want_fg, int want_bg)
{
bool fg_is_bright;
int fg = attribute_printer_decode_color
(self->want_foreground, &fg_is_bright);
int fg = attribute_printer_decode_color (want_fg, &fg_is_bright);
bool bg_is_bright;
int bg = attribute_printer_decode_color
(self->want_background, &bg_is_bright);
int bg = attribute_printer_decode_color (want_bg, &bg_is_bright);
int attributes = self->want;
bool have_inverse = !!(attributes & ATTRIBUTE_INVERSE);
if (have_inverse)
{
@ -3720,14 +3703,8 @@ formatter_flush (struct formatter *self, FILE *stream, int flush_opts)
if (self->ctx->word_wrapping && !(flush_opts & FLUSH_OPT_NOWRAP))
line = line_wrap (line, g_terminal.columns);
// TODO: rewrite the sloppily hacked mess around attribute_printer;
// so far I just didn't want to break everything at once
struct attribute_printer state;
attribute_printer_init (&state, self->ctx, stream);
attribute_printer_reset (&state);
struct line_char_attrs attrs =
{ .fg = -1, .bg = -1, .named = ATTR_RESET, .text = 0 };
struct attribute_printer state = ATTRIBUTE_PRINTER_INIT (self->ctx, stream);
struct line_char_attrs attrs = {}; // Won't compare equal to anything
LIST_FOR_EACH (struct line_char, c, line)
{
if (attrs.fg != c->attrs.fg
@ -3735,17 +3712,12 @@ formatter_flush (struct formatter *self, FILE *stream, int flush_opts)
|| attrs.named != c->attrs.named
|| attrs.text != c->attrs.text)
{
if (c->attrs.named != -1)
attribute_printer_apply (&state, c->attrs.named);
else
{
state.want = c->attrs.text;
state.want_foreground = c->attrs.fg;
state.want_background = c->attrs.bg;
attribute_printer_reset (&state);
attribute_printer_update (&state);
}
attrs = c->attrs;
if (attrs.named != -1)
attribute_printer_apply_named (&state, attrs.named);
else
attribute_printer_apply (&state,
attrs.text, attrs.fg, attrs.bg);
}
fwrite (c->bytes, c->len, 1, stream);