diff --git a/include/my_ncurses.h b/include/my_ncurses.h index 5c6cf40..daca394 100644 --- a/include/my_ncurses.h +++ b/include/my_ncurses.h @@ -21,6 +21,7 @@ #define KEY_DC CSI(0x7e33) #include +#include typedef struct { diff --git a/src/key_bindings/control_commands.c b/src/key_bindings/control_commands.c index 5237d57..e7e3eb6 100644 --- a/src/key_bindings/control_commands.c +++ b/src/key_bindings/control_commands.c @@ -34,18 +34,32 @@ int eof_command(int key, buffer_t *buffer, env_t *env) return (0); } +int history_size(history_t *hist) +{ + int i = 1; + + if (!hist) + return (0); + while (hist->next) { + hist = hist->next; + i++; + } + return (i); +} + bool set_buffer_to_history(buffer_t *buffer, env_t *env) { history_t *hist = env->history; char *cmd = NULL; int len; + int hist_index = history_size(env->history) - buffer->history_index; if (buffer->history_index == 0) cmd = buffer->saved_buffer != NULL ? buffer->saved_buffer : ""; else - for (int i = 1; i < buffer->history_index && hist; i++) + for (int i = 0; i < hist_index && hist; i++) hist = hist->next; - if (!cmd && (!hist || !(cmd = hist->command))) + if (hist_index < 0 || (!cmd && (!hist || !(cmd = hist->command)))) return (false); if (!buffer->buffer || buffer->size < (len = strlen(cmd))) { buffer->buffer = realloc(buffer->buffer, buffer->size + len); @@ -75,12 +89,4 @@ int down_history_command(int key, buffer_t *buffer, env_t *env) buffer->history_index--; set_buffer_to_history(buffer, env); return (0); -} - -int clear_screen_command(int key, buffer_t *buffer, env_t *env) -{ - my_move(env->window, 0, 0); - my_clrtobot(); - prompt_prepare(buffer, env); - return (0); } \ No newline at end of file diff --git a/src/key_bindings/other_bindings.c b/src/key_bindings/other_bindings.c index 622f66c..112e994 100644 --- a/src/key_bindings/other_bindings.c +++ b/src/key_bindings/other_bindings.c @@ -13,4 +13,12 @@ int quoted_insert_command(int key, buffer_t *buffer, env_t *env) { buffer->quoted_insert = true; return (0); +} + +int clear_screen_command(int key, buffer_t *buffer, env_t *env) +{ + my_move(env->window, 0, 0); + my_clrtobot(); + prompt_prepare(buffer, env); + return (0); } \ No newline at end of file