summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/term/term.h14
-rw-r--r--include/term/term_input.h6
-rw-r--r--src/snapshot.c14
-rw-r--r--tests/term_test.c50
4 files changed, 72 insertions, 12 deletions
diff --git a/include/term/term.h b/include/term/term.h
index dc41114..d413db5 100644
--- a/include/term/term.h
+++ b/include/term/term.h
@@ -408,11 +408,16 @@ struct term_view {
const uint32_t *palette; /* TERM_PALETTE_SIZE ARGB entries */
};
+/* The view of s, NULL for a NULL s. The view and view->palette keep their
+ * address until term_snapshot_free(); their contents change on each update. */
TERM_API const struct term_view *term_snapshot_view(const struct term_snapshot *s);
-/* The cells of viewport row y: view->size.x entries. */
+/* The cells of viewport row y: view->size.x entries. NULL when y is outside
+ * the viewport or s is NULL. An update can move the cells, so the pointer is
+ * valid only until the next term_snapshot_update() or term_snapshot_free() of s. */
TERM_API const struct term_cell *term_snapshot_row(const struct term_snapshot *s, int y);
/* An identity for the content of row y. Two rows with the same key hold the
- * same cells, also when the content moved to another row. */
+ * same cells, also when the content moved to another row. 0 when y is outside
+ * the viewport or s is NULL. */
TERM_API uint64_t term_snapshot_row_key(const struct term_snapshot *s, int y);
/* Number of rows in history above the screen. 0 if alternate screen or no history. */
@@ -423,10 +428,13 @@ TERM_API int term_history_rows(const struct term *t);
* an empty string; NULL means allocation failed or t is NULL. Release the
* result with term_text_free(). */
TERM_API char *term_history_text(struct term *t, int max_rows);
+/* The selected columns of viewport row y, end exclusive. An empty span when y
+ * is outside the viewport or s is NULL. */
TERM_API struct term_span term_snapshot_row_selection(const struct term_snapshot *s, int y);
/* The code points of a cluster cell. Returns the number of code points and
- * writes at most cap of them. */
+ * writes at most cap of them. Writes nothing when cps is NULL or cap is 0.
+ * Returns 0 when s is NULL. */
TERM_API int term_snapshot_cluster(const struct term_snapshot *s, uint32_t text, uint32_t *cps, int cap);
/* The ARGB value of a color in a view. */
diff --git a/include/term/term_input.h b/include/term/term_input.h
index c317bea..19d9355 100644
--- a/include/term/term_input.h
+++ b/include/term/term_input.h
@@ -1,5 +1,5 @@
-#ifndef TERM_INPUT_TYPES_H
-#define TERM_INPUT_TYPES_H
+#ifndef TERM_INPUT_H
+#define TERM_INPUT_H
#include <stdbool.h>
#include <stddef.h>
@@ -199,4 +199,4 @@ struct term_input_result {
}
#endif
-#endif /* TERM_INPUT_TYPES_H */
+#endif /* TERM_INPUT_H */
diff --git a/src/snapshot.c b/src/snapshot.c
index 8dbb498..c578f18 100644
--- a/src/snapshot.c
+++ b/src/snapshot.c
@@ -314,34 +314,36 @@ bool term_snapshot_update(struct term_snapshot *s, struct term *t)
const struct term_view *term_snapshot_view(const struct term_snapshot *s)
{
- return &s->view;
+ return s ? &s->view : NULL;
}
const struct term_cell *term_snapshot_row(const struct term_snapshot *s, int y)
{
- if (y < 0 || y >= s->view.size.y)
+ if (!s || y < 0 || y >= s->view.size.y)
return NULL;
return s->cells + (size_t)y * s->view.size.x;
}
uint64_t term_snapshot_row_key(const struct term_snapshot *s, int y)
{
- if (y < 0 || y >= s->view.size.y)
+ if (!s || y < 0 || y >= s->view.size.y)
return 0;
return s->keys[y];
}
struct term_span term_snapshot_row_selection(const struct term_snapshot *s, int y)
{
- if (y < 0 || y >= s->view.size.y)
+ if (!s || y < 0 || y >= s->view.size.y)
return (struct term_span){ 0 };
return s->selection[y];
}
int term_snapshot_cluster(const struct term_snapshot *s, uint32_t text, uint32_t *cps, int cap)
{
+ if (!s)
+ return 0;
if (!(text & TERM_TEXT_CLUSTER)) {
- if (cap > 0)
+ if (cps && cap > 0)
cps[0] = text;
return 1;
}
@@ -350,7 +352,7 @@ int term_snapshot_cluster(const struct term_snapshot *s, uint32_t text, uint32_t
return 0;
int n = (int)s->arena[off];
int copy = n < cap ? n : cap;
- if (copy > 0)
+ if (cps && copy > 0)
memcpy(cps, &s->arena[off + 1], (size_t)copy * sizeof(*cps));
return n;
}
diff --git a/tests/term_test.c b/tests/term_test.c
index 66dbd96..b904148 100644
--- a/tests/term_test.c
+++ b/tests/term_test.c
@@ -1440,6 +1440,55 @@ static void test_snapshot_follows_new_terminal(void)
term_snapshot_free(s);
}
+/* The getters take a NULL snapshot and an out-of-range row like a NULL
+ * terminal: they return the empty value. The view keeps its address. */
+static void test_snapshot_null_and_range(void)
+{
+ uint32_t cps[4] = { 0xAAAA, 0xAAAA, 0xAAAA, 0xAAAA };
+ assert(term_snapshot_view(NULL) == NULL);
+ assert(term_snapshot_row(NULL, 0) == NULL);
+ assert(term_snapshot_row_key(NULL, 0) == 0);
+ struct term_span none = term_snapshot_row_selection(NULL, 0);
+ assert(none.begin == 0 && none.end == 0);
+ assert(term_snapshot_cluster(NULL, 'a', cps, 4) == 0);
+ assert(term_snapshot_cluster(NULL, TERM_TEXT_CLUSTER, cps, 4) == 0);
+ assert(cps[0] == 0xAAAA);
+
+ struct term *t = tt_new(4, 2);
+ tt_write(t, "e\xcc\x81", 3);
+ struct term_snapshot *s = term_snapshot_new();
+ assert(term_snapshot_update(s, t));
+
+ const struct term_view *view = term_snapshot_view(s);
+ const uint32_t *palette = view->palette;
+ assert(term_snapshot_update(s, t));
+ assert(term_snapshot_view(s) == view && view->palette == palette);
+
+ assert(term_snapshot_row(s, -1) == NULL);
+ assert(term_snapshot_row(s, 2) == NULL);
+ assert(term_snapshot_row_key(s, -1) == 0);
+ assert(term_snapshot_row_key(s, 2) == 0);
+ struct term_span off = term_snapshot_row_selection(s, 2);
+ assert(off.begin == 0 && off.end == 0);
+
+ /* A cluster cell gives its count with no buffer, and with cap 0 it
+ * writes nothing. */
+ uint32_t text = term_snapshot_row(s, 0)[0].text;
+ assert(text & TERM_TEXT_CLUSTER);
+ assert(term_snapshot_cluster(s, text, NULL, 0) == 2);
+ assert(term_snapshot_cluster(s, text, cps, 0) == 2);
+ assert(cps[0] == 0xAAAA);
+ assert(term_snapshot_cluster(s, text, NULL, 4) == 2);
+ /* A plain code point is one code point, and cap 0 writes nothing. */
+ assert(term_snapshot_cluster(s, 'a', NULL, 0) == 1);
+ assert(term_snapshot_cluster(s, 'a', cps, 0) == 1);
+ assert(cps[0] == 0xAAAA);
+ assert(term_snapshot_cluster(s, 'a', cps, 4) == 1 && cps[0] == 'a');
+
+ term_snapshot_free(s);
+ term_free(t);
+}
+
static void test_string_payloads(void)
{
static const char *prefixes[] = {"\033]0;", "\033]7;file://host/", "\033P"};
@@ -1859,6 +1908,7 @@ int main(void)
test_alt_shrink_then_flood();
test_alt_erase_scrollback();
test_snapshot_follows_new_terminal();
+ test_snapshot_null_and_range();
test_bulk_rows_survive_edits();
assert(!strcmp(UNICODE_DATA_VERSION, "18.0.0"));