console: make empty line behavior run-time configurable

This commit is contained in:
Ivan Grokhotkov 2020-04-03 00:16:21 +02:00
parent c34352549a
commit ece41b04e3
5 changed files with 18 additions and 18 deletions

View File

@ -1,10 +0,0 @@
menu "Linenoise"
config ESP_LINENOISE_RETURN_ZERO_STRING
bool "Return 0 length strings"
default n
help
If enabled linenoise will return 0 length strings if the user presses enter with out typing
a command, if disabled NULL will be returned instead.
endmenu

View File

@ -116,7 +116,6 @@
#include <sys/fcntl.h>
#include <unistd.h>
#include "linenoise.h"
#include "sdkconfig.h"
#define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100
#define LINENOISE_MAX_LINE 4096
@ -130,6 +129,7 @@ static int dumbmode = 0; /* Dumb mode where line editing is disabled. Off by def
static int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN;
static int history_len = 0;
static char **history = NULL;
static bool allow_empty = true;
/* The linenoiseState structure represents the state during line editing.
* We pass this state to functions implementing specific editing
@ -888,6 +888,10 @@ static int linenoiseEdit(char *buf, size_t buflen, const char *prompt)
return l.len;
}
void linenoiseAllowEmpty(bool val) {
allow_empty = val;
}
int linenoiseProbe(void) {
/* Switch to non-blocking mode */
int flags = fcntl(STDIN_FILENO, F_GETFL);
@ -980,13 +984,11 @@ char *linenoise(const char *prompt) {
} else {
count = linenoiseDumb(buf, LINENOISE_MAX_LINE, prompt);
}
#ifdef CONFIG_ESP_LINENOISE_RETURN_ZERO_STRING
if (count >= 0) {
#else
if (count > 0) {
#endif
sanitize(buf);
count = strlen(buf);
} else if (count == 0 && allow_empty) {
/* will return an empty (0-length) string */
} else {
free(buf);
return NULL;

View File

@ -43,6 +43,8 @@
extern "C" {
#endif
#include <stdbool.h>
typedef struct linenoiseCompletions {
size_t len;
char **cvec;
@ -68,6 +70,7 @@ void linenoiseClearScreen(void);
void linenoiseSetMultiLine(int ml);
void linenoiseSetDumbMode(int set);
void linenoisePrintKeyCodes(void);
void linenoiseAllowEmpty(bool);
#ifdef __cplusplus
}

View File

@ -36,6 +36,9 @@ Linenoise library does not need explicit initialization. However, some configura
:cpp:func:`linenoiseSetMultiLine`
Switch between single line and multi line editing modes. In single line mode, if the length of the command exceeds the width of the terminal, the command text is scrolled within the line to show the end of the text. In this case the beginning of the text is hidden. Single line needs less data to be sent to refresh screen on each key press, so exhibits less glitching compared to the multi line mode. On the flip side, editing commands and copying command text from terminal in single line mode is harder. Default is single line mode.
:cpp:func:`linenoiseAllowEmpty`
Set whether linenoise library will return a zero-length string (if ``true``) or ``NULL`` (if ``false``) for empty lines. By default, zero-length strings are returned.
Main loop
^^^^^^^^^

View File

@ -112,6 +112,9 @@ static void initialize_console(void)
/* Set command history size */
linenoiseHistorySetMaxLen(100);
/* Don't return empty lines */
linenoiseAllowEmpty(false);
#if CONFIG_STORE_HISTORY
/* Load command history from filesystem */
linenoiseHistoryLoad(HISTORY_PATH);
@ -174,7 +177,7 @@ void app_main(void)
break;
}
/* Add the command to the history if not empty*/
if(strlen(line) > 0) {
if (strlen(line) > 0) {
linenoiseHistoryAdd(line);
#if CONFIG_STORE_HISTORY
/* Save command history to filesystem */
@ -197,8 +200,7 @@ void app_main(void)
/* linenoise allocates line buffer on the heap, so need to free it */
linenoiseFree(line);
}
ESP_LOGE(TAG, "Error or end-of-input, terminating console");
esp_console_deinit();
vTaskDelete(NULL); /* terminate app_main */
}