add dump mode functions

This commit is contained in:
Данила Горнушко 2023-11-22 18:01:36 +03:00
parent 943defdbaf
commit 56e1e89ff4

View file

@ -137,6 +137,7 @@ static void refreshLineWithFlags(struct linenoiseState *l, int flags);
static int maskmode = 0; /* Show "***" instead of input. For passwords. */ static int maskmode = 0; /* Show "***" instead of input. For passwords. */
static int rawmode = 0; /* For atexit() function to check if restore is needed*/ static int rawmode = 0; /* For atexit() function to check if restore is needed*/
static int mlmode = 0; /* Multi line mode. Default is single line. */ static int mlmode = 0; /* Multi line mode. Default is single line. */
static int dumbmode = 0; /* Dumb mode where line editing is disabled. Off by default */
static int atexit_registered = 0; /* Register atexit just 1 time. */ static int atexit_registered = 0; /* Register atexit just 1 time. */
static int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN; static int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN;
static int history_len = 0; static int history_len = 0;
@ -171,25 +172,6 @@ int linenoiseHistoryAdd(const char *line);
#define REFRESH_ALL (REFRESH_CLEAN|REFRESH_WRITE) // Do both. #define REFRESH_ALL (REFRESH_CLEAN|REFRESH_WRITE) // Do both.
static void refreshLine(struct linenoiseState *l); static void refreshLine(struct linenoiseState *l);
/* Debugging macro. */
#if 0
FILE *lndebug_fp = NULL;
#define lndebug(...) \
do { \
if (lndebug_fp == NULL) { \
lndebug_fp = fopen("/tmp/lndebug.txt","a"); \
fprintf(lndebug_fp, \
"[%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->oldrows,old_rows); \
} \
fprintf(lndebug_fp, ", " __VA_ARGS__); \
fflush(lndebug_fp); \
} while (0)
#else
#define lndebug(fmt, ...)
#endif
/* ======================= Low level terminal handling ====================== */ /* ======================= Low level terminal handling ====================== */
/* Enable "mask mode". When it is enabled, instead of the input that /* Enable "mask mode". When it is enabled, instead of the input that
@ -210,6 +192,16 @@ void linenoiseSetMultiLine(int ml) {
mlmode = ml; mlmode = ml;
} }
/* Set if terminal does not recognize escape sequences */
void linenoiseSetDumbMode(int set) {
dumbmode = set;
}
/* Returns whether the current mode is dumbmode or not. */
bool linenoiseIsDumbMode(void) {
return dumbmode;
}
/* Return true if the terminal name is in the list of terminals we know are /* Return true if the terminal name is in the list of terminals we know are
* not able to understand basic escape sequences. */ * not able to understand basic escape sequences. */
static int isUnsupportedTerm(void) { static int isUnsupportedTerm(void) {
@ -672,14 +664,12 @@ static void refreshMultiLine(struct linenoiseState *l, int flags) {
if (flags & REFRESH_CLEAN) { if (flags & REFRESH_CLEAN) {
if (old_rows-rpos > 0) { if (old_rows-rpos > 0) {
lndebug("go down %d", old_rows-rpos);
snprintf(seq,64,"\x1b[%dB", old_rows-rpos); snprintf(seq,64,"\x1b[%dB", old_rows-rpos);
abAppend(&ab,seq,strlen(seq)); abAppend(&ab,seq,strlen(seq));
} }
/* Now for every row clear it, go up. */ /* Now for every row clear it, go up. */
for (j = 0; j < old_rows-1; j++) { for (j = 0; j < old_rows-1; j++) {
lndebug("clear+up");
snprintf(seq,64,"\r\x1b[0K\x1b[1A"); snprintf(seq,64,"\r\x1b[0K\x1b[1A");
abAppend(&ab,seq,strlen(seq)); abAppend(&ab,seq,strlen(seq));
} }
@ -687,7 +677,6 @@ static void refreshMultiLine(struct linenoiseState *l, int flags) {
if (flags & REFRESH_ALL) { if (flags & REFRESH_ALL) {
/* Clean the top line. */ /* Clean the top line. */
lndebug("clear");
snprintf(seq,64,"\r\x1b[0K"); snprintf(seq,64,"\r\x1b[0K");
abAppend(&ab,seq,strlen(seq)); abAppend(&ab,seq,strlen(seq));
} }
@ -711,7 +700,6 @@ static void refreshMultiLine(struct linenoiseState *l, int flags) {
l->pos == l->len && l->pos == l->len &&
(l->pos+plen) % l->cols == 0) (l->pos+plen) % l->cols == 0)
{ {
lndebug("<newline>");
abAppend(&ab,"\n",1); abAppend(&ab,"\n",1);
snprintf(seq,64,"\r"); snprintf(seq,64,"\r");
abAppend(&ab,seq,strlen(seq)); abAppend(&ab,seq,strlen(seq));
@ -721,18 +709,15 @@ static void refreshMultiLine(struct linenoiseState *l, int flags) {
/* Move cursor to right position. */ /* Move cursor to right position. */
rpos2 = (plen+l->pos+l->cols)/l->cols; /* Current cursor relative row */ rpos2 = (plen+l->pos+l->cols)/l->cols; /* Current cursor relative row */
lndebug("rpos2 %d", rpos2);
/* Go up till we reach the expected positon. */ /* Go up till we reach the expected positon. */
if (rows-rpos2 > 0) { if (rows-rpos2 > 0) {
lndebug("go-up %d", rows-rpos2);
snprintf(seq,64,"\x1b[%dA", rows-rpos2); snprintf(seq,64,"\x1b[%dA", rows-rpos2);
abAppend(&ab,seq,strlen(seq)); abAppend(&ab,seq,strlen(seq));
} }
/* Set column. */ /* Set column. */
col = (plen+(int)l->pos) % (int)l->cols; col = (plen+(int)l->pos) % (int)l->cols;
lndebug("set col %d", 1+col);
if (col) if (col)
snprintf(seq,64,"\r\x1b[%dC", col); snprintf(seq,64,"\r\x1b[%dC", col);
else else
@ -740,7 +725,6 @@ static void refreshMultiLine(struct linenoiseState *l, int flags) {
abAppend(&ab,seq,strlen(seq)); abAppend(&ab,seq,strlen(seq));
} }
lndebug("\n");
l->oldpos = l->pos; l->oldpos = l->pos;
if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */ if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */