Merge branch 'feature/console_no_empty_lines' into 'master'

console: allow not returning empty lines (Github PR)

Closes IDFGH-2869

See merge request espressif/esp-idf!8232
This commit is contained in:
Ivan Grokhotkov
2020-05-05 16:12:50 +08:00
4 changed files with 33 additions and 11 deletions

View File

@@ -129,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_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN;
static int history_len = 0; static int history_len = 0;
static char **history = NULL; static char **history = NULL;
static bool allow_empty = true;
/* The linenoiseState structure represents the state during line editing. /* The linenoiseState structure represents the state during line editing.
* We pass this state to functions implementing specific editing * We pass this state to functions implementing specific editing
@@ -887,6 +888,10 @@ static int linenoiseEdit(char *buf, size_t buflen, const char *prompt)
return l.len; return l.len;
} }
void linenoiseAllowEmpty(bool val) {
allow_empty = val;
}
int linenoiseProbe(void) { int linenoiseProbe(void) {
/* Switch to non-blocking mode */ /* Switch to non-blocking mode */
int flags = fcntl(STDIN_FILENO, F_GETFL); int flags = fcntl(STDIN_FILENO, F_GETFL);
@@ -982,8 +987,9 @@ char *linenoise(const char *prompt) {
if (count > 0) { if (count > 0) {
sanitize(buf); sanitize(buf);
count = strlen(buf); count = strlen(buf);
} } else if (count == 0 && allow_empty) {
if (count <= 0) { /* will return an empty (0-length) string */
} else {
free(buf); free(buf);
return NULL; return NULL;
} }

View File

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

View File

@@ -36,6 +36,9 @@ Linenoise library does not need explicit initialization. However, some configura
:cpp:func:`linenoiseSetMultiLine` :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. 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 Main loop
^^^^^^^^^ ^^^^^^^^^

View File

@@ -22,6 +22,7 @@
#include "nvs_flash.h" #include "nvs_flash.h"
static const char* TAG = "example"; static const char* TAG = "example";
#define PROMPT_STR CONFIG_IDF_TARGET
/* Console command history can be stored to and loaded from a file. /* Console command history can be stored to and loaded from a file.
* The easiest way to do this is to use FATFS filesystem on top of * The easiest way to do this is to use FATFS filesystem on top of
@@ -112,6 +113,9 @@ static void initialize_console(void)
/* Set command history size */ /* Set command history size */
linenoiseHistorySetMaxLen(100); linenoiseHistorySetMaxLen(100);
/* Don't return empty lines */
linenoiseAllowEmpty(false);
#if CONFIG_STORE_HISTORY #if CONFIG_STORE_HISTORY
/* Load command history from filesystem */ /* Load command history from filesystem */
linenoiseHistoryLoad(HISTORY_PATH); linenoiseHistoryLoad(HISTORY_PATH);
@@ -140,13 +144,14 @@ void app_main(void)
/* Prompt to be printed before each line. /* Prompt to be printed before each line.
* This can be customized, made dynamic, etc. * This can be customized, made dynamic, etc.
*/ */
const char* prompt = LOG_COLOR_I "esp32> " LOG_RESET_COLOR; const char* prompt = LOG_COLOR_I PROMPT_STR "> " LOG_RESET_COLOR;
printf("\n" printf("\n"
"This is an example of ESP-IDF console component.\n" "This is an example of ESP-IDF console component.\n"
"Type 'help' to get the list of commands.\n" "Type 'help' to get the list of commands.\n"
"Use UP/DOWN arrows to navigate through command history.\n" "Use UP/DOWN arrows to navigate through command history.\n"
"Press TAB when typing command name to auto-complete.\n"); "Press TAB when typing command name to auto-complete.\n"
"Press Enter or Ctrl+C will terminate the console environment.\n");
/* Figure out if the terminal supports escape sequences */ /* Figure out if the terminal supports escape sequences */
int probe_status = linenoiseProbe(); int probe_status = linenoiseProbe();
@@ -160,7 +165,7 @@ void app_main(void)
/* Since the terminal doesn't support escape sequences, /* Since the terminal doesn't support escape sequences,
* don't use color codes in the prompt. * don't use color codes in the prompt.
*/ */
prompt = "esp32> "; prompt = PROMPT_STR "> ";
#endif //CONFIG_LOG_COLORS #endif //CONFIG_LOG_COLORS
} }
@@ -170,15 +175,17 @@ void app_main(void)
* The line is returned when ENTER is pressed. * The line is returned when ENTER is pressed.
*/ */
char* line = linenoise(prompt); char* line = linenoise(prompt);
if (line == NULL) { /* Ignore empty lines */ if (line == NULL) { /* Break on EOF or error */
continue; break;
} }
/* Add the command to the history */ /* Add the command to the history if not empty*/
if (strlen(line) > 0) {
linenoiseHistoryAdd(line); linenoiseHistoryAdd(line);
#if CONFIG_STORE_HISTORY #if CONFIG_STORE_HISTORY
/* Save command history to filesystem */ /* Save command history to filesystem */
linenoiseHistorySave(HISTORY_PATH); linenoiseHistorySave(HISTORY_PATH);
#endif #endif
}
/* Try to run the command */ /* Try to run the command */
int ret; int ret;
@@ -195,4 +202,7 @@ void app_main(void)
/* linenoise allocates line buffer on the heap, so need to free it */ /* linenoise allocates line buffer on the heap, so need to free it */
linenoiseFree(line); linenoiseFree(line);
} }
ESP_LOGE(TAG, "Error or end-of-input, terminating console");
esp_console_deinit();
} }