From 563c2fd6417ea8178229bcd2ca32883195cdb3b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D0=BB=D0=B0=20=D0=93=D0=BE=D1=80?= =?UTF-8?q?=D0=BD=D1=83=D1=88=D0=BA=D0=BE?= Date: Tue, 28 Nov 2023 12:24:21 +0300 Subject: [PATCH] working prompt update --- components/console/linenoise/linenoise.c | 12 ++++++------ main/console.c | 22 +++++++++++++--------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/components/console/linenoise/linenoise.c b/components/console/linenoise/linenoise.c index 4f4b23a..5503798 100644 --- a/components/console/linenoise/linenoise.c +++ b/components/console/linenoise/linenoise.c @@ -928,7 +928,7 @@ int linenoiseEditStart(struct linenoiseState *l) { /* Populate the linenoise state that we pass to functions implementing * specific editing functionalities. */ l->in_completion = 0; - l->plen = strlen(l->prompt); + // l->plen = strlen(l->prompt); l->oldpos = l->pos = 0; l->len = 0; l->cols = getColumns(); @@ -944,16 +944,16 @@ int linenoiseEditStart(struct linenoiseState *l) { xSemaphoreTake(stdout_taken_sem, portMAX_DELAY); if (!dumbmode) { linenoiseHistoryAdd(""); - int pos1 = getCursorPosition(); + // int pos1 = getCursorPosition(); if (fwrite(l->prompt,l->plen,1,stdout) == -1) { xSemaphoreGive(stdout_taken_sem); return -1; } flushWrite(); - int pos2 = getCursorPosition(); - if (pos1 >= 0 && pos2 >= 0) { - l->plen = pos2 - pos1; - } + // int pos2 = getCursorPosition(); + // if (pos1 >= 0 && pos2 >= 0) { + // l->plen = pos2 - pos1; + // } } else { if (fwrite(l->prompt,l->plen,1,stdout) == -1) { xSemaphoreGive(stdout_taken_sem); diff --git a/main/console.c b/main/console.c index efeba18..cc5fd5f 100644 --- a/main/console.c +++ b/main/console.c @@ -36,44 +36,48 @@ struct linenoiseState ls; SemaphoreHandle_t console_taken_sem; static void update_prompt() { - static char* text; + char text[30]; + int text_len; static char* prompt_color; + text[0] = '\0'; switch (curr_can_state.state) { case CAN_NOT_INSTALLED: - text = "not installed"; + strcat(text, "not installed"); prompt_color = LOG_COLOR(LOG_COLOR_RED); break; case CAN_STOPPED: - text = "stopped"; + strcat(text, "stopped"); prompt_color = LOG_COLOR(LOG_COLOR_RED); break; case CAN_ERROR_ACTIVE: - text = "error active"; + strcat(text, "error active"); prompt_color = LOG_COLOR(LOG_COLOR_GREEN); break; case CAN_ERROR_PASSIVE: - text = "error passive"; + strcat(text, "error passive"); prompt_color = LOG_COLOR(LOG_COLOR_BROWN); break; case CAN_BUF_OFF: - text = "bus off"; + strcat(text, "bus off"); prompt_color = LOG_COLOR(LOG_COLOR_RED); break; case CAN_RECOVERING: - text = "recovering"; + strcat(text, "recovering"); prompt_color = LOG_COLOR(LOG_COLOR_RED); break; } + strcat(text, " > "); prompt_buf[0] = '\0'; if (use_colors) { strcat(prompt_buf, prompt_color); strcat(prompt_buf, text); - strcat(prompt_buf, " > "); strcat(prompt_buf, LOG_RESET_COLOR); } else { strcat(prompt_buf, text); } + text_len = strlen(text); ls.prompt = prompt_buf; + ls.plen = text_len; } void console_task_tx(void* arg) { @@ -84,7 +88,7 @@ void console_task_tx(void* arg) { size_t msg_to_print_size; while(1) { msg_to_print = (char *)xRingbufferReceive(can_messages, &msg_to_print_size, prompt_timeout); - // update_prompt(); + update_prompt(); xSemaphoreTake(console_taken_sem, portMAX_DELAY); xSemaphoreTake(stdout_taken_sem, portMAX_DELAY); linenoiseHide(&ls);