diff --git a/ansi2html.h b/ansi2html.h
index 21a1c09..6478c28 100644
--- a/ansi2html.h
+++ b/ansi2html.h
@@ -82,7 +82,7 @@ static int color_css(char* buf, size_t size, int32_t color, int is_bg) {
}
static char* vterm_render(const vterm_t* vt) {
- const size_t cap =
+ const size_t cap = 1024
+ (size_t)(vt->last_row + 1) * 4
+ (size_t)(vt->last_row + 1) * (vt->last_col + 1) *
(sizeof("&") - 1);
@@ -288,13 +288,14 @@ static int utf8_len(const unsigned char c) {
}
static char* ansi2html(const char* ansislop) {
- vterm_t vt = {0};
+ vterm_t* vt = malloc(sizeof(vterm_t));
+ memset(vt, 0, sizeof(vterm_t));
- vt.slop.fg = vt.slop.bg = -1;
+ vt->slop.fg = vt->slop.bg = -1;
for (int r = 0; r < VTERM_ROWS; r++) {
for (int c = 0; c < VTERM_COLS; c++) {
- vt.cells[r][c].slop.fg = vt.cells[r][c].slop.bg = -1;
- vt.cells[r][c].utf8[0] = ' ';
+ vt->cells[r][c].slop.fg = vt->cells[r][c].slop.bg = -1;
+ vt->cells[r][c].utf8[0] = ' ';
}
}
@@ -302,7 +303,7 @@ static char* ansi2html(const char* ansislop) {
if (*p == '\e') {
p++;
if (*p == '[') {
- p = vterm_csi(&vt, p + 1);
+ p = vterm_csi(vt, p + 1);
}
else if (*p == '(') {
p += 2;
@@ -313,19 +314,19 @@ static char* ansi2html(const char* ansislop) {
}
else if (*p == '\r') {
p++;
- vt.col = 0;
+ vt->col = 0;
}
else if (*p == '\n') {
p++;
- vt.col = 0;
- vt.row++;
+ vt->col = 0;
+ vt->row++;
}
else if (*p == '\t') {
p++;
- vt.col = (vt.col + 8) & ~7;
- if (vt.col >= VTERM_COLS) {
- vt.col = 0;
- vt.row++;
+ vt->col = (vt->col + 8) & ~7;
+ if (vt->col >= VTERM_COLS) {
+ vt->col = 0;
+ vt->row++;
}
}
else if ((uint8_t)*p < 0x20) {
@@ -333,9 +334,11 @@ static char* ansi2html(const char* ansislop) {
}
else {
const int l = utf8_len((unsigned char)*p);
- vterm_put(&vt, p, l);
+ vterm_put(vt, p, l);
p += l;
}
}
- return vterm_render(&vt);
+ char* html = vterm_render(vt);
+ free(vt);
+ return html;
}