1
0
Fork 0

number of visible charaters of the prompt is saved in current structure

This commit is contained in:
Esteban Lombeyda 2014-04-01 08:34:17 +02:00
parent 92c43d1f89
commit 0a4e416ebc
1 changed files with 13 additions and 15 deletions

View File

@ -170,7 +170,8 @@ struct current {
size_t pos; /* Cursor position, measured in chars */
int cols; /* Size of the window, in chars */
int rows; /* Screen rows */
const char *prompt;
const char *prompt; /*string with control charaters for displaying the prompt */
size_t pchars; /*number of visible charaters in the prompt */
char *capture; /* Allocated capture buffer, or NULL for none. Always null terminated */
#if defined(USE_TERMIOS)
int fd; /* Terminal fd */
@ -314,12 +315,11 @@ static void clearScreen(struct current *current)
static void cursorToLeft(struct current *current)
{
size_t pchars = 20;
/**
* how many lines are need to display the current->pos characters of the
* current buffer
*/
size_t number_lines = new_line_numbers(current->pos, current->cols, pchars);
size_t number_lines = new_line_numbers(current->pos, current->cols, current->pchars);
/**
* move cursor number_lines above
*/
@ -349,11 +349,10 @@ static void outputControlChar(struct current *current, char ch)
static void eraseEol(struct current *current)
{
size_t pchars = 20;
/**
* number of additional lines to display chars characters (the quite buffer)
*/
size_t number_lines = new_line_numbers(current->chars, current->cols, pchars);
size_t number_lines = new_line_numbers(current->chars, current->cols, current->pchars);
size_t i;
/**
@ -1043,12 +1042,7 @@ static void refreshMultiLine(const char *prompt, struct current *current)
getWindowSize(current);
plen = strlen(prompt);
pchars = utf8_strlen(prompt, plen);
/* Scan the prompt for embedded ansi color control sequences and
* discount them as characters/columns.
*/
pchars -= countColorControlChars(prompt);
pchars = current->pchars;
/* Cursor to left edge, then the prompt */
// cursorToLeft(current);
@ -1468,8 +1462,7 @@ static void moveCursorToRight(struct current * current) {
}
#else
static void moveCursorToLeft(struct current * current) {
size_t pchars = 20;
int x = next_allowed_x(current->pos + 1, current->cols, pchars);
int x = next_allowed_x(current->pos + 1, current->cols, current->pchars);
if(x==0) {
fd_printf(current->fd, "\x1b[1A\x1b[%dG", current->cols);
} else {
@ -1477,8 +1470,7 @@ static void moveCursorToLeft(struct current * current) {
}
}
static void moveCursorToRight(struct current * current) {
size_t pchars = 20;
int x = next_allowed_x(current->pos-1, current->cols, pchars);
int x = next_allowed_x(current->pos-1, current->cols, current->pchars);
if(current->pos>current->chars) {
return;
}
@ -1836,12 +1828,18 @@ char *linenoise(const char *prompt)
}
else
{
size_t pchars = utf8_strlen(prompt, strlen(prompt));
/* Scan the prompt for embedded ansi color control sequences and
* discount them as characters/columns.
*/
pchars -= countColorControlChars(prompt);
current.buf = buf;
current.bufmax = sizeof(buf);
current.len = 0;
current.chars = 0;
current.pos = 0;
current.prompt = prompt;
current.pchars = pchars;
current.capture = NULL;
initLinenoiseLine(&current);