diff options
| author | Berke Güzel <wenekar1@gmail.com> | 2026-10-15 11:37:31 +0300 |
|---|---|---|
| committer | Berke Güzel <wenekar1@gmail.com> | 2026-10-15 11:37:31 +0300 |
| commit | af6a37b1c4509c7766933a7cb45d2ee9a3a23f98 (patch) | |
| tree | e7b0294ebf0d9b124b8885a76c119d7f46d5b0fb /src | |
| parent | acd338ea33de36d223cc6d301712eed4a9501e6c (diff) | |
Return owned text and share cell attribute flags with snapshots
Make text queries return allocated strings released with term_text_free, keeping each capture atomic. Align public attributes with the write core, remove snapshot conversion tables, and migrate tests and the Kitty adapter.
Diffstat (limited to 'src')
| -rw-r--r-- | src/cell.h | 30 | ||||
| -rw-r--r-- | src/internal.h | 15 | ||||
| -rw-r--r-- | src/screen.c | 1 | ||||
| -rw-r--r-- | src/selection.c | 29 | ||||
| -rw-r--r-- | src/snapshot.c | 34 | ||||
| -rw-r--r-- | src/term.c | 23 | ||||
| -rw-r--r-- | src/text_run.h | 1 |
7 files changed, 35 insertions, 98 deletions
@@ -9,27 +9,25 @@ #include "term.h" enum cell_flags { - CELL_BOLD = 1 << 0, - CELL_ITALIC = 1 << 1, - CELL_UNDERLINE = 1 << 2, - CELL_STRIKE = 1 << 3, - CELL_OVERLINE = 1 << 4, - CELL_EMOJI = 1 << 5, - CELL_TEXT = 1 << 6, + CELL_BOLD = TERM_ATTR_BOLD, + CELL_ITALIC = TERM_ATTR_ITALIC, + CELL_UNDERLINE = TERM_ATTR_UNDERLINE, + CELL_STRIKE = TERM_ATTR_STRIKE, + CELL_OVERLINE = TERM_ATTR_OVERLINE, + CELL_EMOJI = TERM_ATTR_EMOJI, + CELL_TEXT = TERM_ATTR_TEXT, CELL_WIDE = 1 << 7, CELL_TRAIL = 1 << 8, CELL_WRAP_PAD = 1 << 9, - CELL_FAINT = 1 << 10, - CELL_DOUBLE_UNDERLINE = 1 << 11, - CELL_CURLY_UNDERLINE = 1 << 12, - CELL_DOTTED_UNDERLINE = 1 << 13, - CELL_DASHED_UNDERLINE = 1 << 14, - CELL_LINK = 1 << 15, + CELL_FAINT = TERM_ATTR_FAINT, + CELL_DOUBLE_UNDERLINE = TERM_ATTR_DOUBLE_UNDERLINE, + CELL_CURLY_UNDERLINE = TERM_ATTR_CURLY_UNDERLINE, + CELL_DOTTED_UNDERLINE = TERM_ATTR_DOTTED_UNDERLINE, + CELL_DASHED_UNDERLINE = TERM_ATTR_DASHED_UNDERLINE, + CELL_LINK = TERM_ATTR_LINK, }; -#define CELL_ANY_UNDERLINE \ - (CELL_UNDERLINE | CELL_DOUBLE_UNDERLINE | CELL_CURLY_UNDERLINE | \ - CELL_DOTTED_UNDERLINE | CELL_DASHED_UNDERLINE) +#define CELL_ANY_UNDERLINE TERM_ATTR_UNDERLINE_MASK /* c is a code point or a grapheme handle. pad holds the OSC 8 link id. */ struct cell { diff --git a/src/internal.h b/src/internal.h index d823018..8c02c91 100644 --- a/src/internal.h +++ b/src/internal.h @@ -364,21 +364,6 @@ static inline bool term_str_was_truncated(struct term *t) return truncated; } -/* Copy text into buf with the rules of term_selection_text(). NULL text - * copies as empty. Return the full length. */ -static inline size_t term_copy_out(const char *text, char *buf, size_t cap) -{ - if (!text) - text = ""; - size_t n = strlen(text); - if (buf && cap) { - size_t k = n < cap ? n : cap - 1; - if (k) memcpy(buf, text, k); - buf[k] = '\0'; - } - return n; -} - /* Every thread has its own anchor, so its address names the thread. */ extern _Thread_local char term_thread_anchor; diff --git a/src/screen.c b/src/screen.c index 1712faf..87d6f2c 100644 --- a/src/screen.c +++ b/src/screen.c @@ -829,7 +829,6 @@ static void tables_init(void) if (atomic_compare_exchange_strong(&tables_state, &expected, 1)) { term_isa_init(); term_text_tables_init(); - term_snapshot_tables_init(); atomic_store_explicit(&tables_state, 2, memory_order_release); return; } diff --git a/src/selection.c b/src/selection.c index 2727e8a..3ac2ee3 100644 --- a/src/selection.c +++ b/src/selection.c @@ -5,14 +5,6 @@ #include <stdlib.h> #include <string.h> -/* Copies a heap string into buf and frees it. Returns the length without the NUL. */ -static size_t text_out(char *text, char *buf, size_t cap) -{ - size_t n = term_copy_out(text, buf, cap); - free(text); - return n; -} - static char *link_at_text(const struct term *t, struct term_vector2i cell) { int col = cell.x, row = cell.y; @@ -62,14 +54,13 @@ static char *link_at_text(const struct term *t, struct term_vector2i cell) return out; } -size_t term_link_at(const struct term *t, struct term_vector2i cell, char *buf, size_t cap) +char *term_link_at(const struct term *t, struct term_vector2i cell) { - if (!t) - return 0; + if (!t) return NULL; term_write_begin((struct term *)t); char *text = link_at_text(t, cell); term_write_end((struct term *)t); - return text_out(text, buf, cap); + return text; } void term_select_clear(struct term *t) @@ -775,7 +766,7 @@ static char *range_text_inner(const struct term *t, return text; } -static char *history_text(struct term *t, int max_rows) +char *term_history_text(struct term *t, int max_rows) { if (!t) return NULL; @@ -822,17 +813,11 @@ static char *history_text(struct term *t, int max_rows) return text; } -size_t term_history_text(struct term *t, int max_rows, char *buf, size_t cap) +char *term_selection_text(const struct term *t) { - return text_out(history_text(t, max_rows), buf, cap); -} - -size_t term_selection_text(const struct term *t, char *buf, size_t cap) -{ - if (!t) - return 0; + if (!t) return NULL; term_write_begin((struct term *)t); char *text = range_text_inner(t, NULL); term_write_end((struct term *)t); - return text_out(text, buf, cap); + return text; } diff --git a/src/snapshot.c b/src/snapshot.c index 6578c9a..8dbb498 100644 --- a/src/snapshot.c +++ b/src/snapshot.c @@ -87,33 +87,6 @@ static void invalidate(struct term_snapshot *s) } } -/* Attribute contributions of the low and high byte of cell flags. */ -static uint16_t lo_attrs[256], hi_attrs[256]; -static uint8_t hi_underline[256]; -void term_snapshot_tables_init(void) -{ - for (int b = 0; b < 256; b++) { - uint16_t lo = 0, hi = 0; - if (b & CELL_BOLD) lo |= TERM_ATTR_BOLD; - if (b & CELL_ITALIC) lo |= TERM_ATTR_ITALIC; - if (b & CELL_STRIKE) lo |= TERM_ATTR_STRIKE; - if (b & CELL_OVERLINE) lo |= TERM_ATTR_OVERLINE; - if (b & CELL_EMOJI) lo |= TERM_ATTR_EMOJI; - if (b & CELL_TEXT) lo |= TERM_ATTR_TEXT; - uint16_t f = (uint16_t)(b << 8); - if (f & CELL_FAINT) hi |= TERM_ATTR_FAINT; - if (f & CELL_LINK) hi |= TERM_ATTR_LINK; - uint8_t ul = 0; - if (f & CELL_DOUBLE_UNDERLINE) ul = TERM_UNDERLINE_DOUBLE; - else if (f & CELL_CURLY_UNDERLINE) ul = TERM_UNDERLINE_CURLY; - else if (f & CELL_DOTTED_UNDERLINE) ul = TERM_UNDERLINE_DOTTED; - else if (f & CELL_DASHED_UNDERLINE) ul = TERM_UNDERLINE_DASHED; - lo_attrs[b] = lo; - hi_attrs[b] = hi; - hi_underline[b] = ul; - } -} - static uint32_t arena_add(struct term_snapshot *s, const struct gstore *g, uint32_t handle, bool *ok) { @@ -148,10 +121,6 @@ static uint32_t convert_row(struct term_snapshot *s, const struct gstore *g, for (int x = 0; x < cols; x++) { const struct cell *c = &src[x]; uint16_t f = c->flags; - uint8_t lo = (uint8_t)f, hi = (uint8_t)(f >> 8); - uint16_t ul = hi_underline[hi]; - if (!ul && (f & CELL_UNDERLINE)) - ul = TERM_UNDERLINE_SINGLE; uint32_t text = c->c; if (TERM_EXPECT(text < 0x20, 0)) text = text ? ' ' : 0; @@ -164,8 +133,7 @@ static uint32_t convert_row(struct term_snapshot *s, const struct gstore *g, .text = text, .fg = c->fg, .bg = c->bg, - .attrs = (uint16_t)(lo_attrs[lo] | hi_attrs[hi] | - ul << TERM_ATTR_UNDERLINE_SHIFT), + .attrs = (uint16_t)(f & ~(CELL_WIDE | CELL_TRAIL | CELL_WRAP_PAD)), .width = (f & CELL_TRAIL) ? 0 : (f & CELL_WIDE) ? 2 : 1, }; } @@ -104,22 +104,25 @@ void term_focus(struct term *t, bool focused) term_write_end(t); } -size_t term_cwd(const struct term *t, char *buf, size_t cap) +char *term_cwd(const struct term *t) { - if (!t) - return term_copy_out(NULL, buf, cap); + if (!t) return NULL; term_write_begin((struct term *)t); - size_t n = term_copy_out(t->cwd, buf, cap); + char *text = t->cwd ? term_strdup(t->cwd) : NULL; term_write_end((struct term *)t); - return n; + return text; } -size_t term_pointer_shape(const struct term *t, char *buf, size_t cap) +char *term_pointer_shape(const struct term *t) { - if (!t) - return term_copy_out(NULL, buf, cap); + if (!t) return NULL; term_write_begin((struct term *)t); - size_t n = term_copy_out(t->pointer_shape, buf, cap); + char *text = t->pointer_shape ? term_strdup(t->pointer_shape) : NULL; term_write_end((struct term *)t); - return n; + return text; +} + +void term_text_free(char *text) +{ + free(text); } diff --git a/src/text_run.h b/src/text_run.h index d546062..fac638f 100644 --- a/src/text_run.h +++ b/src/text_run.h @@ -21,7 +21,6 @@ struct width_cache { /* term_new() builds the shared tables before any terminal can use them. */ bool term_tables_ready(void); void term_text_tables_init(void); -void term_snapshot_tables_init(void); int term_text_class(uint32_t cp); |
