adding new functions and a new struct

This commit is contained in:
Данила Горнушко 2023-11-22 15:49:30 +03:00
parent 57f4569fcd
commit e3fe13d3ea
2 changed files with 50 additions and 26 deletions

View file

@ -137,21 +137,6 @@ static int history_len = 0;
static char **history = NULL; static char **history = NULL;
static bool allow_empty = true; static bool allow_empty = true;
/* The linenoiseState structure represents the state during line editing.
* We pass this state to functions implementing specific editing
* functionalities. */
struct linenoiseState {
char *buf; /* Edited line buffer. */
size_t buflen; /* Edited line buffer size. */
const char *prompt; /* Prompt to display. */
size_t plen; /* Prompt length. */
size_t pos; /* Current cursor position. */
size_t oldpos; /* Previous refresh cursor position. */
size_t len; /* Current edited line length. */
size_t cols; /* Number of columns in terminal. */
size_t maxrows; /* Maximum num of rows used so far (multiline mode) */
int history_index; /* The history index we are currently editing. */
};
enum KEY_ACTION{ enum KEY_ACTION{
KEY_NULL = 0, /* NULL */ KEY_NULL = 0, /* NULL */
@ -188,7 +173,7 @@ FILE *lndebug_fp = NULL;
fprintf(lndebug_fp, \ fprintf(lndebug_fp, \
"[%d %d %d] p: %d, rows: %d, rpos: %d, max: %d, oldmax: %d\n", \ "[%d %d %d] p: %d, rows: %d, rpos: %d, max: %d, oldmax: %d\n", \
(int)l->len,(int)l->pos,(int)l->oldpos,plen,rows,rpos, \ (int)l->len,(int)l->pos,(int)l->oldpos,plen,rows,rpos, \
(int)l->maxrows,old_rows); \ (int)l->oldrows,old_rows); \
} \ } \
fprintf(lndebug_fp, ", " __VA_ARGS__); \ fprintf(lndebug_fp, ", " __VA_ARGS__); \
fflush(lndebug_fp); \ fflush(lndebug_fp); \
@ -568,13 +553,13 @@ static void refreshMultiLine(struct linenoiseState *l) {
int rpos = (plen+l->oldpos+l->cols)/l->cols; /* cursor relative row. */ int rpos = (plen+l->oldpos+l->cols)/l->cols; /* cursor relative row. */
int rpos2; /* rpos after refresh. */ int rpos2; /* rpos after refresh. */
int col; /* colum position, zero-based. */ int col; /* colum position, zero-based. */
int old_rows = l->maxrows; int old_rows = l->oldrows;
int j; int j;
int fd = fileno(stdout); int fd = fileno(stdout);
struct abuf ab; struct abuf ab;
/* Update maxrows if needed. */ /* Update oldrows if needed. */
if (rows > (int)l->maxrows) l->maxrows = rows; if (rows > (int)l->oldrows) l->oldrows = rows;
/* First step: clear all the lines used before. To do so start by /* First step: clear all the lines used before. To do so start by
* going to the last row. */ * going to the last row. */
@ -615,7 +600,7 @@ static void refreshMultiLine(struct linenoiseState *l) {
snprintf(seq,64,"\r"); snprintf(seq,64,"\r");
abAppend(&ab,seq,strlen(seq)); abAppend(&ab,seq,strlen(seq));
rows++; rows++;
if (rows > (int)l->maxrows) l->maxrows = rows; if (rows > (int)l->oldrows) l->oldrows = rows;
} }
/* Move cursor to right position. */ /* Move cursor to right position. */
@ -829,7 +814,7 @@ static int linenoiseEdit(char *buf, size_t buflen, const char *prompt)
l.oldpos = l.pos = 0; l.oldpos = l.pos = 0;
l.len = 0; l.len = 0;
l.cols = getColumns(); l.cols = getColumns();
l.maxrows = 0; l.oldrows = 0;
l.history_index = 0; l.history_index = 0;
/* Buffer starts empty. */ /* Buffer starts empty. */

View file

@ -46,11 +46,46 @@ extern "C" {
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
extern char *linenoiseEditMore;
/* The linenoiseState structure represents the state during line editing.
* We pass this state to functions implementing specific editing
* functionalities. */
struct linenoiseState {
int in_completion; /* The user pressed TAB and we are now in completion
* mode, so input is handled by completeLine(). */
size_t completion_idx; /* Index of next completion to propose. */
int ifd; /* Terminal stdin file descriptor. */
int ofd; /* Terminal stdout file descriptor. */
char *buf; /* Edited line buffer. */
size_t buflen; /* Edited line buffer size. */
const char *prompt; /* Prompt to display. */
size_t plen; /* Prompt length. */
size_t pos; /* Current cursor position. */
size_t oldpos; /* Previous refresh cursor position. */
size_t len; /* Current edited line length. */
size_t cols; /* Number of columns in terminal. */
size_t oldrows; /* Rows used by last refrehsed line (multiline mode) */
int history_index; /* The history index we are currently editing. */
};
typedef struct linenoiseCompletions { typedef struct linenoiseCompletions {
size_t len; size_t len;
char **cvec; char **cvec;
} linenoiseCompletions; } linenoiseCompletions;
/* Non blocking API. */
int linenoiseEditStart(struct linenoiseState *l, int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt);
char *linenoiseEditFeed(struct linenoiseState *l);
void linenoiseEditStop(struct linenoiseState *l);
void linenoiseHide(struct linenoiseState *l);
void linenoiseShow(struct linenoiseState *l);
/* Blocking API. */
char *linenoise(const char *prompt);
void linenoiseFree(void *ptr);
/* Completion API. */
typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *); typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *);
typedef char*(linenoiseHintsCallback)(const char *, int *color, int *bold); typedef char*(linenoiseHintsCallback)(const char *, int *color, int *bold);
typedef void(linenoiseFreeHintsCallback)(void *); typedef void(linenoiseFreeHintsCallback)(void *);
@ -59,21 +94,25 @@ void linenoiseSetHintsCallback(linenoiseHintsCallback *);
void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *); void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *);
void linenoiseAddCompletion(linenoiseCompletions *, const char *); void linenoiseAddCompletion(linenoiseCompletions *, const char *);
int linenoiseProbe(void); /* History API. */
char *linenoise(const char *prompt);
void linenoiseFree(void *ptr);
int linenoiseHistoryAdd(const char *line); int linenoiseHistoryAdd(const char *line);
int linenoiseHistorySetMaxLen(int len); int linenoiseHistorySetMaxLen(int len);
int linenoiseHistorySave(const char *filename); int linenoiseHistorySave(const char *filename);
int linenoiseHistoryLoad(const char *filename); int linenoiseHistoryLoad(const char *filename);
void linenoiseHistoryFree(void); void linenoiseHistoryFree(void);
/* Other utilities. */
void linenoiseClearScreen(void); void linenoiseClearScreen(void);
void linenoiseSetMultiLine(int ml); void linenoiseSetMultiLine(int ml);
void linenoiseSetDumbMode(int set);
bool linenoiseIsDumbMode(void);
void linenoisePrintKeyCodes(void); void linenoisePrintKeyCodes(void);
// Allow empty input from console (Enter key)
void linenoiseAllowEmpty(bool); void linenoiseAllowEmpty(bool);
int linenoiseSetMaxLineLen(size_t len); int linenoiseSetMaxLineLen(size_t len);
// Testing console for escape codes support
int linenoiseProbe(void);
// Dump mode for consoles withoud escape codes support
void linenoiseSetDumbMode(int set);
bool linenoiseIsDumbMode(void);
#ifdef __cplusplus #ifdef __cplusplus
} }