Skip to content

Commit

Permalink
shell: rework vt100 commands storage
Browse files Browse the repository at this point in the history
VT100 commands are now stored as strings rather than character arrays.
This change will make it easier to create a unified macro for sending
all VT100 commands.

Signed-off-by: Jakub Rzeszutko <jakub.rzeszutko@nordicsemi.no>
  • Loading branch information
Jakub Rzeszutko authored and cfriedt committed Sep 29, 2021
1 parent 851cccb commit 0a50ebe
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 592 deletions.
3 changes: 2 additions & 1 deletion include/shell/shell_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ extern "C" {
#endif

enum shell_vt100_color {
SHELL_VT100_COLOR_DEFAULT,
SHELL_VT100_COLOR_BLACK,
SHELL_VT100_COLOR_RED,
SHELL_VT100_COLOR_GREEN,
Expand All @@ -22,6 +21,8 @@ enum shell_vt100_color {
SHELL_VT100_COLOR_CYAN,
SHELL_VT100_COLOR_WHITE,

SHELL_VT100_COLOR_DEFAULT,

VT100_COLOR_END
};

Expand Down
71 changes: 47 additions & 24 deletions subsys/shell/shell_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,35 @@
#include <ctype.h>
#include "shell_ops.h"

#define CMD_CURSOR_LEN 8
void z_shell_op_cursor_vert_move(const struct shell *shell, int32_t delta)
{
if (delta != 0) {
z_shell_raw_fprintf(shell->fprintf_ctx, "\033[%d%c",
delta > 0 ? delta : -delta,
delta > 0 ? 'A' : 'B');
char dir = delta > 0 ? 'A' : 'B';

if (delta == 0) {
return;
}

if (delta < 0) {
delta = -delta;
}

Z_SHELL_VT100_CMD(shell, "\e[%d%c", delta, dir);
}

void z_shell_op_cursor_horiz_move(const struct shell *shell, int32_t delta)
{
if (delta != 0) {
z_shell_raw_fprintf(shell->fprintf_ctx, "\033[%d%c",
delta > 0 ? delta : -delta,
delta > 0 ? 'C' : 'D');
char dir = delta > 0 ? 'C' : 'D';

if (delta == 0) {
return;
}

if (delta < 0) {
delta = -delta;
}

Z_SHELL_VT100_CMD(shell, "\e[%d%c", delta, dir);
}

/* Function returns true if command length is equal to multiplicity of terminal
Expand Down Expand Up @@ -285,8 +298,8 @@ static void char_replace(const struct shell *shell, char data)

void z_shell_op_char_insert(const struct shell *shell, char data)
{
if (shell->ctx->internal.flags.insert_mode &&
(shell->ctx->cmd_buff_len != shell->ctx->cmd_buff_pos)) {
if (z_flag_insert_mode_get(shell) &&
(shell->ctx->cmd_buff_len != shell->ctx->cmd_buff_pos)) {
char_replace(shell, data);
} else {
data_insert(shell, &data, 1);
Expand Down Expand Up @@ -418,22 +431,33 @@ void z_shell_print_stream(const void *user_ctx, const char *data, size_t len)
static void vt100_bgcolor_set(const struct shell *shell,
enum shell_vt100_color bgcolor)
{
if (!IS_ENABLED(CONFIG_SHELL_VT100_COLORS)) {
return;
}

if (bgcolor >= VT100_COLOR_END) {
return;
}

if ((bgcolor == SHELL_NORMAL) ||
(shell->ctx->vt100_ctx.col.bgcol == bgcolor)) {
return;
}

/* -1 because default value is first in enum */
uint8_t cmd[] = SHELL_VT100_BGCOLOR(bgcolor - 1);

shell->ctx->vt100_ctx.col.bgcol = bgcolor;
z_shell_raw_fprintf(shell->fprintf_ctx, "%s", cmd);

Z_SHELL_VT100_CMD(shell, "\e[403%dm", bgcolor);
}

void z_shell_vt100_color_set(const struct shell *shell,
enum shell_vt100_color color)
{
if (!IS_ENABLED(CONFIG_SHELL_VT100_COLORS)) {
return;
}

if (color >= VT100_COLOR_END) {
return;
}

if (shell->ctx->vt100_ctx.col.col == color) {
return;
Expand All @@ -442,20 +466,19 @@ void z_shell_vt100_color_set(const struct shell *shell,
shell->ctx->vt100_ctx.col.col = color;

if (color != SHELL_NORMAL) {

uint8_t cmd[] = SHELL_VT100_COLOR(color - 1);

z_shell_raw_fprintf(shell->fprintf_ctx, "%s", cmd);
Z_SHELL_VT100_CMD(shell, "\e[1;3%dm", color);
} else {
static const uint8_t cmd[] = SHELL_VT100_MODESOFF;

z_shell_raw_fprintf(shell->fprintf_ctx, "%s", cmd);
Z_SHELL_VT100_CMD(shell, SHELL_VT100_MODESOFF);
}
}

void z_shell_vt100_colors_restore(const struct shell *shell,
const struct shell_vt100_colors *color)
const struct shell_vt100_colors *color)
{
if (!IS_ENABLED(CONFIG_SHELL_VT100_COLORS)) {
return;
}

z_shell_vt100_color_set(shell, color->col);
vt100_bgcolor_set(shell, color->bgcol);
}
Expand All @@ -464,7 +487,7 @@ void z_shell_vfprintf(const struct shell *shell, enum shell_vt100_color color,
const char *fmt, va_list args)
{
if (IS_ENABLED(CONFIG_SHELL_VT100_COLORS) &&
shell->ctx->internal.flags.use_colors &&
z_flag_use_colors_get(shell) &&
(color != shell->ctx->vt100_ctx.col.col)) {
struct shell_vt100_colors col;

Expand Down
8 changes: 3 additions & 5 deletions subsys/shell/shell_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ static inline void z_shell_raw_fprintf(const struct shell_fprintf *const ctx,
va_end(args);
}

/* Macro to send VT100 commands. */
#define Z_SHELL_VT100_CMD(_shell_, _cmd_) \
/* Macro to send VT100 command. */
#define Z_SHELL_VT100_CMD(_shell_, ...) \
do { \
if (!IS_ENABLED(CONFIG_SHELL_VT100_COMMANDS)) \
break; \
\
static const char cmd[] = _cmd_; \
z_shell_raw_fprintf(_shell_->fprintf_ctx, "%s", cmd); \
z_shell_raw_fprintf(_shell_->fprintf_ctx, __VA_ARGS__); \
} while (0)

/* Function sends VT100 command to clear the screen from cursor position to
Expand Down
Loading

0 comments on commit 0a50ebe

Please sign in to comment.