Replace ESC 999D with CR. OSX default Terminal app does not handle 999D well, the cursor will wrap back to the previous row in the last colum, instead of ignoring the sequence if the cursor is already at the left edge. In order to avoid reintroducing the nG sequence that is not compatible with base VT100 emulation and ANSI.SYS, we use CR that should be hopefully widely supported.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
diff --git a/linenoise.c b/linenoise.c
index b352106..13f0865 100644
--- a/linenoise.c
+++ b/linenoise.c
@@ -480,7 +480,7 @@ static void refreshSingleLine(struct linenoiseState *l) {
abInit(&ab);
/* Cursor to left edge */
- snprintf(seq,64,"\x1b[999D");
+ snprintf(seq,64,"\r");
abAppend(&ab,seq,strlen(seq));
/* Write the prompt and the current buffer content */
abAppend(&ab,l->prompt,strlen(l->prompt));
@@ -489,7 +489,7 @@ static void refreshSingleLine(struct linenoiseState *l) {
snprintf(seq,64,"\x1b[0K");
abAppend(&ab,seq,strlen(seq));
/* Move cursor to original position. */
- snprintf(seq,64,"\x1b[999D\x1b[%dC", (int)(pos+plen));
+ snprintf(seq,64,"\r\x1b[%dC", (int)(pos+plen));
abAppend(&ab,seq,strlen(seq));
if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */
abFree(&ab);
@@ -525,13 +525,13 @@ static void refreshMultiLine(struct linenoiseState *l) {
/* Now for every row clear it, go up. */
for (j = 0; j < old_rows-1; j++) {
lndebug("clear+up");
- snprintf(seq,64,"\x1b[999D\x1b[0K\x1b[1A");
+ snprintf(seq,64,"\r\x1b[0K\x1b[1A");
abAppend(&ab,seq,strlen(seq));
}
/* Clean the top line. */
lndebug("clear");
- snprintf(seq,64,"\x1b[999D\x1b[0K");
+ snprintf(seq,64,"\r\x1b[0K");
abAppend(&ab,seq,strlen(seq));
/* Write the prompt and the current buffer content */
@@ -546,7 +546,7 @@ static void refreshMultiLine(struct linenoiseState *l) {
{
lndebug("<newline>");
abAppend(&ab,"\n",1);
- snprintf(seq,64,"\x1b[999D");
+ snprintf(seq,64,"\r");
abAppend(&ab,seq,strlen(seq));
rows++;
if (rows > (int)l->maxrows) l->maxrows = rows;
@@ -567,9 +567,9 @@ static void refreshMultiLine(struct linenoiseState *l) {
col = (plen+(int)l->pos) % (int)l->cols;
lndebug("set col %d", 1+col);
if (col)
- snprintf(seq,64,"\x1b[999D\x1b[%dC", col);
+ snprintf(seq,64,"\r\x1b[%dC", col);
else
- snprintf(seq,64,"\x1b[999D");
+ snprintf(seq,64,"\r");
abAppend(&ab,seq,strlen(seq));
lndebug("\n");
@@ -918,7 +918,7 @@ void linenoisePrintKeyCodes(void) {
printf("'%c' %02x (%d) (type quit to exit)\n",
isprint(c) ? c : '?', (int)c, (int)c);
- printf("\x1b[999D"); /* Go left edge manually, we are in raw mode. */
+ printf("\r"); /* Go left edge manually, we are in raw mode. */
fflush(stdout);
}
disableRawMode(STDIN_FILENO);