aboutsummaryrefslogtreecommitdiffstats
path: root/erts
diff options
context:
space:
mode:
authorDan Gudmundsson <[email protected]>2017-09-14 09:46:14 +0200
committerDan Gudmundsson <[email protected]>2017-09-14 09:46:14 +0200
commit2e8d9a446ef77391ae7faf6ee321479f6af5a92b (patch)
treeb82e22ab231fdfe77edaf5fa29719a0b7bcff464 /erts
parentc39ffbd27c1bf61be512e1c5edbee63d244bc8f0 (diff)
parentbd36da0f509ee4e357fdf51b60f3dfcecc824eb8 (diff)
downloadotp-2e8d9a446ef77391ae7faf6ee321479f6af5a92b.tar.gz
otp-2e8d9a446ef77391ae7faf6ee321479f6af5a92b.tar.bz2
otp-2e8d9a446ef77391ae7faf6ee321479f6af5a92b.zip
Merge branch 'maint'
* maint: stdlib: Fix jumping to beginning or end of line Fix del_chars not considering wide chars and update buffer length before calling write_buf Make cp_pos_to_col function aware of the ANSI escape codes
Diffstat (limited to 'erts')
-rw-r--r--erts/emulator/drivers/unix/ttsl_drv.c76
1 files changed, 57 insertions, 19 deletions
diff --git a/erts/emulator/drivers/unix/ttsl_drv.c b/erts/emulator/drivers/unix/ttsl_drv.c
index 8efe35e321..7355df6059 100644
--- a/erts/emulator/drivers/unix/ttsl_drv.c
+++ b/erts/emulator/drivers/unix/ttsl_drv.c
@@ -936,10 +936,10 @@ static int put_chars(byte *s, int l)
int n;
n = insert_buf(s, l);
+ if (lpos > llen)
+ llen = lpos;
if (n > 0)
write_buf(lbuf + lpos - n, n);
- if (lpos > llen)
- llen = lpos;
return TRUE;
}
@@ -1016,7 +1016,7 @@ static int del_chars(int n)
outc(' ');
move_left(1);
}
- move_cursor(llen + l, lpos);
+ move_cursor(llen + gcs, lpos);
}
else if (pos < lpos) {
l = lpos - pos; /* Buffer characters */
@@ -1036,7 +1036,7 @@ static int del_chars(int n)
outc(' ');
move_left(1);
}
- move_cursor(llen + l, lpos);
+ move_cursor(llen + gcs, lpos);
}
return TRUE;
}
@@ -1095,6 +1095,7 @@ static int insert_buf(byte *s, int n)
ch = 0;
} while (lpos % 8);
} else if (ch == '\e') {
+ DEBUGLOG(("insert_buf: ANSI Escape: \\e"));
lbuf[lpos++] = (CONTROL_TAG | ((Uint32) ch));
} else if (ch == '\n' || ch == '\r') {
write_buf(lbuf + buffpos, lpos - buffpos);
@@ -1156,7 +1157,7 @@ static int write_buf(Uint32 *s, int n)
--n; s++;
}
} else if (*s == (CONTROL_TAG | ((Uint32) '\e'))) {
- outc('\e');
+ outc(lastput = '\e');
--n;
++s;
} else if (*s & CONTROL_TAG) {
@@ -1251,26 +1252,63 @@ static int move_cursor(int from_pos, int to_pos)
return to_col-from_col;
}
-static int cp_pos_to_col(int cp_pos)
+/*
+ * Returns the length of an ANSI escape code in a buffer, this function only consider
+ * color escape sequences like `\e[33m` or `\e[21;33m`. If a sequence has no valid
+ * terminator, the length is equal the number of characters between `\e` and the first
+ * invalid character, inclusive.
+ */
+
+static int ansi_escape_width(Uint32 *s, int max_length)
{
-#ifdef HAVE_WCWIDTH
int i;
- int col = 0;
-
- for (i = 0; i < cp_pos; i++) {
- int w = wcwidth(lbuf[i]);
- if (w > 0) {
- col += w;
- }
+
+ if (*s != (CONTROL_TAG | ((Uint32) '\e'))) {
+ return 0;
+ } else if (max_length <= 1) {
+ return 1;
+ } else if (s[1] != '[') {
+ return 2;
}
- return col;
-#else
+
+ for (i = 2; i < max_length && (s[i] == ';' || (s[i] >= '0' && s[i] <= '9')); i++);
+
+ return i + 1;
+}
+
+static int cp_pos_to_col(int cp_pos)
+{
/*
- * We dont' have any character width information. Assume that
- * code points are one column wide.
+ * If we don't have any character width information. Assume that
+ * code points are one column wide
*/
- return cp_pos;
+ int w = 1;
+ int col = 0;
+ int i = 0;
+ int j;
+
+ if (cp_pos > llen) {
+ col += cp_pos - llen;
+ cp_pos = llen;
+ }
+
+ while (i < cp_pos) {
+ j = ansi_escape_width(lbuf + i, llen - i);
+
+ if (j > 0) {
+ i += j;
+ } else {
+#ifdef HAVE_WCWIDTH
+ w = wcwidth(lbuf[i]);
#endif
+ if (w > 0) {
+ col += w;
+ }
+ i++;
+ }
+ }
+
+ return col;
}
static int start_termcap(void)