A few improvements to mask mode.

from orig commit 4261898
This commit is contained in:
Данила Горнушко 2023-11-22 21:04:22 +03:00
parent 4b7d4b8786
commit f19803a5fa
2 changed files with 16 additions and 14 deletions

View file

@ -122,7 +122,7 @@ static linenoiseCompletionCallback *completionCallback = NULL;
static linenoiseHintsCallback *hintsCallback = NULL; static linenoiseHintsCallback *hintsCallback = NULL;
static linenoiseFreeHintsCallback *freeHintsCallback = NULL; static linenoiseFreeHintsCallback *freeHintsCallback = NULL;
static int maskmode = 0; /* for mask input mode */ static int maskmode = 0; /* Show "***" instead of input. For passwords. */
static int mlmode = 0; /* Multi line mode. Default is single line. */ static int mlmode = 0; /* Multi line mode. Default is single line. */
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;
@ -190,11 +190,16 @@ FILE *lndebug_fp = NULL;
/* ======================= Low level terminal handling ====================== */ /* ======================= Low level terminal handling ====================== */
void linenoiseMaskModeEnable() { /* Enable "mask mode". When it is enabled, instead of the input that
* the user is typing, the terminal will just display a corresponding
* number of asterisks, like "****". This is useful for passwords and other
* secrets that should not be displayed. */
void linenoiseMaskModeEnable(void) {
maskmode = 1; maskmode = 1;
} }
void linenoiseMaskModeDisable() { /* Disable mask mode. */
void linenoiseMaskModeDisable(void) {
maskmode = 0; maskmode = 0;
} }
@ -460,11 +465,10 @@ static void refreshSingleLine(struct linenoiseState *l) {
/* Write the prompt and the current buffer content */ /* Write the prompt and the current buffer content */
abAppend(&ab,l->prompt,strlen(l->prompt)); abAppend(&ab,l->prompt,strlen(l->prompt));
if (maskmode == 1) { if (maskmode == 1) {
while (len--) { while (len--) abAppend(&ab,"*",1);
abAppend(&ab,"*",1); } else {
}
} else
abAppend(&ab,buf,len); abAppend(&ab,buf,len);
}
/* Show hits if any. */ /* Show hits if any. */
refreshShowHints(&ab,l,plen); refreshShowHints(&ab,l,plen);
/* Erase to right */ /* Erase to right */
@ -519,12 +523,10 @@ static void refreshMultiLine(struct linenoiseState *l) {
/* Write the prompt and the current buffer content */ /* Write the prompt and the current buffer content */
abAppend(&ab,l->prompt,strlen(l->prompt)); abAppend(&ab,l->prompt,strlen(l->prompt));
if (maskmode == 1) { if (maskmode == 1) {
for (uint i = 0; i < l->len; i++) { for (uint i = 0; i < l->len; i++) abAppend(&ab,"*",1);
abAppend(&ab,"*",1); } else {
}
} else
abAppend(&ab,l->buf,l->len); abAppend(&ab,l->buf,l->len);
}
/* Show hits if any. */ /* Show hits if any. */
refreshShowHints(&ab,l,plen); refreshShowHints(&ab,l,plen);

View file

@ -67,8 +67,8 @@ void linenoiseClearScreen(void);
void linenoiseSetMultiLine(int ml); void linenoiseSetMultiLine(int ml);
void linenoisePrintKeyCodes(void); void linenoisePrintKeyCodes(void);
void linenoiseMaskModeEnable(); void linenoiseMaskModeEnable(void);
void linenoiseMaskModeDisable(); void linenoiseMaskModeDisable(void);
#ifdef __cplusplus #ifdef __cplusplus