From 8941f5dc629c99b183ec09e79b838dc10c86a0ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sun, 8 Nov 2020 01:27:06 +0100 Subject: handle manual line breaks, arbitrary input length --- tea.c | 57 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/tea.c b/tea.c index 5839ef9..940c310 100644 --- a/tea.c +++ b/tea.c @@ -24,9 +24,8 @@ char *src; /* source text */ int src_l; int src_s; -int *breaks; /* position of broken lines in source text */ - /* (1 where a character is displayed on a new line, - 0 otherwise.) */ +int *breaks; /* positions of visual line breaks in source text */ + /* i.e. breaks[src index] = 1 if line broken before, else 0 */ int x, y; /* current cursor position */ int w, h; /* terminal width, height */ @@ -101,31 +100,44 @@ int down() { } int right() {} int left() {} +void breakline() { + prn("\r\n"); + breaks[src_l + 1] = 1; + x = margin + 1; + prn(CSI "%dC", margin - 1); + if (y == h) yorig--; + else y++; +} + void addc(char c) { - if (c != '\n' && x + 1 > w) { - prn("\r\n"); - breaks[src_l + 1] = 1; - x = margin + 1; - prn(CSI "%dC", margin - 1); - if (y == h) yorig--; - else y++; - } else - x++; + char *tmp; + int i, *tmp2; + + if (c != '\n' && x + 1 > w) breakline(); + else x++; if (src_l + 2 > src_s) { - fprintf(stderr, "too big!\n"); - exit(1); + src_s += 50; + tmp = realloc(src, (src_s + 1) * sizeof(char)); + if (tmp == NULL) rerr(1, "realloc"); + src = tmp; + + tmp2 = realloc(breaks, src_s * sizeof(int)); + if (breaks == NULL) err(1, "malloc"); + breaks = tmp2; + for (i = src_s - 50; i < src_s; i++) + breaks[i] = 0; } src[++src_l] = c; src[src_l + 1] = '\0'; - prn("%c", c); } int main() { + bool dot; char c, *line, *p, *tmp; int i, line_s, r; struct winsize ws; - src_s = 1000; + src_s = 100; src = malloc((src_s + 1) * sizeof(char)); if (src == NULL) err(1, "malloc"); src_l = -1; @@ -197,9 +209,10 @@ int main() { /* restore original cursor position (CUP) */ prn(CSI "%d;%dH", yorig, xorig + margin - 1); - x = xorig + margin - 1; + x = margin + 1; y = yorig; + dot = false; while (read(ttyfd, &c, 1) != 0) { switch (c) { case '\033': /* escape */ @@ -214,9 +227,19 @@ int main() { case 3: /* ctrl-c */ goto quit; break; + case 13: /* enter */ + addc('\n'); + breakline(); + prn(CSI "0m"); + break; default: if (iscntrl(c)) break; + if (x == margin + 1 && c == '.') { + prn(CSI "%dD", margin - 1); + prn(CSI "2m"); + } addc(c); + prn("%c", c); break; } } -- cgit v1.2.3