From 0b53bf08b02736ca4facdc13099ae360bbe4a541 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sun, 8 Nov 2020 00:46:57 +0100 Subject: keep track of line breaks in variable --- tea.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'tea.c') diff --git a/tea.c b/tea.c index d580493..5839ef9 100644 --- a/tea.c +++ b/tea.c @@ -20,10 +20,14 @@ char **lines; int lines_c; int lines_s; -char *src; +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 x, y; /* current cursor position */ int w, h; /* terminal width, height */ int margin = 10; @@ -99,8 +103,8 @@ int left() {} void addc(char c) { if (c != '\n' && x + 1 > w) { - prn("\r"); - addc('\n'); + prn("\r\n"); + breaks[src_l + 1] = 1; x = margin + 1; prn(CSI "%dC", margin - 1); if (y == h) yorig--; @@ -126,6 +130,11 @@ int main() { if (src == NULL) err(1, "malloc"); src_l = -1; + breaks = malloc(src_s * sizeof(int)); + if (breaks == NULL) err(1, "malloc"); + for (i = 0; i < src_s; i++) + breaks[i] = 0; + ttyfd = open("/dev/tty", O_RDWR); if (ttyfd == -1) rerr(1, "open"); -- cgit v1.2.3