commit eb3876e51201e6fb9cd81610c038bf73d38d2ffb
parent c53af3db4ccc5685f6839b768ca40f6f07f5b751
Author: Jan Klemkow <j.klemkow@wemelug.de>
Date: Mon, 3 Feb 2020 22:52:53 +0100
add strelen function
strelen is aware of ESC sequences
Diffstat:
| M | TODO | | | 2 | +- |
| M | scroll.c | | | 40 | ++++++++++++++++++++++++++++++++++++++-- |
2 files changed, 39 insertions(+), 3 deletions(-)
diff --git a/TODO b/TODO
@@ -1,4 +1,4 @@
- * strlen function which is aware of ESC sequences and unicode
+ * strlen function which is aware of unicode
* Alternative Screen mode detection
* scroll down function
* scroll/jump to buttom on new input
diff --git a/scroll.c b/scroll.c
@@ -27,6 +27,7 @@ TAILQ_HEAD(tailhead, line) head;
struct line {
TAILQ_ENTRY(line) entries;
+ size_t size;
size_t len;
char *str;
} *bottom;
@@ -82,6 +83,40 @@ reset(void)
die("tcsetattr:");
}
+size_t
+strelen(const char *str, size_t size)
+{
+ enum {CHAR, BREK, ESC} state = CHAR;
+ size_t len = 0;
+
+ for (size_t i = 0; i < size; i++) {
+ char c = str[i];
+
+ switch (state) {
+ case CHAR:
+ if (c == '\033')
+ state = BREK;
+ else
+ len++;
+ break;
+ case BREK:
+ if (c == '[') {
+ state = ESC;
+ } else {
+ state = CHAR;
+ len++;
+ }
+ break;
+ case ESC:
+ if (c >= 64 && c <= 126)
+ state = CHAR;
+ break;
+ }
+ }
+
+ return len;
+}
+
void
addline(char *str, size_t size)
{
@@ -90,7 +125,8 @@ addline(char *str, size_t size)
if (line == NULL)
die("malloc:");
- line->len = size;
+ line->size = size;
+ line->len = strelen(str, size);
line->str = malloc(size);
if (line->str == NULL)
die("malloc:");
@@ -131,7 +167,7 @@ scrollup(void)
if (line == NULL)
return;
- write(STDOUT_FILENO, line->str, line->len);
+ write(STDOUT_FILENO, line->str, line->size);
return;
}