diff options
| author | user <user@clank> | 2026-08-14 14:13:33 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-08-14 14:13:33 +0200 |
| commit | 6e2786c0ca394a77fe9db22362d1932728c24012 (patch) | |
| tree | e91568a5f9c03fea841e1a7df14c13dbf18517c6 | |
| parent | sl0ppty: dashed borders and a deeper push-back while dragging (diff) | |
| download | slosh-6e2786c0ca394a77fe9db22362d1932728c24012.tar.gz slosh-6e2786c0ca394a77fe9db22362d1932728c24012.tar.xz slosh-6e2786c0ca394a77fe9db22362d1932728c24012.zip | |
sl0ppty: positional shaders, and a config block to ask for them
Six passes that are pure functions of where a cell is: vignette, gradient,
zebra, ruler, margin, spotlight. The position was already in the context and
nothing had used it; these are what it was for.
The one worth having on its own merits is `ruler`. A column guide that costs
no columns, needs no support from the program in the pane, and works in
anything — an editor that has never heard of one, a pager, a shell. It marks
the background only: a guide that recoloured the text would be making the code
it marks harder to read, which is the opposite of the job. `margin` is the
same idea from the other side, everything past a column receding so an
over-long line visibly runs out of the pane you meant to use.
`spotlight` is the first thing to need something new in the context. The
cursor belongs to whichever pane is focused, so it is handed only to that
pane — otherwise every other pane's light would chase a cursor somewhere else.
It leaves everything inside its radius completely alone, because dimming the
text you are actually working on to make it easier to find would be silly, and
ramps rather than stepping at the edge.
`vignette` and `spotlight` both measure distance with a row counting double,
because a cell is about twice as tall as it is wide and an effect that claims
to be round has to say so in cells. The layout's own centre-distance already
does this. The test for it is that on a 41x21 pane — square on screen, not in
cells — the top edge and the left edge come out equally dark.
Scanlines are not a seventh shader. A CRT scanline is `zebra` with a band of
one row, and shipping the same arithmetic under two names would be pretending
they are different things.
These are not policy, so they needed a way to be asked for, and a KDL block
whose children are the passes gets it without inventing a syntax: order in the
block is order in the chain, because a chain is a sequence, and listing a
shader twice is how you get two rulers. Unknown names are refused with a
warning rather than guessed at. Empty by default — these are yours to want,
not opinions we hold.
| -rw-r--r-- | README.md | 19 | ||||
| -rw-r--r-- | config/config.kdl | 28 | ||||
| -rw-r--r-- | src/app.c | 26 | ||||
| -rw-r--r-- | src/config.c | 38 | ||||
| -rw-r--r-- | src/config.h | 7 | ||||
| -rw-r--r-- | src/shader.c | 94 | ||||
| -rw-r--r-- | src/shader.h | 24 | ||||
| -rw-r--r-- | tests/shader_test.c | 164 | ||||
| -rw-r--r-- | tests/test_shaders.py | 54 |
9 files changed, 443 insertions, 11 deletions
@@ -82,7 +82,24 @@ like "copied 13 chars". title or the tab strip, so chrome stays legible over a pane that has been dimmed underneath it. `dim_unfocused` (off by default) fades every pane but the focused one; `drag_grayscale` and `drag_dim` are the greying and the -recession you get while dragging. All are strengths from 0 to 255. Because a cell's colour is often "whatever the +recession you get while dragging. All are strengths from 0 to 255. + +Beyond those the session runs itself, a `shaders { }` block is a chain you +choose, in the order you write it: + +```kdl +shaders { + vignette amount=70 // darken towards the edges + zebra amount=22 // band alternate rows + ruler amount=60 at=80 color="#ff5fd7" // a column guide, in any program + margin amount=90 at=100 // long lines run out of the pane + spotlight amount=90 radius=14 // brightness follows the cursor + gradient amount=40 direction=0 // fade down the pane +} +``` + +Each is a pure function of a cell's colour and where it sits, which is why a +ruler costs no columns and works inside a program that has never heard of one. Because a cell's colour is often "whatever the terminal calls default", shaders resolve those through `theme`'s `default_fg` and `default_bg` — set them to your terminal's real colours, or a shaded pane will shift as it turns on. Panes with no shader defer to your terminal exactly diff --git a/config/config.kdl b/config/config.kdl index 4a71c38..c4930bc 100644 --- a/config/config.kdl +++ b/config/config.kdl @@ -78,6 +78,34 @@ dim_unfocused 0 drag_grayscale 200 drag_dim 140 +// Colour passes every pane gets, in the order written — a chain is a sequence, +// so `grayscale` then `tint` is not `tint` then `grayscale`. Each node is one +// pass; `amount` is 0..255 and defaults to 128. Listing a shader twice is how +// you get two of it (two rulers, say). An unknown name is refused with a +// warning and skipped, never guessed at. +// +// dim amount darken towards black +// grayscale amount drain colour, weighted for the eye +// tint amount color="#rrggbb" pull towards a colour +// vignette amount darken towards the pane's edges +// gradient amount direction=0..3 fade towards the background +// 0 down, 1 up, 2 right, 3 left +// zebra amount band=1 darken alternate bands of `band` rows; +// band=1 is also a CRT scanline +// ruler amount at=80 color= mark a column, background only, so the +// text on it stays readable +// margin amount at=100 everything from that column recedes +// spotlight amount radius=12 brightness falls away from the cursor +// +// Empty by default: these are yours to ask for, not opinions we hold. +// +// shaders { +// vignette amount=70 +// zebra amount=22 +// ruler amount=60 at=80 color="#ff5fd7" +// spotlight amount=90 radius=14 +// } + // How long an announcement ("copied 13 chars", a pane's OSC 9 notification, // "config reloaded") stays in the bottom-right corner. toast_ms 2500 @@ -1499,22 +1499,36 @@ static size_t policy_shaders(app_t *a, node_t *n, shader_t *out, size_t cap) { /* A pane with nothing to apply costs nothing: no context is built and no cell * is visited, so an unshaded session emits the same bytes it did before. */ static void shade_leaf(app_t *a, screen_t *s, node_t *n) { - shader_t chain[SHADE_MAX + 2]; + shader_t chain[SHADE_CHAIN_MAX]; size_t nc = 0; - /* Attached first, policy last: a pane's own colour is the thing the - * session's opinion of the moment is then applied to. */ - for (size_t i = 0; i < n->nshaders && nc < SHADE_MAX; i++) + /* Configured first, then this pane's own, then policy last: what you asked + * every pane to look like, adjusted for this pane, and only then the + * session's opinion about this moment — which has to be able to grey out + * whatever the other two produced. */ + for (size_t i = 0; i < CFG.nshaders && nc < SHADE_CHAIN_MAX; i++) + chain[nc++] = CFG.shaders[i]; + for (size_t i = 0; i < n->nshaders && nc < SHADE_CHAIN_MAX; i++) chain[nc++] = n->shaders[i]; - nc += policy_shaders(a, n, &chain[nc], (sizeof chain / sizeof *chain) - nc); + nc += policy_shaders(a, n, &chain[nc], SHADE_CHAIN_MAX - nc); if (!nc) return; + bool focused = n == cur(a)->focus; shade_ctx_t base = { .now_ms = now_ms_(), - .focused = n == cur(a)->focus, + .focused = focused, .default_fg = CFG.default_fg, .default_bg = CFG.default_bg, }; + /* The screen's cursor belongs to whichever pane is focused, so it is only + * this pane's cursor when this pane is the focused one. Handing it to any + * other pane would have its spotlight chasing a cursor somewhere else. */ + if (focused && s->cursor_visible && s->cursor_x >= n->content.x && + s->cursor_y >= n->content.y) { + base.has_cursor = true; + base.cursor_x = (uint16_t)(s->cursor_x - n->content.x); + base.cursor_y = (uint16_t)(s->cursor_y - n->content.y); + } shade_apply(s, chain, nc, n->content.x, n->content.y, n->content.w, n->content.h, &base); } diff --git a/src/config.c b/src/config.c index 6c1650c..92ee560 100644 --- a/src/config.c +++ b/src/config.c @@ -270,6 +270,44 @@ bool config_load(config_t *c, const char *path, char *err, size_t errcap) { c->shell = strdup(sh); } + /* `shaders { vignette amount=60; ruler amount=70 at=80 }` — one node per + * pass, and the order they are written in is the order they run, because a + * chain is a sequence. A block that names nothing known leaves the previous + * set alone rather than silently clearing it. */ + const kdl_node_t *shaders = kdl_child(root, "shaders"); + if (shaders) { + c->nshaders = 0; + for (size_t i = 0; i < shaders->nkids && c->nshaders < SHADE_MAX; i++) { + const kdl_node_t *k = shaders->kids[i]; + if (!k || !k->name) continue; + + long amount = kdl_prop_int(k, "amount", 128); + if (amount < 0) amount = 0; + if (amount > 255) amount = 255; + /* Every shader that takes a number calls it something different, so + * accept each name rather than making you remember which is which. */ + long param = kdl_prop_int(k, "at", -1); + if (param < 0) param = kdl_prop_int(k, "radius", -1); + if (param < 0) param = kdl_prop_int(k, "band", -1); + if (param < 0) param = kdl_prop_int(k, "direction", -1); + if (param < 0) param = 0; + if (param > 65535) param = 65535; + + color_t col = c->frame_focus; /* a sensible default for `ruler` */ + const char *cs = kdl_prop(k, "color", NULL); + if (cs && !parse_color(cs, &col) && err && !err[0]) + snprintf(err, errcap, "bad colour for shader %s: %s", k->name, cs); + + if (!shader_make_p(&c->shaders[c->nshaders], k->name, col, + (uint8_t)amount, (uint16_t)param)) { + if (err && !err[0]) + snprintf(err, errcap, "unknown shader: %s", k->name); + continue; + } + c->nshaders++; + } + } + const kdl_node_t *theme = kdl_child(root, "theme"); if (theme) { struct { diff --git a/src/config.h b/src/config.h index eed50a6..a6bd2f2 100644 --- a/src/config.h +++ b/src/config.h @@ -11,6 +11,7 @@ #include <stddef.h> #include <stdint.h> +#include "shader.h" #include "sl0ppty.h" typedef enum { @@ -66,6 +67,12 @@ typedef struct { uint8_t dim_unfocused; uint8_t drag_grayscale; uint8_t drag_dim; + + /* Colour passes every pane gets, in the order they were written. Ordinary + * shaders rather than policy: the session has no opinion about these, you + * asked for them. */ + shader_t shaders[SHADE_MAX]; + size_t nshaders; bool status_bar; bool focus_follows_mouse; diff --git a/src/shader.c b/src/shader.c index ec49a07..6085eb4 100644 --- a/src/shader.c +++ b/src/shader.c @@ -44,6 +44,87 @@ static void sh_tint(const shader_t *sh, const shade_ctx_t *ctx, cell_t *c) { c->bg = mix(c->bg, sh->color, sh->amount); } +/* ---- positional --------------------------------------------------------- */ + +/* A terminal cell is about twice as tall as it is wide, so any effect that + * claims to be round has to say so in cells: a row counts double, exactly as + * the layout's own centre-distance does. */ +static int dist2(int dx, int dy) { return dx * dx + (dy * 2) * (dy * 2); } + +static void dim_by(cell_t *c, uint8_t a) { + const color_t black = {true, 0, 0, 0}; + c->fg = mix(c->fg, black, a); + c->bg = mix(c->bg, black, a); +} + +/* Darken towards the edges. Falloff is on squared distance, which is both + * cheaper than a square root and the curve a vignette wants anyway: flat + * across the middle, then gathering pace. */ +static void sh_vignette(const shader_t *sh, const shade_ctx_t *ctx, cell_t *c) { + int cx = ctx->cols / 2, cy = ctx->rows / 2; + int far = dist2(cx, cy); + if (far <= 0) return; + int d = dist2((int)ctx->x - cx, (int)ctx->y - cy); + if (d > far) d = far; + dim_by(c, (uint8_t)((int)sh->amount * d / far)); +} + +/* Fade across the pane towards the background, so a pane reads as a surface + * with a light on it rather than a rectangle of text. */ +static void sh_gradient(const shader_t *sh, const shade_ctx_t *ctx, cell_t *c) { + int span, pos; + switch (sh->param) { + case 1: span = ctx->rows; pos = ctx->rows - 1 - ctx->y; break; + case 2: span = ctx->cols; pos = ctx->x; break; + case 3: span = ctx->cols; pos = ctx->cols - 1 - ctx->x; break; + default: span = ctx->rows; pos = ctx->y; break; + } + if (span <= 1) return; + uint8_t a = (uint8_t)((int)sh->amount * pos / (span - 1)); + c->fg = mix(c->fg, ctx->default_bg, a); + c->bg = mix(c->bg, ctx->default_bg, a); +} + +/* Horizontal banding. `param` rows per band, every other band darkened — + * which at one row per band is also what a CRT scanline is, so there is one + * shader here and not two. */ +static void sh_zebra(const shader_t *sh, const shade_ctx_t *ctx, cell_t *c) { + uint16_t band = sh->param ? sh->param : 1; + if (((ctx->y / band) & 1) == 0) return; + dim_by(c, sh->amount); +} + +/* A column guide. Background only: a ruler that recoloured the text would be + * making the code it marks harder to read, which is the opposite of the job. */ +static void sh_ruler(const shader_t *sh, const shade_ctx_t *ctx, cell_t *c) { + if (ctx->x != sh->param) return; + c->bg = mix(c->bg, sh->color, sh->amount); +} + +/* Everything past a column recedes, so an over-long line visibly runs out of + * the part of the pane you meant to use. */ +static void sh_margin(const shader_t *sh, const shade_ctx_t *ctx, cell_t *c) { + if (ctx->x < sh->param) return; + dim_by(c, sh->amount); +} + +/* Brightness falls away from the cursor. Inside the radius nothing happens at + * all, so the text you are working on is never touched; beyond it the dimming + * ramps up over the same distance again rather than switching on at an edge. */ +static void sh_spotlight(const shader_t *sh, const shade_ctx_t *ctx, + cell_t *c) { + if (!ctx->has_cursor) return; + int r = sh->param ? sh->param : 10; + int inner = r * r; + int d = dist2((int)ctx->x - (int)ctx->cursor_x, + (int)ctx->y - (int)ctx->cursor_y); + if (d <= inner) return; + int ramp = inner * 3; + int over = d - inner; + int k = over >= ramp ? 255 : over * 255 / ramp; + dim_by(c, (uint8_t)((int)sh->amount * k / 255)); +} + static const struct { const char *name; shade_fn fn; @@ -51,14 +132,25 @@ static const struct { {"dim", sh_dim}, {"grayscale", sh_grayscale}, {"tint", sh_tint}, + {"vignette", sh_vignette}, + {"gradient", sh_gradient}, + {"zebra", sh_zebra}, + {"ruler", sh_ruler}, + {"margin", sh_margin}, + {"spotlight", sh_spotlight}, }; bool shader_make(shader_t *out, const char *kind, color_t color, uint8_t amount) { + return shader_make_p(out, kind, color, amount, 0); +} + +bool shader_make_p(shader_t *out, const char *kind, color_t color, + uint8_t amount, uint16_t param) { if (!out || !kind) return false; for (size_t i = 0; i < sizeof REGISTRY / sizeof *REGISTRY; i++) { if (strcmp(REGISTRY[i].name, kind) != 0) continue; - *out = (shader_t){REGISTRY[i].name, REGISTRY[i].fn, color, amount}; + *out = (shader_t){REGISTRY[i].name, REGISTRY[i].fn, color, amount, param}; return true; } return false; diff --git a/src/shader.h b/src/shader.h index 74fa2d7..74518fc 100644 --- a/src/shader.h +++ b/src/shader.h @@ -28,8 +28,13 @@ #include "sl0ppty.h" -/* Per pane. Small and fixed: a stack this deep is already a lot of passes. */ +/* Per pane, and separately per config. Small and fixed: a stack this deep is + * already a lot of passes over the same cells. */ #define SHADE_MAX 4 +/* What one pane can end up running: the ones every pane gets from the config, + * the ones attached to this pane, and the one or two the session derived for + * this frame. */ +#define SHADE_CHAIN_MAX (SHADE_MAX * 2 + 2) typedef struct { uint16_t x, y; /* cell position within the content rect, 0-based */ @@ -37,6 +42,12 @@ typedef struct { int64_t now_ms; /* for anything animated */ bool focused; + /* Where the cursor is, in the same rect-relative space as x/y. Only ever + * set for the pane that owns it, so an effect that follows the cursor does + * not chase another pane's. */ + bool has_cursor; + uint16_t cursor_x, cursor_y; + /* What "terminal default" means while shading. A cell whose colour is unset * is drawn in whatever the client's terminal calls default, and we never * learn that RGB — but most terminal text is default-coloured, so a shader @@ -54,11 +65,22 @@ struct shader { shade_fn fn; color_t color; /* the target colour, for shaders that have one */ uint8_t amount; /* strength, 0..255; 0 is identity, 255 is fully applied */ + /* One number whose meaning is the shader's own, because a second parameter + * that is a column for one effect and a radius for another is not really + * two things: + * ruler the column to mark margin first column to dim + * zebra rows per band spotlight radius in columns + * gradient 0 down, 1 up, 2 right, 3 left + */ + uint16_t param; }; /* Build a shader by registry name. False if the name is not a built-in. */ bool shader_make(shader_t *out, const char *kind, color_t color, uint8_t amount); +/* The same, for the shaders that take a number of their own. */ +bool shader_make_p(shader_t *out, const char *kind, color_t color, + uint8_t amount, uint16_t param); /* Iterate the registry: the i'th name, or NULL past the end. */ const char *shader_kind(size_t i); diff --git a/tests/shader_test.c b/tests/shader_test.c index ad3fbc4..959d0f9 100644 --- a/tests/shader_test.c +++ b/tests/shader_test.c @@ -51,6 +51,24 @@ static void run1(screen_t *s, const char *kind, color_t color, uint8_t amount) { shade_apply(s, &sh, 1, 0, 0, s->cols, s->rows, &base); } +static void run1p(screen_t *s, const char *kind, color_t color, uint8_t amount, + uint16_t param, const shade_ctx_t *ctx) { + shader_t sh; + if (!shader_make_p(&sh, kind, color, amount, param)) return; + shade_ctx_t base = ctx ? *ctx : base_ctx(); + shade_apply(s, &sh, 1, 0, 0, s->cols, s->rows, &base); +} + +/* Fills every cell mid-grey, so any change is a change the shader made. */ +static void fill(screen_t *s, color_t fg, color_t bg) { + for (uint16_t y = 0; y < s->rows; y++) + for (uint16_t x = 0; x < s->cols; x++) put(s, x, y, fg, bg); +} + +static uint8_t fg_at(screen_t *s, uint16_t x, uint16_t y) { + return screen_at(s, x, y)->fg.r; +} + /* A probe shader, to see what the pass tells a shader about where it is. */ static struct { uint16_t x, y, cols, rows; @@ -90,9 +108,19 @@ int main(void) { !shader_make(&sh, "bloom", (color_t){0}, 0), ""); ok("a NULL kind is refused", !shader_make(&sh, NULL, (color_t){0}, 0), ""); + ok("vignette is a built-in", + shader_make(&sh, "vignette", (color_t){0}, 0), ""); + ok("gradient is a built-in", + shader_make(&sh, "gradient", (color_t){0}, 0), ""); + ok("zebra is a built-in", shader_make(&sh, "zebra", (color_t){0}, 0), ""); + ok("ruler is a built-in", shader_make(&sh, "ruler", (color_t){0}, 0), ""); + ok("margin is a built-in", shader_make(&sh, "margin", (color_t){0}, 0), ""); + ok("spotlight is a built-in", + shader_make(&sh, "spotlight", (color_t){0}, 0), ""); + size_t n = 0; while (shader_kind(n)) n++; - ok("the registry enumerates every built-in", n == 3, ""); + ok("the registry enumerates every built-in", n == 9, ""); } /* ---- amount is the whole strength scale ---- */ @@ -296,7 +324,7 @@ int main(void) { for (uint16_t x = 0; x < 8; x++) put(&s, x, y, rgb(0x11, 0x22, 0x33), rgb(0, 0, 0)); - shader_t sh = {"probe", probe_fn, {0}, 0}; + shader_t sh = {"probe", probe_fn, {0}, 0, 0}; shade_ctx_t base = base_ctx(); base.now_ms = 1234; base.focused = true; @@ -321,6 +349,138 @@ int main(void) { screen_free(&s); } + /* ---- positional: vignette ---- */ + { + screen_init(&s, 41, 21); + fill(&s, rgb(0xff, 0xff, 0xff), rgb(0xff, 0xff, 0xff)); + run1(&s, "vignette", (color_t){0}, 255); + ok("the centre is untouched", fg_at(&s, 20, 10) == 0xff, + shown(screen_at(&s, 20, 10)->fg)); + ok("a corner is the darkest point", + fg_at(&s, 0, 0) < fg_at(&s, 10, 5) && fg_at(&s, 10, 5) < fg_at(&s, 20, 10), + shown(screen_at(&s, 0, 0)->fg)); + ok("it is symmetric left to right", fg_at(&s, 0, 10) == fg_at(&s, 40, 10), + ""); + ok("and top to bottom", fg_at(&s, 20, 0) == fg_at(&s, 20, 20), ""); + /* A cell is about twice as tall as it is wide, so this pane (41x21) is + * square on screen even though it is not square in cells. Its top edge and + * its left edge are therefore the same distance from the middle, and come + * out equally dark. Without the row doubling the left edge would be four + * times further by the maths and much darker — so this equality is the + * whole proof that the falloff is round in cells rather than in columns. */ + ok("a row counts double, so the falloff is round on screen", + fg_at(&s, 20, 0) == fg_at(&s, 0, 10), + shown(screen_at(&s, 20, 0)->fg)); + screen_free(&s); + } + + /* ---- positional: gradient ---- */ + { + screen_init(&s, 8, 8); + fill(&s, rgb(0xff, 0xff, 0xff), rgb(0xff, 0xff, 0xff)); + run1p(&s, "gradient", (color_t){0}, 255, 0, NULL); /* down */ + ok("a downward gradient leaves the top row alone", fg_at(&s, 0, 0) == 0xff, + shown(screen_at(&s, 0, 0)->fg)); + ok("and reaches the background by the bottom", fg_at(&s, 0, 7) == 0x00, + shown(screen_at(&s, 0, 7)->fg)); + ok("it is uniform across a row", fg_at(&s, 0, 3) == fg_at(&s, 7, 3), ""); + screen_free(&s); + + screen_init(&s, 8, 8); + fill(&s, rgb(0xff, 0xff, 0xff), rgb(0xff, 0xff, 0xff)); + run1p(&s, "gradient", (color_t){0}, 255, 2, NULL); /* rightward */ + ok("direction 2 runs left to right instead", + fg_at(&s, 0, 0) == 0xff && fg_at(&s, 7, 0) == 0x00, ""); + screen_free(&s); + } + + /* ---- positional: zebra ---- */ + { + screen_init(&s, 4, 6); + fill(&s, rgb(0xff, 0xff, 0xff), rgb(0xff, 0xff, 0xff)); + run1p(&s, "zebra", (color_t){0}, 255, 1, NULL); + ok("even rows are left alone", + fg_at(&s, 0, 0) == 0xff && fg_at(&s, 0, 2) == 0xff, ""); + ok("odd rows are darkened", + fg_at(&s, 0, 1) == 0x00 && fg_at(&s, 0, 3) == 0x00, ""); + screen_free(&s); + + screen_init(&s, 4, 8); + fill(&s, rgb(0xff, 0xff, 0xff), rgb(0xff, 0xff, 0xff)); + run1p(&s, "zebra", (color_t){0}, 255, 2, NULL); + ok("a band of 2 alternates every two rows", + fg_at(&s, 0, 0) == 0xff && fg_at(&s, 0, 1) == 0xff && + fg_at(&s, 0, 2) == 0x00 && fg_at(&s, 0, 3) == 0x00, + ""); + screen_free(&s); + } + + /* ---- positional: ruler ---- */ + { + screen_init(&s, 10, 3); + fill(&s, rgb(0xff, 0xff, 0xff), rgb(0x00, 0x00, 0x00)); + run1p(&s, "ruler", rgb(0xff, 0x00, 0x00), 255, 4, NULL); + ok("the marked column takes the colour", + ceq(screen_at(&s, 4, 0)->bg, 0xff, 0, 0), + shown(screen_at(&s, 4, 0)->bg)); + ok("down its whole height", ceq(screen_at(&s, 4, 2)->bg, 0xff, 0, 0), ""); + ok("its neighbours are untouched", + ceq(screen_at(&s, 3, 0)->bg, 0, 0, 0) && + ceq(screen_at(&s, 5, 0)->bg, 0, 0, 0), + ""); + /* A guide that recoloured the text would make the code it marks harder to + * read, which is the opposite of the job. */ + ok("the text on it keeps its own colour", + ceq(screen_at(&s, 4, 0)->fg, 0xff, 0xff, 0xff), + shown(screen_at(&s, 4, 0)->fg)); + screen_free(&s); + } + + /* ---- positional: margin ---- */ + { + screen_init(&s, 10, 2); + fill(&s, rgb(0xff, 0xff, 0xff), rgb(0xff, 0xff, 0xff)); + run1p(&s, "margin", (color_t){0}, 255, 6, NULL); + ok("columns before the margin are untouched", fg_at(&s, 5, 0) == 0xff, ""); + ok("the margin column itself recedes", fg_at(&s, 6, 0) == 0x00, ""); + ok("and everything past it", fg_at(&s, 9, 0) == 0x00, ""); + screen_free(&s); + } + + /* ---- positional: spotlight ---- */ + { + shade_ctx_t ctx = base_ctx(); + ctx.has_cursor = true; + ctx.cursor_x = 20; + ctx.cursor_y = 10; + + screen_init(&s, 41, 21); + fill(&s, rgb(0xff, 0xff, 0xff), rgb(0xff, 0xff, 0xff)); + run1p(&s, "spotlight", (color_t){0}, 255, 5, &ctx); + ok("the cell under the cursor is untouched", fg_at(&s, 20, 10) == 0xff, ""); + ok("and so is everything inside the radius", fg_at(&s, 24, 10) == 0xff, ""); + ok("just past the radius the light has begun to fall away", + fg_at(&s, 26, 10) < 0xff, shown(screen_at(&s, 26, 10)->fg)); + ok("and keeps falling with distance", + fg_at(&s, 26, 10) > fg_at(&s, 28, 10), ""); + ok("the falloff is a ramp, not an edge", + fg_at(&s, 25, 10) > fg_at(&s, 27, 10) && + fg_at(&s, 27, 10) > fg_at(&s, 29, 10), + ""); + ok("far enough out it is fully dark", fg_at(&s, 35, 10) == 0x00, ""); + screen_free(&s); + + /* With no cursor there is nothing to centre on, so it does nothing at all + * rather than guessing at the middle. */ + screen_init(&s, 41, 21); + fill(&s, rgb(0xff, 0xff, 0xff), rgb(0xff, 0xff, 0xff)); + shade_ctx_t nocur = base_ctx(); + run1p(&s, "spotlight", (color_t){0}, 255, 5, &nocur); + ok("without a cursor the spotlight does nothing", + fg_at(&s, 0, 0) == 0xff && fg_at(&s, 40, 20) == 0xff, ""); + screen_free(&s); + } + /* ---- degenerate input ---- */ { screen_init(&s, 4, 2); diff --git a/tests/test_shaders.py b/tests/test_shaders.py index adf029a..5620e78 100644 --- a/tests/test_shaders.py +++ b/tests/test_shaders.py @@ -365,6 +365,56 @@ def test_a_keystroke_mid_drag_clears_the_greying(): content_fg(snap, right) == GREEN, str(content_fg(snap, right))) +# ---- the config block ------------------------------------------------------ + +def test_config_shaders_reach_the_screen(): + """`shaders { ... }` is the surface these are actually driven from.""" + conf = 'shaders {\n ruler amount=255 at=4 color="#ff0000"\n}\n' + with Session(SH, cols=60, rows=12, config=cfg(conf)) as s: + s.settle(200) + p = s.pane() + snap = s.snapshot() + on = snap.style_at(p["content_x"] + 4, p["content_y"]) + off = snap.style_at(p["content_x"] + 3, p["content_y"]) + check("a configured ruler marks its column", + (on or {}).get("bg") == "#ff0000", str(on)) + check("and leaves the neighbouring column alone", + (off or {}).get("bg") != "#ff0000", str(off)) + + +def test_config_shaders_run_in_written_order(): + """Order in the block is order in the chain.""" + a = 'shaders {\n tint amount=255 color="#ff0000"\n tint amount=255 color="#0000ff"\n}\n' + b = 'shaders {\n tint amount=255 color="#0000ff"\n tint amount=255 color="#ff0000"\n}\n' + def last(conf): + with Session(SH, cols=50, rows=10, config=cfg(conf)) as s: + s.settle(200) + p = s.pane() + return content_fg(s.snapshot(), p) + check("the last shader written is the last one applied", + last(a) == "#0000ff" and last(b) == "#ff0000", + f"{last(a)} / {last(b)}") + + +def test_an_unknown_shader_is_refused_not_guessed(): + with Session(SH, cols=50, rows=10, + config=cfg('shaders {\n bloom amount=255\n}\n')) as s: + s.settle(200) + check("an unknown shader leaves the session running and unshaded", + s.alive() and content_fg(s.snapshot(), s.pane()) == GREEN, + str(content_fg(s.snapshot(), s.pane()))) + + +def test_positional_shaders_stay_off_the_chrome(): + conf = 'shaders {\n vignette amount=255\n zebra amount=255 band=1\n}\n' + with Session(SH, cols=60, rows=12, config=cfg(conf)) as s: + s.settle(200) + p = s.pane() + snap = s.snapshot() + check("a full-strength positional stack still leaves the frame alone", + frame_fg(snap, p) == "#ff5fd7", str(frame_fg(snap, p))) + + if __name__ == "__main__": test_off_by_default() test_dims_everything_but_the_focused_pane() @@ -381,4 +431,8 @@ if __name__ == "__main__": test_the_hovered_target_is_still_highlighted_over_the_dashes() test_a_drag_that_ends_off_a_pane_still_clears() test_a_keystroke_mid_drag_clears_the_greying() + test_config_shaders_reach_the_screen() + test_config_shaders_run_in_written_order() + test_an_unknown_shader_is_refused_not_guessed() + test_positional_shaders_stay_off_the_chrome() sys.exit(report()) |
