diff --git a/FIX.md b/FIX.md index c81aa45..82f7658 100644 --- a/FIX.md +++ b/FIX.md @@ -1,8 +1,9 @@ # To fix - [x] Crash with history (!x) -- [ ] Up arrow that not get args +- [x] Up arrow that not get args - [ ] !x no args - [ ] Conditional jump/ invalid read with cd (PWD env var set) - [x] Alias loop - [ ] All AFL crashes - [ ] Fix CTRL+E and -> +- [ ] Multi line editing diff --git a/TODO.md b/TODO.md index 3be1685..2534448 100644 --- a/TODO.md +++ b/TODO.md @@ -5,3 +5,4 @@ - [ ] Finish line editing - [ ] Syntax highlighting - [ ] CTRL+R +- [ ] Git integration diff --git a/src/repl/key_arrow.c b/src/repl/key_arrow.c index 0077234..4054661 100644 --- a/src/repl/key_arrow.c +++ b/src/repl/key_arrow.c @@ -13,6 +13,27 @@ #include "readline.h" #include "u_str.h" +static +bool cat_history_entry(buff_t *buff, size_t i, his_command_t *cmd_history) +{ + if (!ensure_buff_av_capacity(buff, + strlen(cmd_history[i].command))) + return false; + strcpy(buff->str, cmd_history[i].command); + if (buff->str == nullptr) + return true; + buff->sz = strlen(buff->str); + if (cmd_history[i].arg != nullptr) { + if (!ensure_buff_av_capacity(buff, + strlen(cmd_history[i].arg) + 1)) + return false; + buff->str[buff->sz] = ' '; + strcat(buff->str, cmd_history[i].arg); + buff->sz += strlen(cmd_history[i].arg) + 1; + } + return true; +} + bool handle_key_arrow_up( readline_helper_t *rh, exec_ctx_t *ec, buff_t *buff) { @@ -20,17 +41,8 @@ bool handle_key_arrow_up( if (cmd_history->sz && rh->history_idx) { rh->history_idx--; - ensure_buff_av_capacity(buff, - strlen(cmd_history[rh->history_idx].command)); - strcpy(buff->str, cmd_history[rh->history_idx].command); - if (buff->str == nullptr) - return true; - buff->sz = strlen(buff->str); - if (cmd_history[rh->history_idx].arg) { - ensure_buff_av_capacity(buff, - strlen(cmd_history[rh->history_idx].arg)); - strcat(buff->str, cmd_history[rh->history_idx].arg); - } + if (!cat_history_entry(buff, rh->history_idx, cmd_history)) + return false; rh->cursor = buff->sz; refresh_line(rh); } @@ -44,17 +56,8 @@ bool handle_key_arrow_down( if (cmd_history->sz && rh->history_idx + 1 < cmd_history->sz) { rh->history_idx++; - ensure_buff_av_capacity(buff, - strlen(cmd_history[rh->history_idx].command)); - strcpy(buff->str, cmd_history[rh->history_idx].command); - if (buff->str == nullptr) - return true; - buff->sz = strlen(buff->str); - if (cmd_history[rh->history_idx].arg) { - ensure_buff_av_capacity(buff, - strlen(cmd_history[rh->history_idx].arg)); - strcat(buff->str, cmd_history[rh->history_idx].arg); - } + if (!cat_history_entry(buff, rh->history_idx, cmd_history)) + return false; rh->cursor = buff->sz; refresh_line(rh); } diff --git a/src/repl/key_control.c b/src/repl/key_control.c index ca6fe35..7431264 100644 --- a/src/repl/key_control.c +++ b/src/repl/key_control.c @@ -5,10 +5,12 @@ ** _ */ +#include #include #include "key_handler.h" #include "repl.h" +#include "vt100_esc_codes.h" bool handle_key_ctrl_c(readline_helper_t *rh, exec_ctx_t *ec, buff_t *buff) { @@ -27,21 +29,33 @@ bool handle_key_ctrl_d(readline_helper_t *, exec_ctx_t *ec, buff_t *buff) return true; } -bool handle_key_ctrl_e(readline_helper_t *rh, exec_ctx_t *, buff_t *buff) +bool handle_key_ctrl_l(readline_helper_t *, exec_ctx_t *ec, buff_t *) { - rh->cursor = buff->sz; + WRITE_CONST(STDOUT_FILENO, ESC "[2J"); + WRITE_CONST(STDOUT_FILENO, ESC "[H"); + print_shell_prompt(ec); + return false; +} + +bool handle_backspace(readline_helper_t *rh, exec_ctx_t *, buff_t *buff) +{ + if (rh->cursor == 0) + return false; + rh->cursor--; + memmove(&buff->str[rh->cursor], &buff->str[rh->cursor + 1], + buff->sz - rh->cursor); + buff->sz--; refresh_line(rh); return false; } -bool handle_key_ctrl_a(readline_helper_t *rh, exec_ctx_t *, buff_t *) +bool handle_delete(readline_helper_t *rh, exec_ctx_t *, buff_t *buff) { - rh->cursor = 0; + if (rh->cursor >= buff->sz) + return false; + memmove(&buff->str[rh->cursor], &buff->str[rh->cursor + 1], + buff->sz - rh->cursor); + buff->sz--; refresh_line(rh); return false; } - -bool handle_key_ctrl_b(readline_helper_t *rh, exec_ctx_t *ec, buff_t *buff) -{ - return handle_key_arrow_left(rh, ec, buff); -} diff --git a/src/repl/key_control2.c b/src/repl/key_control2.c deleted file mode 100644 index 674402e..0000000 --- a/src/repl/key_control2.c +++ /dev/null @@ -1,49 +0,0 @@ -/* -** EPITECH PROJECT, 2025 -** __ -** File description: -** _ -*/ - -#include -#include - -#include "key_handler.h" -#include "repl.h" -#include "vt100_esc_codes.h" - -bool handle_key_ctrl_f(readline_helper_t *rh, exec_ctx_t *ec, buff_t *buff) -{ - return handle_key_arrow_right(rh, ec, buff); -} - -bool handle_key_ctrl_l(readline_helper_t *, exec_ctx_t *ec, buff_t *) -{ - WRITE_CONST(STDOUT_FILENO, ESC "[2J"); - WRITE_CONST(STDOUT_FILENO, ESC "[H"); - print_shell_prompt(ec); - return false; -} - -bool handle_backspace(readline_helper_t *rh, exec_ctx_t *, buff_t *buff) -{ - if (rh->cursor == 0) - return false; - rh->cursor--; - memmove(&buff->str[rh->cursor], &buff->str[rh->cursor + 1], - buff->sz - rh->cursor); - buff->sz--; - refresh_line(rh); - return false; -} - -bool handle_delete(readline_helper_t *rh, exec_ctx_t *, buff_t *buff) -{ - if (rh->cursor >= buff->sz) - return false; - memmove(&buff->str[rh->cursor], &buff->str[rh->cursor + 1], - buff->sz - rh->cursor); - buff->sz--; - refresh_line(rh); - return false; -} diff --git a/src/repl/key_control_edit.c b/src/repl/key_control_edit.c new file mode 100644 index 0000000..3d87785 --- /dev/null +++ b/src/repl/key_control_edit.c @@ -0,0 +1,34 @@ +/* +** EPITECH PROJECT, 2025 +** __ +** File description: +** _ +*/ + +#include + +#include "key_handler.h" + +bool handle_key_ctrl_e(readline_helper_t *rh, exec_ctx_t *, buff_t *buff) +{ + rh->cursor = buff->sz; + refresh_line(rh); + return false; +} + +bool handle_key_ctrl_a(readline_helper_t *rh, exec_ctx_t *, buff_t *) +{ + rh->cursor = 0; + refresh_line(rh); + return false; +} + +bool handle_key_ctrl_b(readline_helper_t *rh, exec_ctx_t *ec, buff_t *buff) +{ + return handle_key_arrow_left(rh, ec, buff); +} + +bool handle_key_ctrl_f(readline_helper_t *rh, exec_ctx_t *ec, buff_t *buff) +{ + return handle_key_arrow_right(rh, ec, buff); +}