From c0091a8b7a5efd7b0f4f714327710513802d4668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sun, 8 Nov 2020 19:34:34 +0100 Subject: fix inline bold/italic bugs --- tea.c | 65 ++++++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/tea.c b/tea.c index c0065be..0865a35 100644 --- a/tea.c +++ b/tea.c @@ -24,14 +24,15 @@ char *src; /* source text */ int src_l; int src_s; -int *types; /* type of each character in source text */ - /* types[src_i] = BREAK if last character of visual line */ - /* = HIDDEN if hidden inline */ - /* = NORMAL otherwise */ +int *types; /* type of each character in source text (see enums below) */ -#define NORMAL 0 -#define BREAK 1 -#define HIDDEN 2 +enum { + NORMAL = 0 << 0, + BREAK = 1 << 1, /* last character of visual line */ + HIDDEN = 1 << 2, + BOLD = 1 << 3, + ITALIC = 1 << 4 +}; int x, y; /* current cursor position */ int w, h; /* terminal width, height */ @@ -135,6 +136,7 @@ void addc(char c) { types[i] = 0; } src[++src_l] = c; + types[src_l] = 0; src[src_l + 1] = '\0'; } @@ -143,7 +145,7 @@ void delc() { } int main() { - bool dot; + bool bold, dot, italic; char c, *line, *p, *tmp; int i, line_s, r; struct winsize ws; @@ -223,32 +225,45 @@ int main() { x = 1; y = yorig; + bold = false; dot = false; + italic = false; while (read(ttyfd, &c, 1) != 0) { switch (c) { case '\033': /* escape */ read(ttyfd, &c, 1); - if (c == 'b') { + switch (c) { + case 'b': + if (bold) break; addc('\\'); hide(); addc('f'); hide(); addc('B'); hide(); - prn(CSI "1m"); - } else if (c == 'i') { + bold = true; + break; + case 'i': + if (italic) break; addc('\\'); hide(); addc('f'); hide(); addc('I'); hide(); - prn(CSI "3m"); - } else if (c == 'r') { + italic = true; + break; + case 'r': + case ' ': + if (!bold && !italic) break; addc('\\'); hide(); addc('f'); hide(); addc('R'); hide(); prn(CSI "0m"); - } else if (c == '[') { + bold = false; + italic = false; + break; + case '[': read(ttyfd, &c, 1); if (c == 'A') up(); if (c == 'B') down(); if (c == 'C') right(); if (c == 'D') left(); + break; } break; case 3: /* ctrl-c */ @@ -257,24 +272,32 @@ int main() { case 13: /* enter */ addc('\n'); breakline(); - prn(CSI "0m"); + dot = false; break; case 127: /* backspace */ if (x == 1) break; prn("\b"); + delc(); + if (src[src_l-2] == '\\' && src[src_l-1] == 'f') { + if (src[src_l] = 'B') + bold = false; + if (src[src_l] = 'I') + italic = false; + } while (types[src_l] == HIDDEN) delc(); - delc(); x--; - if (x == 1) - prn(CSI "0m"); + if (x == 1) dot = false; break; default: if (iscntrl(c)) break; - if (x == 1 && c == '.') - prn(CSI "2m"); + prn(CSI "0m"); + if (x == 1 && c == '.') dot = true; addc(c); - if (x + 1 > w) breakline(); + if (x > w) breakline(); + if (bold) prn(CSI "1m"); + if (dot) prn(CSI "2m"); + if (italic) prn(CSI "3m"); prn("%c", c); x++; break; -- cgit v1.2.3