From ab1f2ef71c48bc9617db0874ca21681679f4d678 Mon Sep 17 00:00:00 2001 From: nicm Date: Tue, 20 Jan 2026 16:32:05 +0000 Subject: [PATCH] Add a function to convert a screen to a string, from Michael Grant. --- screen.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tmux.h | 1 + 2 files changed, 72 insertions(+) diff --git a/screen.c b/screen.c index 12a55097..b65a9106 100644 --- a/screen.c +++ b/screen.c @@ -744,3 +744,74 @@ screen_mode_to_string(int mode) tmp[strlen(tmp) - 1] = '\0'; return (tmp); } + +const char * +screen_print(struct screen *s) +{ + static char *buf; + static size_t len = 16384; + const char *acs; + u_int x, y; + int n; + size_t last = 0; + struct utf8_data ud; + struct grid_line *gl; + struct grid_cell_entry *gce; + + if (buf == NULL) + buf = xmalloc(len); + + for (y = 0; y < screen_hsize(s) + s->grid->sy; y++) { + n = snprintf(buf + last, len - last, "%.4d \"", y); + if (n <= 0 || (u_int)n >= len - last) + goto out; + last += n; + + gl = &s->grid->linedata[y]; + for (x = 0; x < gl->cellused; x++) { + gce = &gl->celldata[x]; + if (gce->flags & GRID_FLAG_PADDING) + continue; + + if (~gce->flags & GRID_FLAG_EXTENDED) { + if (last + 2 >= len) + goto out; + buf[last++] = gce->data.data; + } else if (gce->flags & GRID_FLAG_TAB) { + if (last + 2 >= len) + goto out; + buf[last++] = '\t'; + } else if (gce->flags & GRID_ATTR_CHARSET) { + acs = tty_acs_get(NULL, gce->data.data); + if (acs != NULL) + n = strlen(acs); + else { + acs = &gce->data.data; + n = 1; + } + if (last + n + 1 >= len) + goto out; + memcpy(buf + last, acs, n); + last += n; + } else { + utf8_to_data(gl->extddata[gce->offset].data, + &ud); + if (ud.size > 0) { + if (last + ud.size + 1 >= len) + goto out; + memcpy(buf + last, ud.data, ud.size); + last += ud.size; + } + } + } + + if (last + 3 >= len) + goto out; + buf[last++] = '"'; + buf[last++] = '\n'; + } + +out: + buf[last] = '\0'; + return (buf); +} diff --git a/tmux.h b/tmux.h index e14113ff..e5145b10 100644 --- a/tmux.h +++ b/tmux.h @@ -3176,6 +3176,7 @@ void screen_select_cell(struct screen *, struct grid_cell *, void screen_alternate_on(struct screen *, struct grid_cell *, int); void screen_alternate_off(struct screen *, struct grid_cell *, int); const char *screen_mode_to_string(int); +const char *screen_print(struct screen *); /* window.c */ extern struct windows windows;