diff options
author | John Ankarström <john@ankarstrom.se> | 2020-11-08 01:27:06 +0100 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2020-11-08 01:27:06 +0100 |
commit | 8941f5dc629c99b183ec09e79b838dc10c86a0ea (patch) | |
tree | 091bb8d70e7592884add236f421097280c119632 /tea.c | |
parent | 0b53bf08b02736ca4facdc13099ae360bbe4a541 (diff) | |
download | tea-8941f5dc629c99b183ec09e79b838dc10c86a0ea.tar.gz |
handle manual line breaks, arbitrary input length
Diffstat (limited to 'tea.c')
-rw-r--r-- | tea.c | 57 |
1 files changed, 40 insertions, 17 deletions
@@ -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; } } |