summaryrefslogtreecommitdiff
path: root/degesch.c
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2016-10-29 05:33:22 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2016-10-29 17:53:06 +0200
commit742d590b8ddd0ae13b2fbce50e96fcfa57cd088d (patch)
tree4504d18590f297b763d18be06accab13e8b03cc8 /degesch.c
parentb6528c73e3e38ef60daf12bb1c2737e235601fd1 (diff)
downloadxK-742d590b8ddd0ae13b2fbce50e96fcfa57cd088d.tar.gz
xK-742d590b8ddd0ae13b2fbce50e96fcfa57cd088d.tar.xz
xK-742d590b8ddd0ae13b2fbce50e96fcfa57cd088d.zip
degesch: simplify "attribute_printer"
Now that the line wrapper took over some of the state.
Diffstat (limited to 'degesch.c')
-rw-r--r--degesch.c62
1 files changed, 17 insertions, 45 deletions
diff --git a/degesch.c b/degesch.c
index f417e0b..4ac12a6 100644
--- a/degesch.c
+++ b/degesch.c
@@ -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);