Expand tabs in print.

This commit is contained in:
Nicholas Marriott
2026-07-01 12:25:45 +01:00
parent 1853c220a4
commit 111a11ac6a

View File

@@ -453,6 +453,7 @@ screen_write_text(struct screen_write_ctx *ctx, u_int cx, u_int width,
va_list ap; va_list ap;
char *tmp; char *tmp;
u_int cy = s->cy, i, end, next, idx = 0, at, left; u_int cy = s->cy, i, end, next, idx = 0, at, left;
u_int w, pos, n;
struct utf8_data *text; struct utf8_data *text;
struct grid_cell gc; struct grid_cell gc;
@@ -472,9 +473,14 @@ screen_write_text(struct screen_write_ctx *ctx, u_int cx, u_int width,
for (end = idx; text[end].size != 0; end++) { for (end = idx; text[end].size != 0; end++) {
if (text[end].size == 1 && text[end].data[0] == '\n') if (text[end].size == 1 && text[end].data[0] == '\n')
break; break;
if (at + text[end].width > left) /* A tab expands to the next multiple of eight columns. */
if (text[end].size == 1 && text[end].data[0] == '\t')
w = 8 - (at % 8);
else
w = text[end].width;
if (at + w > left)
break; break;
at += text[end].width; at += w;
} }
/* /*
@@ -499,10 +505,21 @@ screen_write_text(struct screen_write_ctx *ctx, u_int cx, u_int width,
next = end; next = end;
} }
/* Print the line. */ /* Print the line, expanding any tabs to spaces. */
pos = 0;
for (i = idx; i < end; i++) { for (i = idx; i < end; i++) {
if (text[i].size == 1 && text[i].data[0] == '\t') {
n = 8 - (pos % 8);
utf8_set(&gc.data, ' ');
while (n-- > 0) {
screen_write_cell(ctx, &gc);
pos++;
}
continue;
}
utf8_copy(&gc.data, &text[i]); utf8_copy(&gc.data, &text[i]);
screen_write_cell(ctx, &gc); screen_write_cell(ctx, &gc);
pos += text[i].width;
} }
/* If at the bottom, stop. */ /* If at the bottom, stop. */