(#354) Integrate history with console

master
rexim 2018-10-08 01:12:07 +07:00
parent cb1713d393
commit 5acfea07dc
5 changed files with 56 additions and 10 deletions

View File

@ -165,6 +165,7 @@ int console_handle_event(Console *console,
case SDLK_RETURN: {
const char *source_code = edit_field_as_text(console->edit_field);
/* TODO: console pushes empty strings to the history */
if (history_push(console->history, source_code) < 0) {
return -1;
}
@ -209,6 +210,20 @@ int console_handle_event(Console *console,
gc_collect(console->gc, console->scope.expr);
edit_field_clean(console->edit_field);
} return 0;
case SDLK_UP: {
edit_field_replace(
console->edit_field,
history_current(console->history));
history_prev(console->history);
} return 0;
case SDLK_DOWN: {
edit_field_replace(
console->edit_field,
history_current(console->history));
history_next(console->history);
} return 0;
}
break;
}

View File

@ -245,7 +245,16 @@ static void edit_field_insert_char(Edit_field *edit_field, char c)
void edit_field_clean(Edit_field *edit_field)
{
assert(edit_field);
edit_field->cursor = 0;
edit_field->buffer_size = 0;
edit_field->buffer[0] = 0;
}
void edit_field_replace(Edit_field *edit_field, const char *text)
{
assert(edit_field);
assert(text);
/* TODO: edit_field_replace is not implemented */
}

View File

@ -25,6 +25,7 @@ int edit_field_handle_event(Edit_field *edit_field,
const char *edit_field_as_text(const Edit_field *edit_field);
void edit_field_replace(Edit_field *edit_field, const char *text);
void edit_field_clean(Edit_field *edit_field);
#endif // EDIT_FIELD_H_

View File

@ -12,8 +12,9 @@ struct History
Lt *lt;
char **buffer;
size_t cursor;
size_t begin;
size_t capacity;
size_t cursor;
};
History *create_history(size_t capacity)
@ -34,6 +35,7 @@ History *create_history(size_t capacity)
history->lt = lt;
history->capacity = capacity;
history->begin = 0;
history->cursor = 0;
history->buffer = PUSH_LT(lt, calloc(capacity, sizeof(char*)), free);
@ -63,25 +65,42 @@ int history_push(History *history, const char *command)
assert(history);
assert(command);
const size_t next_cursor = (history->cursor + 1) % history->capacity;
const size_t next_begin = (history->begin + 1) % history->capacity;
if (history->buffer[history->cursor] != NULL) {
free(history->buffer[history->cursor]);
if (history->buffer[history->begin] != NULL) {
free(history->buffer[history->begin]);
}
history->buffer[history->cursor] = string_duplicate(command, NULL);
history->buffer[history->begin] = string_duplicate(command, NULL);
if (history->buffer[history->cursor] == NULL) {
if (history->buffer[history->begin] == NULL) {
return -1;
}
history->cursor = next_cursor;
history->begin = next_begin;
history->cursor = next_begin;
return 0;
}
const char *history_get(History *history, size_t i)
const char *history_current(History *history)
{
assert(history);
return history->buffer[(history->cursor + i) % history->capacity];
return history->buffer[history->cursor];
}
void history_prev(History *history)
{
assert(history);
if (history->cursor == 0) {
history->cursor = history->capacity - 1;
} else {
history->cursor--;
}
}
void history_next(History *history)
{
assert(history);
history->cursor = (history->cursor + 1) % history->capacity;
}

View File

@ -7,6 +7,8 @@ History *create_history(size_t capacity);
void destroy_history(History *history);
int history_push(History *history, const char *command);
const char *history_get(History *history, size_t i);
const char *history_current(History *history);
void history_prev(History *history);
void history_next(History *history);
#endif // HISTORY_H_