conf: allow the user to configure a fixed cursor color

The default is to reverse the foreground/background colors in the cell
with the cursor.

But, if the user configures a specific set of cursor colors, those
will always be used, regardless of other cell attributes (dim, reverse
etc).

The cursor color is specified as two color values, 'text' and
'cursor'.

The block cursor uses the 'cursor' color as background, and the 'text'
color for the glyph.

All other cursor styles uses the 'cursor' color for the cursor, but
uses the cell's foreground color for the glyph (meaning,
dim/reverse/etc applies).
This commit is contained in:
Daniel Eklöf 2019-07-23 18:54:58 +02:00
parent a397c64efe
commit bf4847d3e0
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
5 changed files with 81 additions and 15 deletions

View file

@ -106,6 +106,30 @@ get_config_path(void)
return NULL;
}
static bool
str_to_color(const char *s, uint32_t *color, const char *path, int lineno)
{
if (s == NULL)
return false;
errno = 0;
char *end = NULL;
unsigned long res = strtoul(s, &end, 16);
if (errno != 0) {
LOG_ERRNO("%s:%d: invalid color: %s", path, lineno, s);
return false;
}
if (*end != '\0') {
LOG_ERR("%s:%d: invalid color: %s", path, lineno, s);
return false;
}
*color = res & 0xffffff;
return true;
}
static bool
parse_section_main(const char *key, const char *value, struct config *conf,
const char *path, unsigned lineno)
@ -163,21 +187,11 @@ parse_section_colors(const char *key, const char *value, struct config *conf,
return false;
}
errno = 0;
char *end = NULL;
unsigned long res = strtoul(value, &end, 16);
if (errno != 0) {
LOG_ERRNO("%s:%d: invalid color: %s", path, lineno, value);
uint32_t color_value;
if (!str_to_color(value, &color_value, path, lineno))
return false;
}
if (*end != '\0') {
LOG_ERR("%s:%d: invalid color: %s", path, lineno, value);
return false;
}
*color = res & 0xffffff;
*color = color_value;
return true;
}
@ -199,6 +213,26 @@ parse_section_cursor(const char *key, const char *value, struct config *conf,
}
}
else if (strcmp(key, "color") == 0) {
char *value_copy = strdup(value);
const char *text = strtok(value_copy, " ");
const char *cursor = strtok(NULL, " ");
uint32_t text_color, cursor_color;
if (text == NULL || cursor == NULL ||
!str_to_color(text, &text_color, path, lineno) ||
!str_to_color(cursor, &cursor_color, path, lineno))
{
LOG_ERR("%s:%d: invalid cursor colors: %s", path, lineno, value);
free(value_copy);
return false;
}
conf->cursor.color.text = 1 << 31 | text_color;
conf->cursor.color.cursor = 1 << 31 | cursor_color;
free(value_copy);
}
else {
LOG_ERR("%s:%d: invalid key: %s", path, lineno, key);
return false;
@ -388,6 +422,10 @@ config_load(struct config *conf)
.cursor = {
.style = CURSOR_BLOCK,
.color = {
.text = 0,
.cursor = 0,
},
},
};