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

Rewrote the calculation of prompt length, ignoring length of ANSI esc… #181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion example.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ int main(int argc, char **argv) {
*
* The typed string is returned as a malloc() allocated string by
* linenoise, so the user needs to free() it. */
while((line = linenoise("hello> ")) != NULL) {
const char *prompt = "\033[01;33mhello\033[0m>> ";
while((line = linenoise(prompt)) != NULL) {
/* Do something with the string. */
if (line[0] != '\0' && line[0] != '/') {
printf("echo: '%s'\n", line);
Expand Down
25 changes: 22 additions & 3 deletions linenoise.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ enum KEY_ACTION{
static void linenoiseAtExit(void);
int linenoiseHistoryAdd(const char *line);
static void refreshLine(struct linenoiseState *l);
size_t pstrlen(const char *s);

/* Debugging macro. */
#if 0
Expand Down Expand Up @@ -503,7 +504,7 @@ void refreshShowHints(struct abuf *ab, struct linenoiseState *l, int plen) {
* cursor position, and number of columns of the terminal. */
static void refreshSingleLine(struct linenoiseState *l) {
char seq[64];
size_t plen = strlen(l->prompt);
size_t plen = pstrlen(l->prompt);
int fd = l->ofd;
char *buf = l->buf;
size_t len = l->len;
Expand Down Expand Up @@ -778,7 +779,8 @@ static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen,
l.buf = buf;
l.buflen = buflen;
l.prompt = prompt;
l.plen = strlen(prompt);
// l.plen = strlen(prompt);
l.plen = pstrlen(prompt);
l.oldpos = l.pos = 0;
l.len = 0;
l.cols = getColumns(stdin_fd, stdout_fd);
Expand All @@ -793,7 +795,7 @@ static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen,
* initially is just an empty string. */
linenoiseHistoryAdd("");

if (write(l.ofd,prompt,l.plen) == -1) return -1;
if (write(l.ofd,prompt,strlen(prompt)) == -1) return -1;
while(1) {
char c;
int nread;
Expand Down Expand Up @@ -1199,3 +1201,20 @@ int linenoiseHistoryLoad(const char *filename) {
fclose(fp);
return 0;
}

/* Calculatethe length of the prompt string as is seen from a terminal,
* that is to ignore the length of possible ANSI escape codes.
* Therefore possible mispositions of the cursor can be avoided.
*/
size_t pstrlen(const char *s) {
size_t len = 0, i = 0;
while (s[i] != '\0') {
if (s[i] == '\033') {
i = strpbrk(s + i, "m") - s + 1;
continue;
}
len++;
i++;
}
return len;
}