diff options
author | Nicholas Marriott <nicm@cvs.openbsd.org> | 2019-03-14 09:53:53 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@cvs.openbsd.org> | 2019-03-14 09:53:53 +0000 |
commit | 04e3f7b6162f213f7635439d358717bff4f82bea (patch) | |
tree | e324042e4940d5731397b5e7e488e8847012854d /usr.bin/tmux/style.c | |
parent | 3e1c6f45ac8c7b82c51357ffb6067cb8c8bca08f (diff) |
Add a wrapper (struct style) around styles rather than using the
grid_cell directly. There will be some non-cell members soon.
Diffstat (limited to 'usr.bin/tmux/style.c')
-rw-r--r-- | usr.bin/tmux/style.c | 156 |
1 files changed, 99 insertions, 57 deletions
diff --git a/usr.bin/tmux/style.c b/usr.bin/tmux/style.c index 57828691ac8..b28883fb50c 100644 --- a/usr.bin/tmux/style.c +++ b/usr.bin/tmux/style.c @@ -1,4 +1,4 @@ -/* $OpenBSD: style.c,v 1.14 2017/03/22 07:16:54 nicm Exp $ */ +/* $OpenBSD: style.c,v 1.15 2019/03/14 09:53:52 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -23,27 +23,40 @@ #include "tmux.h" -/* Parse an embedded style of the form "fg=colour,bg=colour,bright,...". */ +/* Mask for bits not included in style. */ +#define STYLE_ATTR_MASK (~GRID_ATTR_CHARSET) + +/* Default style. */ +static struct style style_default = { + { 0, 0, 8, 8, { { ' ' }, 0, 1, 1 } } +}; + +/* + * Parse an embedded style of the form "fg=colour,bg=colour,bright,...". + * Note that this adds onto the given style, so it must have been initialized + * alredy. + */ int -style_parse(const struct grid_cell *defgc, struct grid_cell *gc, - const char *in) +style_parse(struct style *sy, const struct grid_cell *base, const char *in) { - struct grid_cell savedgc; - const char delimiters[] = " ,"; - char tmp[32]; - int val, fg, bg, attr, flags; - size_t end; + struct grid_cell *gc = &sy->gc; + struct grid_cell saved; + const char delimiters[] = " ,"; + char tmp[32]; + int value, fg, bg, attr, flags; + size_t end; if (*in == '\0') return (0); if (strchr(delimiters, in[strlen(in) - 1]) != NULL) return (-1); - memcpy(&savedgc, gc, sizeof savedgc); + memcpy(&saved, base, sizeof saved); fg = gc->fg; bg = gc->bg; attr = gc->attr; flags = gc->flags; + do { end = strcspn(in, delimiters); if (end > (sizeof tmp) - 1) @@ -52,39 +65,40 @@ style_parse(const struct grid_cell *defgc, struct grid_cell *gc, tmp[end] = '\0'; if (strcasecmp(tmp, "default") == 0) { - fg = defgc->fg; - bg = defgc->bg; - attr = defgc->attr; - flags = defgc->flags; + fg = base->fg; + bg = base->bg; + attr = base->attr; + flags = base->flags; } else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) { - if ((val = colour_fromstring(tmp + 3)) == -1) + if ((value = colour_fromstring(tmp + 3)) == -1) goto error; if (*in == 'f' || *in == 'F') { - if (val != 8) - fg = val; + if (value != 8) + fg = value; else - fg = defgc->fg; + fg = base->fg; } else if (*in == 'b' || *in == 'B') { - if (val != 8) - bg = val; + if (value != 8) + bg = value; else - bg = defgc->bg; + bg = base->bg; } else goto error; } else if (strcasecmp(tmp, "none") == 0) attr = 0; else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) { - if ((val = attributes_fromstring(tmp + 2)) == -1) + if ((value = attributes_fromstring(tmp + 2)) == -1) goto error; - attr &= ~val; + attr &= ~value; } else { - if ((val = attributes_fromstring(tmp)) == -1) + if ((value = attributes_fromstring(tmp)) == -1) goto error; - attr |= val; + attr |= value; } in += end + strspn(in + end, delimiters); } while (*in != '\0'); + gc->fg = fg; gc->bg = bg; gc->attr = attr; @@ -93,33 +107,35 @@ style_parse(const struct grid_cell *defgc, struct grid_cell *gc, return (0); error: - memcpy(gc, &savedgc, sizeof *gc); + memcpy(gc, &saved, sizeof *gc); return (-1); } /* Convert style to a string. */ const char * -style_tostring(struct grid_cell *gc) +style_tostring(struct style *sy) { - int off = 0, comma = 0; - static char s[256]; + struct grid_cell *gc = &sy->gc; + int off = 0; + const char *comma = ""; + static char s[256]; *s = '\0'; if (gc->fg != 8) { - off += xsnprintf(s, sizeof s, "fg=%s", colour_tostring(gc->fg)); - comma = 1; + off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", + comma, colour_tostring(gc->fg)); + comma = ","; } - if (gc->bg != 8) { off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", - comma ? "," : "", colour_tostring(gc->bg)); - comma = 1; + comma, colour_tostring(gc->bg)); + comma = ","; } - if (gc->attr != 0 && gc->attr != GRID_ATTR_CHARSET) { xsnprintf(s + off, sizeof s - off, "%s%s", - comma ? "," : "", attributes_tostring(gc->attr)); + comma, attributes_tostring(gc->attr)); + comma = ","; } if (*s == '\0') @@ -131,38 +147,64 @@ style_tostring(struct grid_cell *gc) void style_apply(struct grid_cell *gc, struct options *oo, const char *name) { - const struct grid_cell *gcp; + struct style *sy; memcpy(gc, &grid_default_cell, sizeof *gc); - gcp = options_get_style(oo, name); - gc->fg = gcp->fg; - gc->bg = gcp->bg; - gc->attr |= gcp->attr; + sy = options_get_style(oo, name); + gc->fg = sy->gc.fg; + gc->bg = sy->gc.bg; + gc->attr |= sy->gc.attr; } /* Apply a style, updating if default. */ void style_apply_update(struct grid_cell *gc, struct options *oo, const char *name) { - const struct grid_cell *gcp; - - gcp = options_get_style(oo, name); - if (gcp->fg != 8) - gc->fg = gcp->fg; - if (gcp->bg != 8) - gc->bg = gcp->bg; - if (gcp->attr != 0) - gc->attr |= gcp->attr; + struct style *sy; + + sy = options_get_style(oo, name); + if (sy->gc.fg != 8) + gc->fg = sy->gc.fg; + if (sy->gc.bg != 8) + gc->bg = sy->gc.bg; + if (sy->gc.attr != 0) + gc->attr |= sy->gc.attr; +} + +/* Initialize style from cell. */ +void +style_set(struct style *sy, const struct grid_cell *gc) +{ + memset(sy, 0, sizeof *sy); + memcpy(&sy->gc, gc, sizeof sy->gc); +} + +/* Copy style. */ +void +style_copy(struct style *dst, struct style *src) +{ + memcpy(dst, src, sizeof *dst); } /* Check if two styles are the same. */ int -style_equal(const struct grid_cell *gc1, const struct grid_cell *gc2) +style_equal(struct style *sy1, struct style *sy2) +{ + struct grid_cell *gc1 = &sy1->gc; + struct grid_cell *gc2 = &sy2->gc; + + if (gc1->fg != gc2->fg) + return (0); + if (gc1->bg != gc2->bg) + return (0); + if ((gc1->attr & STYLE_ATTR_MASK) != (gc2->attr & STYLE_ATTR_MASK)) + return (0); + return (1); +} + +/* Is this style default? */ +int +style_is_default(struct style *sy) { - return (gc1->fg == gc2->fg && - gc1->bg == gc2->bg && - (gc1->flags & ~GRID_FLAG_PADDING) == - (gc2->flags & ~GRID_FLAG_PADDING) && - (gc1->attr & ~GRID_ATTR_CHARSET) == - (gc2->attr & ~GRID_ATTR_CHARSET)); + return (style_equal(sy, &style_default)); } |