Skip to content

Commit 598f343

Browse files
danieldegrassekartben
authored andcommitted
shell: handle reprint correctly when output crosses line boundary
When reprinting the shell command buffer with long user inputs, the reprinted buffer may cross a line boundary, and require a newline to be printed. For these cases, print the command buffer character by character, inserting newlines as appropriate. Fixes #82155 Signed-off-by: Daniel DeGrasse <[email protected]>
1 parent 50f6142 commit 598f343

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

subsys/shell/shell_ops.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,29 @@ static void reprint_from_cursor(const struct shell *sh, uint16_t diff,
250250
z_shell_raw_fprintf(sh->fprintf_ctx, "*");
251251
}
252252
} else {
253-
z_shell_fprintf(sh, SHELL_NORMAL, "%s",
254-
&sh->ctx->cmd_buff[sh->ctx->cmd_buff_pos]);
253+
/* Check if the reprint will cross a line boundary */
254+
int line_len = sh->ctx->cmd_buff_len + z_shell_strlen(sh->ctx->prompt);
255+
int buff_pos = sh->ctx->cmd_buff_pos + z_shell_strlen(sh->ctx->prompt);
256+
257+
if ((buff_pos / sh->ctx->vt100_ctx.cons.terminal_wid) !=
258+
(line_len / sh->ctx->vt100_ctx.cons.terminal_wid)) {
259+
/*
260+
* Reprint will take multiple lines.
261+
* Print each character directly.
262+
*/
263+
int pos = sh->ctx->cmd_buff_pos;
264+
265+
while (buff_pos < line_len) {
266+
if (buff_pos++ % sh->ctx->vt100_ctx.cons.terminal_wid == 0U) {
267+
z_cursor_next_line_move(sh);
268+
}
269+
z_shell_fprintf(sh, SHELL_NORMAL, "%c",
270+
sh->ctx->cmd_buff[pos++]);
271+
}
272+
} else {
273+
z_shell_fprintf(sh, SHELL_NORMAL, "%s",
274+
&sh->ctx->cmd_buff[sh->ctx->cmd_buff_pos]);
275+
}
255276
}
256277
sh->ctx->cmd_buff_pos = sh->ctx->cmd_buff_len;
257278

0 commit comments

Comments
 (0)