mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
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:
commit
b0f448a972
@ -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_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
|
||||
@ -887,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);
|
||||
@ -982,8 +987,9 @@ char *linenoise(const char *prompt) {
|
||||
if (count > 0) {
|
||||
sanitize(buf);
|
||||
count = strlen(buf);
|
||||
}
|
||||
if (count <= 0) {
|
||||
} else if (count == 0 && allow_empty) {
|
||||
/* will return an empty (0-length) string */
|
||||
} else {
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
^^^^^^^^^
|
||||
|
@ -22,6 +22,7 @@
|
||||
#include "nvs_flash.h"
|
||||
|
||||
static const char* TAG = "example";
|
||||
#define PROMPT_STR CONFIG_IDF_TARGET
|
||||
|
||||
/* 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
|
||||
@ -112,6 +113,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);
|
||||
@ -140,13 +144,14 @@ void app_main(void)
|
||||
/* Prompt to be printed before each line.
|
||||
* 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"
|
||||
"This is an example of ESP-IDF console component.\n"
|
||||
"Type 'help' to get the list of commands.\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 */
|
||||
int probe_status = linenoiseProbe();
|
||||
@ -160,7 +165,7 @@ void app_main(void)
|
||||
/* Since the terminal doesn't support escape sequences,
|
||||
* don't use color codes in the prompt.
|
||||
*/
|
||||
prompt = "esp32> ";
|
||||
prompt = PROMPT_STR "> ";
|
||||
#endif //CONFIG_LOG_COLORS
|
||||
}
|
||||
|
||||
@ -170,15 +175,17 @@ void app_main(void)
|
||||
* The line is returned when ENTER is pressed.
|
||||
*/
|
||||
char* line = linenoise(prompt);
|
||||
if (line == NULL) { /* Ignore empty lines */
|
||||
continue;
|
||||
if (line == NULL) { /* Break on EOF or error */
|
||||
break;
|
||||
}
|
||||
/* Add the command to the history */
|
||||
linenoiseHistoryAdd(line);
|
||||
/* Add the command to the history if not empty*/
|
||||
if (strlen(line) > 0) {
|
||||
linenoiseHistoryAdd(line);
|
||||
#if CONFIG_STORE_HISTORY
|
||||
/* Save command history to filesystem */
|
||||
linenoiseHistorySave(HISTORY_PATH);
|
||||
/* Save command history to filesystem */
|
||||
linenoiseHistorySave(HISTORY_PATH);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Try to run the command */
|
||||
int ret;
|
||||
@ -195,4 +202,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();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user