mirror of
https://github.com/zoriya/ash.git
synced 2025-12-06 06:36:17 +00:00
Solving a bug with the history
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
#define KEY_DC CSI(0x7e33)
|
||||
|
||||
#include <termios.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user