Skip to content

Commit

Permalink
Add a one-command history to the Nulix shell
Browse files Browse the repository at this point in the history
When hitting enter, the command buffer is copied to another
buffer before the command split.

On left arrow press, the oldbuffer is copied to the actual one
and the screen is updated

Signed-off-by: lfalkau <[email protected]>
  • Loading branch information
e2r3p13 committed Dec 19, 2022
1 parent 69370be commit b670f8c
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion kernel/nsh/nsh.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Nulix shell
*
* created: 2022/12/07 - xlmod <[email protected]>
* updated: 2022/12/16 - glafond- <glafond-@student.42.fr>
* updated: 2022/12/19 - lfalkau <lfalkau@student.42.fr>
*/

#include <kernel/string.h>
Expand All @@ -21,6 +21,7 @@ extern struct screenbuf *sb_current;

// Buffer where the user input is stored
char nsh_buf[NSH_BUFSIZE];
char nsh_old[NSH_BUFSIZE];
// Buffer where the splitted cmd is stored
char *nsh_cmd[NSH_BUFSIZE / 2 + 1];

Expand Down Expand Up @@ -80,6 +81,9 @@ static void nsh_createcmd() {
char c;
int ibuf = 0;
int icmd = 0;

memset(nsh_old, 0, NSH_BUFSIZE);
memcpy(nsh_old, nsh_buf, nsh_bufindex);
while (ibuf < nsh_bufindex) {
c = nsh_buf[ibuf];
if (c != ' ' && c != 0) {
Expand Down Expand Up @@ -129,6 +133,13 @@ static void nsh_shortcut(struct kbd_event *evt) {
case KEY_PAGE_UP:
sb_scroll_top(sb_current);
break;
case KEY_CURSOR_LEFT:
for (int i = 0; i < nsh_bufindex; i++)
sb_putchar(sb_current, '\b');
nsh_bufindex = strlen(nsh_old);
memcpy(nsh_buf, nsh_old, nsh_bufindex);
sb_putstr(sb_current, nsh_buf);
break;
default:
break;
}
Expand Down

0 comments on commit b670f8c

Please sign in to comment.