mirror of
https://github.com/tmux/tmux.git
synced 2025-09-02 05:21:10 +00:00
Add a wrapper (struct style) around styles rather than using the
grid_cell directly. There will be some non-cell members soon.
This commit is contained in:
152
style.c
152
style.c
@ -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;
|
||||
struct style *sy;
|
||||
|
||||
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;
|
||||
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)
|
||||
{
|
||||
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));
|
||||
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 (style_equal(sy, &style_default));
|
||||
}
|
||||
|
Reference in New Issue
Block a user