aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2020-11-08 19:04:25 +0100
committerJohn Ankarström <john@ankarstrom.se>2020-11-08 19:04:25 +0100
commita0d2bf8bebb6dd4121a2a00b7ee7c340c0dfea9f (patch)
tree623a342afe1e434b43d0e6fb96bb3eff9b954922
parent831f0f71f280dba7923861433bdea03d4f030a3c (diff)
downloadtea-a0d2bf8bebb6dd4121a2a00b7ee7c340c0dfea9f.tar.gz
support hidden escape codes for bold, italic
-rw-r--r--tea.c82
1 files changed, 51 insertions, 31 deletions
diff --git a/tea.c b/tea.c
index 3797c21..c0065be 100644
--- a/tea.c
+++ b/tea.c
@@ -24,12 +24,17 @@ char *src; /* source text */
int src_l;
int src_s;
-int *breaks; /* positions of visual line breaks in source text */
- /* i.e. breaks[src index] = 1 if line broken before, else 0 */
+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 */
+
+#define NORMAL 0
+#define BREAK 1
+#define HIDDEN 2
int x, y; /* current cursor position */
int w, h; /* terminal width, height */
-int margin = 10;
int current = -1; /* currently selected item */
int xorig, yorig; /* original cursor position in cols, rows */
@@ -102,11 +107,15 @@ int left() {}
void breakline() {
prn("\r\n");
- breaks[src_l + 1] = 1;
- x = margin;
+ types[src_l] = 1;
+ x = 1;
if (y == h) yorig--;
else y++;
- prn(CSI "%d;%dH", y, margin);
+ prn(CSI "1G");
+}
+
+void hide() {
+ types[src_l] = HIDDEN;
}
void addc(char c) {
@@ -119,11 +128,11 @@ void addc(char c) {
if (tmp == NULL) rerr(1, "realloc");
src = tmp;
- tmp2 = realloc(breaks, src_s * sizeof(int));
- if (breaks == NULL) err(1, "malloc");
- breaks = tmp2;
+ tmp2 = realloc(types, src_s * sizeof(int));
+ if (types == NULL) err(1, "malloc");
+ types = tmp2;
for (i = src_s - 50; i < src_s; i++)
- breaks[i] = 0;
+ types[i] = 0;
}
src[++src_l] = c;
src[src_l + 1] = '\0';
@@ -144,10 +153,10 @@ int main() {
if (src == NULL) err(1, "malloc");
src_l = -1;
- breaks = malloc(src_s * sizeof(int));
- if (breaks == NULL) err(1, "malloc");
+ types = malloc(src_s * sizeof(int));
+ if (types == NULL) err(1, "malloc");
for (i = 0; i < src_s; i++)
- breaks[i] = 0;
+ types[i] = 0;
ttyfd = open("/dev/tty", O_RDWR);
if (ttyfd == -1) rerr(1, "open");
@@ -210,8 +219,8 @@ int main() {
y = y - r;
/* restore original cursor position (CUP) */
- prn(CSI "%d;%dH", yorig, margin);
- x = margin;
+ prn(CSI "%d;1H", yorig);
+ x = 1;
y = yorig;
dot = false;
@@ -219,12 +228,28 @@ int main() {
switch (c) {
case '\033': /* escape */
read(ttyfd, &c, 1);
- if (c != '[') break;
- read(ttyfd, &c, 1);
- if (c == 'A') up();
- if (c == 'B') down();
- if (c == 'C') right();
- if (c == 'D') left();
+ if (c == 'b') {
+ addc('\\'); hide();
+ addc('f'); hide();
+ addc('B'); hide();
+ prn(CSI "1m");
+ } else if (c == 'i') {
+ addc('\\'); hide();
+ addc('f'); hide();
+ addc('I'); hide();
+ prn(CSI "3m");
+ } else if (c == 'r') {
+ addc('\\'); hide();
+ addc('f'); hide();
+ addc('R'); hide();
+ prn(CSI "0m");
+ } else if (c == '[') {
+ read(ttyfd, &c, 1);
+ if (c == 'A') up();
+ if (c == 'B') down();
+ if (c == 'C') right();
+ if (c == 'D') left();
+ }
break;
case 3: /* ctrl-c */
goto quit;
@@ -235,24 +260,19 @@ int main() {
prn(CSI "0m");
break;
case 127: /* backspace */
- if (x == margin || x == 1) break;
+ if (x == 1) break;
prn("\b");
+ while (types[src_l] == HIDDEN)
+ delc();
delc();
x--;
- if (x == 1) {
+ if (x == 1)
prn(CSI "0m");
- for (i = 1; i < margin; i++)
- prn(" ");
- x = margin;
- }
break;
default:
if (iscntrl(c)) break;
- if (x == margin && c == '.') {
- prn(CSI "%d;%dH", y, 1);
- x = 1;
+ if (x == 1 && c == '.')
prn(CSI "2m");
- }
addc(c);
if (x + 1 > w) breakline();
prn("%c", c);