Skip to content

Commit

Permalink
curses ui: fix default color detection
Browse files Browse the repository at this point in the history
closes: #1209
  • Loading branch information
rnpnr committed Jan 5, 2025
1 parent dedb8b7 commit 75892aa
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 38 deletions.
24 changes: 4 additions & 20 deletions ui-terminal-curses.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ static CellColor color_terminal(Ui *ui, uint8_t index) {
}

static inline unsigned int color_pair_hash(short fg, short bg) {
if (fg == -1)
if (fg == CELL_COLOR_DEFAULT)
fg = COLORS;
if (bg == -1)
if (bg == CELL_COLOR_DEFAULT)
bg = COLORS + 1;
return fg * (COLORS + 2) + bg;
}
Expand All @@ -170,10 +170,6 @@ static short color_pair_get(short fg, short bg) {

if (!color2palette) {
pair_content(0, &default_fg, &default_bg);
if (default_fg == -1)
default_fg = CELL_COLOR_WHITE;
if (default_bg == -1)
default_bg = CELL_COLOR_BLACK;
has_default_colors = (use_default_colors() == OK);
color_pairs_max = MIN(MAX_COLOR_PAIRS, SHRT_MAX);
if (COLORS)
Expand All @@ -192,7 +188,7 @@ static short color_pair_get(short fg, short bg) {
bg = default_bg;
}

if (!color2palette || (fg == -1 && bg == -1))
if (!color2palette)
return 0;

unsigned int index = color_pair_hash(fg, bg);
Expand Down Expand Up @@ -292,18 +288,6 @@ static void ui_term_backend_free(Ui *term) {
endwin();
}

bool is_default_color(CellColor c) {
static bool is_default_color(CellColor c) {
return c == CELL_COLOR_DEFAULT;
}

static bool is_default_bg(CellColor c) {
if (change_colors == 1)
return c == default_bg;
return is_default_color(c);
}

static bool is_default_fg(CellColor c) {
if (change_colors == 1)
return c == default_fg;
return is_default_color(c);
}
8 changes: 0 additions & 8 deletions ui-terminal-vt100.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,3 @@ static void ui_term_backend_free(Ui *tui) {
static bool is_default_color(CellColor c) {
return c.index == ((CellColor) CELL_COLOR_DEFAULT).index;
}

static bool is_default_fg(CellColor c) {
return is_default_color(c);
}

static bool is_default_bg(CellColor c) {
return is_default_color(c);
}
30 changes: 20 additions & 10 deletions ui-terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@
/* helper macro for handling UiTerm.cells */
#define CELL_AT_POS(UI, X, Y) (((UI)->cells) + (X) + ((Y) * (UI)->width));

#define CELL_STYLE_DEFAULT (CellStyle){.fg = CELL_COLOR_DEFAULT, .bg = CELL_COLOR_DEFAULT, .attr = CELL_ATTR_NORMAL}

static bool is_default_fg(CellColor c) {
return is_default_color(c);
}

static bool is_default_bg(CellColor c) {
return is_default_color(c);
}

void ui_die(Ui *tui, const char *msg, va_list ap) {
ui_terminal_free(tui);
if (tui->termkey)
Expand Down Expand Up @@ -121,7 +131,8 @@ bool ui_style_define(Win *win, int id, const char *style) {
return false;
if (!style)
return true;
CellStyle cell_style = tui->styles[win->id * UI_STYLE_MAX + UI_STYLE_DEFAULT];

CellStyle cell_style = CELL_STYLE_DEFAULT;
char *style_copy = strdup(style), *option = style_copy;
while (option) {
while (*option == ' ')
Expand Down Expand Up @@ -185,7 +196,10 @@ static void ui_draw_string(Ui *tui, int x, int y, const char *str, Win *win, enu
debug("draw-string: [%d][%d]\n", y, x);
if (x < 0 || x >= tui->width || y < 0 || y >= tui->height)
return;
CellStyle style = tui->styles[(win ? win->id : 0)*UI_STYLE_MAX + style_id];

/* NOTE: the style that style_id refers to may contain unset values; we need to properly
* clear the cell first then go through ui_window_style_set to get the correct style */
CellStyle default_style = tui->styles[UI_STYLE_MAX * win->id + UI_STYLE_DEFAULT];
// FIXME: does not handle double width characters etc, share code with view.c?
Cell *cells = tui->cells + y * tui->width;
const size_t cell_size = sizeof(cells[0].data)-1;
Expand All @@ -197,8 +211,8 @@ static void ui_draw_string(Ui *tui, int x, int y, const char *str, Win *win, enu
len = MIN(len, cell_size);
strncpy(cells[x].data, str, len);
cells[x].data[len] = '\0';
cells[x].style = style;
x++;
cells[x].style = default_style;
ui_window_style_set(win, cells + x++, style_id);
}
}

Expand Down Expand Up @@ -342,7 +356,7 @@ void ui_draw(Ui *tui) {
for (Win *win = tui->windows; win; win = win->next)
ui_window_draw(win);
if (tui->info[0])
ui_draw_string(tui, 0, tui->height-1, tui->info, NULL, UI_STYLE_INFO);
ui_draw_string(tui, 0, tui->height-1, tui->info, tui->vis->win, UI_STYLE_INFO);
vis_event_emit(tui->vis, VIS_EVENT_UI_DRAW);
ui_term_backend_blit(tui);
}
Expand Down Expand Up @@ -453,11 +467,7 @@ bool ui_window_init(Ui *tui, Win *w, enum UiOption options) {

CellStyle *styles = &tui->styles[w->id * UI_STYLE_MAX];
for (int i = 0; i < UI_STYLE_MAX; i++) {
styles[i] = (CellStyle) {
.fg = CELL_COLOR_DEFAULT,
.bg = CELL_COLOR_DEFAULT,
.attr = CELL_ATTR_NORMAL,
};
styles[i] = CELL_STYLE_DEFAULT;
}

styles[UI_STYLE_CURSOR].attr |= CELL_ATTR_REVERSE;
Expand Down

0 comments on commit 75892aa

Please sign in to comment.