Skip to content

Commit 60f7df2

Browse files
committed
feat(esp_linenoise): Expose esp_linenoise_probe API
this commit also adds a getter and setter function for the esp_linenoise_config_t prompt field. this makes the re-probing and update of prompt possible.
1 parent 2e74536 commit 60f7df2

File tree

5 files changed

+69
-13
lines changed

5 files changed

+69
-13
lines changed

esp_linenoise/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "1.0.2"
1+
version: "1.0.3"
22
description: "ESP Linenoise - Line editing C library"
33
url: https://github.com/espressif/idf-extra-components/tree/master/esp_linenoise
44
license: Apache-2.0

esp_linenoise/include/esp_linenoise.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ typedef struct esp_linenoise_config {
126126
*/
127127
typedef struct esp_linenoise_instance *esp_linenoise_handle_t;
128128

129+
/**
130+
* @brief Probe the terminal to check weather it supports escape sequences
131+
*
132+
* @param handle The linenoise handle used to check
133+
* @return int 0 if the terminal supports escape sequences
134+
*/
135+
int esp_linenoise_probe(esp_linenoise_handle_t handle);
136+
129137
/**
130138
* @brief Returns the default parameters for creating a linenoise instance.
131139
*
@@ -307,6 +315,24 @@ esp_err_t esp_linenoise_set_max_cmd_line_length(esp_linenoise_handle_t handle, s
307315
*/
308316
esp_err_t esp_linenoise_get_max_cmd_line_length(esp_linenoise_handle_t handle, size_t *max_cmd_line_length);
309317

318+
/**
319+
* @brief Sets the prompt used by the esp_linenoise instance.
320+
*
321+
* @param handle Handle to the linenoise instance.
322+
* @param prompt Prompt to be set.
323+
* @return ESP_OK on success, or error code on failure.
324+
*/
325+
esp_err_t esp_linenoise_set_prompt(esp_linenoise_handle_t handle, const char *prompt);
326+
327+
/**
328+
* @brief Gets the current esp_linenoise instance prompt.
329+
*
330+
* @param handle Handle to the linenoise instance.
331+
* @param prompt esp_linenoise instance current prompt.
332+
* @return ESP_OK on success, or error code on failure.
333+
*/
334+
esp_err_t esp_linenoise_get_prompt(esp_linenoise_handle_t handle, const char **prompt);
335+
310336
/**
311337
* @brief Return the output file descriptor used by esp_linenoise
312338
*

esp_linenoise/private_include/esp_linenoise_private.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,6 @@ esp_linenoise_instance_t *esp_linenoise_create_instance_static(void)
8989
return instance;
9090
}
9191

92-
/**
93-
* @brief Probe the terminal to check weather it supports escape sequences
94-
*
95-
* @param instance The linenoise instance used to check
96-
* @return int 0 if the terminal supports escape sequences
97-
*/
98-
int esp_linenoise_probe(esp_linenoise_instance_t *instance);
99-
10092
/**
10193
* @brief This function is used by the callback function registered by the user
10294
* in order to add completion options given the input string when the

esp_linenoise/src/esp_linenoise.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,9 +1041,9 @@ static void esp_linenoise_sanitize(char *src)
10411041
*dst = 0;
10421042
}
10431043

1044-
int esp_linenoise_probe(esp_linenoise_instance_t *instance)
1044+
int esp_linenoise_probe(esp_linenoise_handle_t handle)
10451045
{
1046-
esp_linenoise_config_t *config = &instance->config;
1046+
esp_linenoise_instance_t *instance = (esp_linenoise_instance_t *)handle;
10471047

10481048
/* Make sure we are in non blocking mode before performing the terminal probing */
10491049
int fd_in = instance->config.in_fd;
@@ -1057,7 +1057,7 @@ int esp_linenoise_probe(esp_linenoise_instance_t *instance)
10571057

10581058
/* Device status request */
10591059
char status_request_str[] = "\x1b[5n";
1060-
config->write_bytes_cb(out_fd, status_request_str, sizeof(status_request_str));
1060+
instance->config.write_bytes_cb(out_fd, status_request_str, sizeof(status_request_str));
10611061

10621062
/* Try to read response */
10631063
int timeout_ms = 500;
@@ -1067,7 +1067,7 @@ int esp_linenoise_probe(esp_linenoise_instance_t *instance)
10671067
usleep(retry_ms * 1000);
10681068
timeout_ms -= retry_ms;
10691069
char c;
1070-
int cb = config->read_bytes_cb(fd_in, &c, 1);
1070+
int cb = instance->config.read_bytes_cb(fd_in, &c, 1);
10711071
if (cb < 0) {
10721072
continue;
10731073
}
@@ -1580,6 +1580,30 @@ esp_err_t esp_linenoise_get_max_cmd_line_length(esp_linenoise_handle_t handle, s
15801580
return ESP_OK;
15811581
}
15821582

1583+
esp_err_t esp_linenoise_set_prompt(esp_linenoise_handle_t handle, const char *prompt)
1584+
{
1585+
ESP_LINENOISE_CHECK_INSTANCE(handle);
1586+
1587+
if (prompt == NULL || strlen(prompt) == 0) {
1588+
return ESP_ERR_INVALID_ARG;
1589+
}
1590+
1591+
((esp_linenoise_instance_t *)handle)->config.prompt = prompt;
1592+
return ESP_OK;
1593+
}
1594+
1595+
esp_err_t esp_linenoise_get_prompt(esp_linenoise_handle_t handle, const char **prompt)
1596+
{
1597+
ESP_LINENOISE_CHECK_INSTANCE(handle);
1598+
1599+
if (prompt == NULL) {
1600+
return ESP_ERR_INVALID_ARG;
1601+
}
1602+
1603+
*prompt = ((esp_linenoise_instance_t *)handle)->config.prompt;
1604+
return ESP_OK;
1605+
}
1606+
15831607
esp_err_t esp_linenoise_get_out_fd(esp_linenoise_handle_t handle, int *fd)
15841608
{
15851609
ESP_LINENOISE_CHECK_INSTANCE(handle);

esp_linenoise/test_apps/main/test_esp_linenoise_get_set.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ TEST_CASE("set and get empty line flag", "[esp_linenoise]")
7272
TEST_ASSERT_EQUAL(ESP_OK, esp_linenoise_delete_instance(h));
7373
}
7474

75+
TEST_CASE("set and get prompt", "[esp_linenoise]")
76+
{
77+
esp_linenoise_handle_t h = get_linenoise_instance_default_config();
78+
79+
const char *test_prompt = "test";
80+
const char *ret_prompt = NULL;
81+
82+
TEST_ASSERT_EQUAL(ESP_OK, esp_linenoise_set_prompt(h, test_prompt));
83+
TEST_ASSERT_EQUAL(ESP_OK, esp_linenoise_get_prompt(h, &ret_prompt));
84+
TEST_ASSERT_TRUE(strcmp(test_prompt, ret_prompt) == 0);
85+
86+
TEST_ASSERT_EQUAL(ESP_OK, esp_linenoise_delete_instance(h));
87+
}
88+
7589
TEST_CASE("default max line length and max history length", "[esp_linenoise]")
7690
{
7791
esp_linenoise_config_t config;

0 commit comments

Comments
 (0)