Add a context for cell/palette/hyperlinks when drawing to tty to avoid

passing so much in parameters.
This commit is contained in:
nicm
2026-06-09 21:22:22 +00:00
parent 34dd42dc7f
commit d533d7c97c
10 changed files with 125 additions and 125 deletions

99
tty.c
View File

@@ -79,6 +79,10 @@ static int tty_check_overlay(struct tty *, u_int, u_int);
#define TTY_QUERY_TIMEOUT 5
#define TTY_REQUEST_LIMIT 30
static struct tty_style_ctx tty_default_style_ctx = {
&grid_default_cell, NULL, NULL
};
void
tty_create_log(void)
{
@@ -1410,7 +1414,7 @@ tty_draw_pane(struct tty *tty, const struct tty_ctx *ctx, u_int py)
if (rr->nx == 0)
continue;
tty_draw_line(tty, s, rr->px - ctx->xoff, py, rr->nx,
rr->px, ctx->yoff + py, &ctx->defaults, ctx->palette);
rr->px, ctx->yoff + py, &ctx->style_ctx);
}
return;
}
@@ -1421,7 +1425,7 @@ tty_draw_pane(struct tty *tty, const struct tty_ctx *ctx, u_int py)
if (rr->nx == 0)
continue;
tty_draw_line(tty, s, i + rr->px - x, py, rr->nx,
rr->px, ry, &ctx->defaults, ctx->palette);
rr->px, ry, &ctx->style_ctx);
}
}
}
@@ -1578,8 +1582,7 @@ tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
return;
}
tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
ctx->s->hyperlinks);
tty_default_attributes(tty, ctx->bg, &ctx->style_ctx);
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
@@ -1601,8 +1604,7 @@ tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
return;
}
tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
ctx->s->hyperlinks);
tty_default_attributes(tty, ctx->bg, &ctx->style_ctx);
tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
@@ -1612,8 +1614,7 @@ tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
void
tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
{
tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
ctx->s->hyperlinks);
tty_default_attributes(tty, ctx->bg, &ctx->style_ctx);
tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, ctx->n, ctx->bg);
}
@@ -1635,8 +1636,7 @@ tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
return;
}
tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
ctx->s->hyperlinks);
tty_default_attributes(tty, ctx->bg, &ctx->style_ctx);
tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
tty_margin_off(tty);
@@ -1663,8 +1663,7 @@ tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
return;
}
tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
ctx->s->hyperlinks);
tty_default_attributes(tty, ctx->bg, &ctx->style_ctx);
tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
tty_margin_off(tty);
@@ -1677,8 +1676,7 @@ tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
void
tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
{
tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
ctx->s->hyperlinks);
tty_default_attributes(tty, ctx->bg, &ctx->style_ctx);
tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->sx, ctx->bg);
}
@@ -1688,8 +1686,7 @@ tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
{
u_int nx = ctx->sx - ctx->ocx;
tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
ctx->s->hyperlinks);
tty_default_attributes(tty, ctx->bg, &ctx->style_ctx);
tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, nx, ctx->bg);
}
@@ -1697,8 +1694,7 @@ tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
void
tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
{
tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
ctx->s->hyperlinks);
tty_default_attributes(tty, ctx->bg, &ctx->style_ctx);
tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->ocx + 1, ctx->bg);
}
@@ -1724,8 +1720,7 @@ tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
return;
}
tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
ctx->s->hyperlinks);
tty_default_attributes(tty, ctx->bg, &ctx->style_ctx);
tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
tty_margin_pane(tty, ctx);
@@ -1756,8 +1751,7 @@ tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
return;
}
tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
ctx->s->hyperlinks);
tty_default_attributes(tty, ctx->bg, &ctx->style_ctx);
tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
tty_margin_pane(tty, ctx);
@@ -1797,8 +1791,7 @@ tty_cmd_scrollup(struct tty *tty, const struct tty_ctx *ctx)
return;
}
tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
ctx->s->hyperlinks);
tty_default_attributes(tty, ctx->bg, &ctx->style_ctx);
tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
tty_margin_pane(tty, ctx);
@@ -1838,8 +1831,7 @@ tty_cmd_scrolldown(struct tty *tty, const struct tty_ctx *ctx)
return;
}
tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
ctx->s->hyperlinks);
tty_default_attributes(tty, ctx->bg, &ctx->style_ctx);
tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
tty_margin_pane(tty, ctx);
@@ -1858,8 +1850,7 @@ tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
{
u_int px, py, nx, ny;
tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
ctx->s->hyperlinks);
tty_default_attributes(tty, ctx->bg, &ctx->style_ctx);
tty_region_pane(tty, ctx, 0, ctx->sy - 1);
tty_margin_off(tty);
@@ -1883,8 +1874,7 @@ tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
{
u_int px, py, nx, ny;
tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
ctx->s->hyperlinks);
tty_default_attributes(tty, ctx->bg, &ctx->style_ctx);
tty_region_pane(tty, ctx, 0, ctx->sy - 1);
tty_margin_off(tty);
@@ -1908,8 +1898,7 @@ tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
{
u_int px, py, nx, ny;
tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
ctx->s->hyperlinks);
tty_default_attributes(tty, ctx->bg, &ctx->style_ctx);
tty_region_pane(tty, ctx, 0, ctx->sy - 1);
tty_margin_off(tty);
@@ -1934,8 +1923,7 @@ tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
return;
}
tty_attributes(tty, &grid_default_cell, &ctx->defaults, ctx->palette,
ctx->s->hyperlinks);
tty_attributes(tty, &grid_default_cell, &ctx->style_ctx);
tty_region_pane(tty, ctx, 0, ctx->sy - 1);
tty_margin_off(tty);
@@ -1968,7 +1956,7 @@ tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
vis += r->ranges[i].nx;
if (vis < gcp->data.width) {
tty_draw_line(tty, s, s->cx, s->cy, gcp->data.width,
px, py, &ctx->defaults, ctx->palette);
px, py, &ctx->style_ctx);
return;
}
}
@@ -1983,8 +1971,7 @@ tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
tty_invalidate(tty);
tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
tty_cell(tty, ctx->cell, &ctx->defaults, ctx->palette,
ctx->s->hyperlinks);
tty_cell(tty, ctx->cell, &ctx->style_ctx);
if (ctx->flags & TTY_CTX_CELL_INVALIDATE)
tty_invalidate(tty);
@@ -2020,8 +2007,7 @@ tty_cmd_cells(struct tty *tty, const struct tty_ctx *ctx)
tty_margin_off(tty);
tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
tty_attributes(tty, ctx->cell, &ctx->defaults, ctx->palette,
ctx->s->hyperlinks);
tty_attributes(tty, ctx->cell, &ctx->style_ctx);
/* Get tty position from pane position for overlay check. */
px = ctx->xoff + ctx->ocx - ctx->wox;
@@ -2098,8 +2084,7 @@ tty_cmd_syncstart(struct tty *tty, const struct tty_ctx *ctx)
void
tty_cell(struct tty *tty, const struct grid_cell *gc,
const struct grid_cell *defaults, struct colour_palette *palette,
struct hyperlinks *hl)
const struct tty_style_ctx *style_ctx)
{
const struct grid_cell *gcp;
@@ -2119,7 +2104,7 @@ tty_cell(struct tty *tty, const struct grid_cell *gc,
/* Check the output codeset and apply attributes. */
gcp = tty_check_codeset(tty, gc);
tty_attributes(tty, gcp, defaults, palette, hl);
tty_attributes(tty, gcp, style_ctx);
/* If it is a single character, write with putc to handle ACS. */
if (gcp->data.size == 1) {
@@ -2464,19 +2449,22 @@ tty_hyperlink(struct tty *tty, const struct grid_cell *gc,
void
tty_attributes(struct tty *tty, const struct grid_cell *gc,
const struct grid_cell *defaults, struct colour_palette *palette,
struct hyperlinks *hl)
const struct tty_style_ctx *style_ctx)
{
struct grid_cell *tc = &tty->cell, gc2;
int changed;
/* Use default style if not given. */
if (style_ctx == NULL)
style_ctx = &tty_default_style_ctx;
/* Copy cell and update default colours. */
memcpy(&gc2, gc, sizeof gc2);
if (~gc->flags & GRID_FLAG_NOPALETTE) {
if (gc2.fg == 8)
gc2.fg = defaults->fg;
gc2.fg = style_ctx->defaults->fg;
if (gc2.bg == 8)
gc2.bg = defaults->bg;
gc2.bg = style_ctx->defaults->bg;
}
/* Ignore cell if it is the same as the last one. */
@@ -2503,9 +2491,9 @@ tty_attributes(struct tty *tty, const struct grid_cell *gc,
}
/* Fix up the colours if necessary. */
tty_check_fg(tty, palette, &gc2);
tty_check_bg(tty, palette, &gc2);
tty_check_us(tty, palette, &gc2);
tty_check_fg(tty, style_ctx->palette, &gc2);
tty_check_bg(tty, style_ctx->palette, &gc2);
tty_check_us(tty, style_ctx->palette, &gc2);
/*
* If any bits are being cleared or the underline colour is now default,
@@ -2561,7 +2549,7 @@ tty_attributes(struct tty *tty, const struct grid_cell *gc,
tty_putcode(tty, TTYC_SMACS);
/* Set hyperlink if any. */
tty_hyperlink(tty, gc, hl);
tty_hyperlink(tty, gc, style_ctx->hyperlinks);
memcpy(&tty->last_cell, &gc2, sizeof tty->last_cell);
}
@@ -2583,8 +2571,9 @@ tty_colours(struct tty *tty, const struct grid_cell *gc)
*/
if (COLOUR_DEFAULT(gc->fg) || COLOUR_DEFAULT(gc->bg)) {
/*
* If don't have AX, send sgr0. This resets both colours to default.
* Otherwise, try to set the default colour only as needed.
* If don't have AX, send sgr0. This resets both colours to
* default. Otherwise, try to set the default colour only as
* needed.
*/
if (!tty_term_flag(tty->term, TTYC_AX))
tty_reset(tty);
@@ -2950,14 +2939,14 @@ tty_default_colours(struct grid_cell *gc, struct window_pane *wp)
}
void
tty_default_attributes(struct tty *tty, const struct grid_cell *defaults,
struct colour_palette *palette, u_int bg, struct hyperlinks *hl)
tty_default_attributes(struct tty *tty, u_int bg,
const struct tty_style_ctx *style_ctx)
{
struct grid_cell gc;
memcpy(&gc, &grid_default_cell, sizeof gc);
gc.bg = bg;
tty_attributes(tty, &gc, defaults, palette, hl);
tty_attributes(tty, &gc, style_ctx);
}
static void