working prompt update

This commit is contained in:
Данила Горнушко 2023-11-28 12:24:21 +03:00
parent 860bdd833d
commit 563c2fd641
2 changed files with 19 additions and 15 deletions

View file

@ -928,7 +928,7 @@ int linenoiseEditStart(struct linenoiseState *l) {
/* Populate the linenoise state that we pass to functions implementing /* Populate the linenoise state that we pass to functions implementing
* specific editing functionalities. */ * specific editing functionalities. */
l->in_completion = 0; l->in_completion = 0;
l->plen = strlen(l->prompt); // l->plen = strlen(l->prompt);
l->oldpos = l->pos = 0; l->oldpos = l->pos = 0;
l->len = 0; l->len = 0;
l->cols = getColumns(); l->cols = getColumns();
@ -944,16 +944,16 @@ int linenoiseEditStart(struct linenoiseState *l) {
xSemaphoreTake(stdout_taken_sem, portMAX_DELAY); xSemaphoreTake(stdout_taken_sem, portMAX_DELAY);
if (!dumbmode) { if (!dumbmode) {
linenoiseHistoryAdd(""); linenoiseHistoryAdd("");
int pos1 = getCursorPosition(); // int pos1 = getCursorPosition();
if (fwrite(l->prompt,l->plen,1,stdout) == -1) { if (fwrite(l->prompt,l->plen,1,stdout) == -1) {
xSemaphoreGive(stdout_taken_sem); xSemaphoreGive(stdout_taken_sem);
return -1; return -1;
} }
flushWrite(); flushWrite();
int pos2 = getCursorPosition(); // int pos2 = getCursorPosition();
if (pos1 >= 0 && pos2 >= 0) { // if (pos1 >= 0 && pos2 >= 0) {
l->plen = pos2 - pos1; // l->plen = pos2 - pos1;
} // }
} else { } else {
if (fwrite(l->prompt,l->plen,1,stdout) == -1) { if (fwrite(l->prompt,l->plen,1,stdout) == -1) {
xSemaphoreGive(stdout_taken_sem); xSemaphoreGive(stdout_taken_sem);

View file

@ -36,44 +36,48 @@ struct linenoiseState ls;
SemaphoreHandle_t console_taken_sem; SemaphoreHandle_t console_taken_sem;
static void update_prompt() { static void update_prompt() {
static char* text; char text[30];
int text_len;
static char* prompt_color; static char* prompt_color;
text[0] = '\0';
switch (curr_can_state.state) { switch (curr_can_state.state) {
case CAN_NOT_INSTALLED: case CAN_NOT_INSTALLED:
text = "not installed"; strcat(text, "not installed");
prompt_color = LOG_COLOR(LOG_COLOR_RED); prompt_color = LOG_COLOR(LOG_COLOR_RED);
break; break;
case CAN_STOPPED: case CAN_STOPPED:
text = "stopped"; strcat(text, "stopped");
prompt_color = LOG_COLOR(LOG_COLOR_RED); prompt_color = LOG_COLOR(LOG_COLOR_RED);
break; break;
case CAN_ERROR_ACTIVE: case CAN_ERROR_ACTIVE:
text = "error active"; strcat(text, "error active");
prompt_color = LOG_COLOR(LOG_COLOR_GREEN); prompt_color = LOG_COLOR(LOG_COLOR_GREEN);
break; break;
case CAN_ERROR_PASSIVE: case CAN_ERROR_PASSIVE:
text = "error passive"; strcat(text, "error passive");
prompt_color = LOG_COLOR(LOG_COLOR_BROWN); prompt_color = LOG_COLOR(LOG_COLOR_BROWN);
break; break;
case CAN_BUF_OFF: case CAN_BUF_OFF:
text = "bus off"; strcat(text, "bus off");
prompt_color = LOG_COLOR(LOG_COLOR_RED); prompt_color = LOG_COLOR(LOG_COLOR_RED);
break; break;
case CAN_RECOVERING: case CAN_RECOVERING:
text = "recovering"; strcat(text, "recovering");
prompt_color = LOG_COLOR(LOG_COLOR_RED); prompt_color = LOG_COLOR(LOG_COLOR_RED);
break; break;
} }
strcat(text, " > ");
prompt_buf[0] = '\0'; prompt_buf[0] = '\0';
if (use_colors) { if (use_colors) {
strcat(prompt_buf, prompt_color); strcat(prompt_buf, prompt_color);
strcat(prompt_buf, text); strcat(prompt_buf, text);
strcat(prompt_buf, " > ");
strcat(prompt_buf, LOG_RESET_COLOR); strcat(prompt_buf, LOG_RESET_COLOR);
} else { } else {
strcat(prompt_buf, text); strcat(prompt_buf, text);
} }
text_len = strlen(text);
ls.prompt = prompt_buf; ls.prompt = prompt_buf;
ls.plen = text_len;
} }
void console_task_tx(void* arg) { void console_task_tx(void* arg) {
@ -84,7 +88,7 @@ void console_task_tx(void* arg) {
size_t msg_to_print_size; size_t msg_to_print_size;
while(1) { while(1) {
msg_to_print = (char *)xRingbufferReceive(can_messages, &msg_to_print_size, prompt_timeout); msg_to_print = (char *)xRingbufferReceive(can_messages, &msg_to_print_size, prompt_timeout);
// update_prompt(); update_prompt();
xSemaphoreTake(console_taken_sem, portMAX_DELAY); xSemaphoreTake(console_taken_sem, portMAX_DELAY);
xSemaphoreTake(stdout_taken_sem, portMAX_DELAY); xSemaphoreTake(stdout_taken_sem, portMAX_DELAY);
linenoiseHide(&ls); linenoiseHide(&ls);