Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify handling of input escape codes & support ctrl+arrows #216

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 43 additions & 21 deletions linenoise.c
Original file line number Diff line number Diff line change
@@ -756,6 +756,15 @@ void linenoiseEditMoveLeft(struct linenoiseState *l) {
}
}

/* Move cursor to the beginning of the previous word. */
void linenoiseEditMoveLeftWord(struct linenoiseState *l) {
while (l->pos > 0 && l->buf[l->pos-1] == ' ')
l->pos--;
while (l->pos > 0 && l->buf[l->pos-1] != ' ')
l->pos--;
refreshLine(l);
}

/* Move cursor on the right. */
void linenoiseEditMoveRight(struct linenoiseState *l) {
if (l->pos != l->len) {
@@ -764,6 +773,15 @@ void linenoiseEditMoveRight(struct linenoiseState *l) {
}
}

/* Move cursor to the end of the next word. */
void linenoiseEditMoveRightWord(struct linenoiseState *l) {
while (l->pos < l->len && l->buf[l->pos] == ' ')
l->pos++;
while (l->pos < l->len && l->buf[l->pos] != ' ')
l->pos++;
refreshLine(l);
}

/* Move cursor to the start of the line. */
void linenoiseEditMoveHome(struct linenoiseState *l) {
if (l->pos != 0) {
@@ -931,7 +949,7 @@ char *linenoiseEditFeed(struct linenoiseState *l) {

char c;
int nread;
char seq[3];
char seq[32];

nread = read(l->ifd,&c,1);
if (nread <= 0) return NULL;
@@ -1001,23 +1019,29 @@ char *linenoiseEditFeed(struct linenoiseState *l) {
linenoiseEditHistoryNext(l, LINENOISE_HISTORY_NEXT);
break;
case ESC: /* escape sequence */
/* Read the next two bytes representing the escape sequence.
* Use two calls to handle slow terminals returning the two
* chars at different times. */
if (read(l->ifd,seq,1) == -1) break;
if (read(l->ifd,seq+1,1) == -1) break;

/* ESC [ sequences. */
if (seq[0] == '[') {
if (seq[1] >= '0' && seq[1] <= '9') {
/* Extended escape, read additional byte. */
if (read(l->ifd,seq+2,1) == -1) break;
if (seq[2] == '~') {
switch(seq[1]) {
case '3': /* Delete key. */
linenoiseEditDelete(l);
break;
}
if (seq[0] == '[') { /* ESC [ */
/* terminated by a byte in \x40-\x7E inclusive */
memset(seq, 0, sizeof(seq));
for (size_t pos = 1; pos < sizeof(seq); pos++) {
if (read(l->ifd,seq+pos,1) == -1) {
/* The next read will be fucked, but continue anyways.
* The same goes for if it runs out of space in seq. */
return linenoiseEditMore;
}
if (0x40 <= seq[pos] && seq[pos] <= 0x7E) break;
}

if (seq[1] == '3' && seq[2] == '~') {
linenoiseEditDelete(l);
} else if (seq[1] == '1' && seq[2] == ';' && seq[3] == '5') {
switch(seq[4]) {
case 'C': /* Ctrl+Right */
linenoiseEditMoveRightWord(l);
break;
case 'D': /* Ctrl+Left */
linenoiseEditMoveLeftWord(l);
break;
}
} else {
switch(seq[1]) {
@@ -1041,10 +1065,8 @@ char *linenoiseEditFeed(struct linenoiseState *l) {
break;
}
}
}

/* ESC O sequences. */
else if (seq[0] == 'O') {
} else if (seq[0] == 'O') { /* ESC O */
if (read(l->ifd,seq+1,1) == -1) break;
switch(seq[1]) {
case 'H': /* Home */
linenoiseEditMoveHome(l);