From ad98233066b72547aee7fa0c87838847ee7f1ece Mon Sep 17 00:00:00 2001 From: topcat001 Date: Tue, 15 Aug 2023 13:57:08 -0700 Subject: [PATCH] Better text placeholder. --- image.c | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/image.c b/image.c index 7135c6a3..20ef104a 100644 --- a/image.c +++ b/image.c @@ -19,6 +19,7 @@ #include #include +#include #include "tmux.h" @@ -50,6 +51,36 @@ image_free_all(struct screen *s) return (redraw); } +/* Create text placeholder for an image. */ +static void +image_fallback(char **ret, u_int sx, u_int sy) +{ + char *buf, *label; + u_int py, size, lsize; + + /* Placeholder label. */ + lsize = xasprintf(&label, "Sixel image (%ux%u)\n", sx, sy); + size = (sx + 2) * sy + 1; + if (lsize > size) + size = lsize; + + /* Each placeholder line has \r\n at the end. */ + *ret = buf = xmalloc(size); + for (py = 0; py < sy; py++) { + memset(buf, '+', sx); + buf += sx; + snprintf(buf, 3, "\r\n"); + buf += 2; + } + + /* Print the label.*/ + if (size == lsize) + memcpy(*ret, label, lsize); + else + memcpy(*ret, label, lsize - 1); + free(label); +} + struct image* image_store(struct screen *s, struct sixel_image *si) { @@ -63,8 +94,7 @@ image_store(struct screen *s, struct sixel_image *si) im->py = s->cy; sixel_size_in_cells(si, &im->sx, &im->sy); - /* XXX Fallback mode: This can be abstracted further. */ - xasprintf(&im->fallback, "Sixel image (%ux%u)\n", im->sx, im->sy); + image_fallback(&im->fallback, im->sx, im->sy); TAILQ_INSERT_TAIL(&s->images, im, entry); @@ -135,6 +165,9 @@ image_scroll_up(struct screen *s, u_int lines) im->py = 0; sixel_size_in_cells(im->data, &im->sx, &im->sy); + + free(im->fallback); + image_fallback(&im->fallback, im->sx, im->sy); redraw = 1; } return (redraw);