Solving a bug with the history

This commit is contained in:
Anonymus Raccoon
2020-05-24 18:53:21 +02:00
parent 5932e7ca61
commit 75e860158b
3 changed files with 25 additions and 10 deletions

View File

@@ -21,6 +21,7 @@
#define KEY_DC CSI(0x7e33)
#include <termios.h>
#include <stdio.h>
typedef struct
{

View File

@@ -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);
}

View File

@@ -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);
}