Refactor code to get cell colors

This commit is contained in:
Kovid Goyal 2021-10-28 13:35:30 +05:30
parent 5f12fbc2ee
commit 93dbcab10a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
5 changed files with 28 additions and 25 deletions

View File

@ -583,6 +583,27 @@ left_shift(Line *self, PyObject *args) {
Py_RETURN_NONE;
}
static color_type
resolve_color(ColorProfile *cp, color_type val, color_type defval) {
switch(val & 0xff) {
case 1:
return cp->color_table[(val >> 8) & 0xff];
case 2:
return val >> 8;
default:
return defval;
}
}
bool
colors_for_cell(Line *self, ColorProfile *cp, index_type *x, color_type *fg, color_type *bg) {
if (*x >= self->xnum) return false;
if (*x > 0 && !self->gpu_cells[*x].attrs.width && self->gpu_cells[*x-1].attrs.width == 2) (*x)--;
*fg = resolve_color(cp, self->gpu_cells[*x].fg, *fg);
*bg = resolve_color(cp, self->gpu_cells[*x].bg, *bg);
return true;
}
char_type
line_get_char(Line *self, index_type at) {
char_type ch = self->cpu_cells[at].ch;

View File

@ -119,3 +119,4 @@ void historybuf_clear(HistoryBuf *self);
void mark_text_in_line(PyObject *marker, Line *line);
bool line_has_mark(Line *, uint16_t mark);
PyObject* as_text_generic(PyObject *args, void *container, get_line_func get_line, index_type lines, ANSIBuf *ansibuf);
bool colors_for_cell(Line *self, ColorProfile *cp, index_type *x, color_type *fg, color_type *bg);

View File

@ -1509,28 +1509,6 @@ screen_fake_move_cursor_to_position(Screen *self, index_type x, index_type y) {
return count > 0;
}
static color_type
resolve_color(ColorProfile *cp, color_type val, color_type defval) {
switch(val & 0xff) {
case 1:
return cp->color_table[(val >> 8) & 0xff];
case 2:
return val >> 8;
default:
return defval;
}
}
bool
resolve_cell_colors(Screen *self, index_type x, index_type y, color_type *fg, color_type *bg, color_type default_fg, color_type default_bg) {
if (x < self->columns && y < self->lines) {
linebuf_init_line(self->linebuf, y);
GPUCell *g = self->linebuf->line->gpu_cells + x;
*fg = resolve_color(self->color_profile, g->fg, default_fg);
*bg = resolve_color(self->color_profile, g->bg, default_bg);
return true;
} else { *fg = 0; *bg = 0; return false; }
}
// }}}
// Editing {{{

View File

@ -249,7 +249,6 @@ void screen_report_key_encoding_flags(Screen *self);
bool screen_detect_url(Screen *screen, unsigned int x, unsigned int y);
int screen_cursor_at_a_shell_prompt(const Screen *);
bool screen_fake_move_cursor_to_position(Screen *, index_type x, index_type y);
bool resolve_cell_colors(Screen *self, index_type x, index_type y, color_type *fg, color_type *bg, color_type default_fg, color_type default_bg);
#define DECLARE_CH_SCREEN_HANDLER(name) void screen_##name(Screen *screen);
DECLARE_CH_SCREEN_HANDLER(bell)
DECLARE_CH_SCREEN_HANDLER(backspace)

View File

@ -291,8 +291,12 @@ cell_update_uniform_block(ssize_t vao_idx, Screen *screen, int uniform_buffer, G
rd->cursor_fg_sprite_idx = UNDERLINE_IDX; break;
}
} else rd->cursor_fg_sprite_idx = UNFOCUSED_IDX;
color_type cell_fg, cell_bg;
resolve_cell_colors(screen, screen->cursor->x, screen->cursor->y, &cell_fg, &cell_bg, rd->default_fg, rd->default_bg);
color_type cell_fg = rd->default_fg, cell_bg = rd->default_bg;
if (screen->cursor->y < screen->lines) {
linebuf_init_line(screen->linebuf, screen->cursor->y);
index_type x = screen->cursor->x;
colors_for_cell(screen->linebuf->line, screen->color_profile, &x, &cell_fg, &cell_bg);
}
if (screen->color_profile->overridden.cursor_color.type == COLOR_IS_INDEX || screen->color_profile->overridden.cursor_color.type == COLOR_IS_RGB) {
// since the program is controlling the cursor color we hope it has chosen one
// that has good contrast with the text color of the cell