Skip to content

Commit

Permalink
Improve linenoise.c compatability.
Browse files Browse the repository at this point in the history
+ Don't flush characters when switching into and out of raw mode. This
avoids eating characters that follow '\n'. (Such characters can occur
when pasting multiple line input, or when scripts are driving input.)

+ Try to be slightly cleverer about calculating the length of the prompt,
so that prompts with embedded '\n' characters are handled OK. This is
an area that really needs to be replaced with a query of the cursor
position from the terminal.

+ As a hack, just assume the screen is very wide if we don't know how
wide it is. This allows dexpropt to work correctly. (It was getting
confused by the editing commands emitted when the end-of-line was reached.)

Change-Id: I988dd0f0bceb22b298e915be0dde085c9358ef66
  • Loading branch information
jackpal committed Jun 4, 2010
1 parent ad5431d commit 192a288
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions liblinenoise/linenoise.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ static int enableRawMode(int fd) {
* We want read to return every single byte, without timeout. */
raw.c_cc[VMIN] = 1; raw.c_cc[VTIME] = 0; /* 1 byte, no timer */

/* put terminal in raw mode after flushing */
if (tcsetattr(fd,TCSAFLUSH,&raw) < 0) goto fatal;
/* put terminal in raw mode */
if (tcsetattr(fd,TCSADRAIN,&raw) < 0) goto fatal;
rawmode = 1;
return 0;

Expand All @@ -150,7 +150,7 @@ static int enableRawMode(int fd) {

static void disableRawMode(int fd) {
/* Don't even check the return value as it's too late. */
if (rawmode && tcsetattr(fd,TCSAFLUSH,&orig_termios) != -1)
if (rawmode && tcsetattr(fd,TCSADRAIN,&orig_termios) != -1)
rawmode = 0;
}

Expand All @@ -163,16 +163,30 @@ static void linenoiseAtExit(void) {
static int getColumns(void) {
struct winsize ws;

if (ioctl(1, TIOCGWINSZ, &ws) == -1) return 80;
if (ioctl(1, TIOCGWINSZ, &ws) == -1) return 4096;
if (ws.ws_col == 0) {
return 80;
return 4096;
}
return ws.ws_col;
}

static int effectiveLen(const char* prompt) {
int col = 0;
char c;
// TODO: Handle escape sequences.
while ( (c = *prompt++) != 0 ) {
if (c == '\n') {
col = 0;
} else {
col++;
}
}
return col;
}

static void refreshLine(int fd, const char *prompt, char *buf, size_t len, size_t pos, size_t cols) {
char seq[64];
size_t plen = strlen(prompt);
size_t plen = effectiveLen(prompt);

while((plen+pos) >= cols) {
buf++;
Expand Down

0 comments on commit 192a288

Please sign in to comment.