From 4a0042ba1563b8d2f0669c7650e997b6c1a9d95e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 28 Jun 2020 09:16:43 +0200 Subject: [PATCH] sixel: insert: sort sixels such that those furthest up in the scrollback is at the back --- sixel.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/sixel.c b/sixel.c index 3c6d67af..ddb0091c 100644 --- a/sixel.c +++ b/sixel.c @@ -90,6 +90,25 @@ sixel_erase(struct terminal *term, struct sixel *sixel) static void sixel_insert(struct terminal *term, struct sixel sixel) { + const int scrollback_end + = (term->grid->offset + term->rows) & (term->grid->num_rows - 1); + + int end_row = sixel.pos.row + sixel.rows - scrollback_end; + while (end_row < 0) + end_row += term->grid->num_rows; + + tll_foreach(term->grid->sixel_images, it) { + int e = it->item.pos.row + it->item.rows - scrollback_end; + e -= scrollback_end; + while (e < 0) + e += term->grid->num_rows; + + if (e < end_row) { + tll_insert_before(term->grid->sixel_images, it, sixel); + return; + } + } + tll_push_back(term->grid->sixel_images, sixel); }