7 #include "system/error.h"
9 #include "system/nth_alloc.h"
21 History *create_history(size_t capacity)
28 History *history = PUSH_LT(
30 nth_alloc(sizeof(History)),
32 if (history == NULL) {
33 throw_error(ERROR_TYPE_LIBC);
38 history->capacity = capacity;
42 history->buffer = PUSH_LT(lt, calloc(capacity, sizeof(char*)), free);
43 if (history->buffer == NULL) {
44 throw_error(ERROR_TYPE_LIBC);
51 void destroy_history(History *history)
55 for (size_t i = 0; i < history->capacity; ++i) {
56 if (history->buffer[i] != NULL) {
57 free(history->buffer[i]);
61 RETURN_LT0(history->lt);
64 int history_push(History *history, const char *command)
69 const size_t next_begin = (history->begin + 1) % history->capacity;
71 if (history->buffer[history->begin] != NULL) {
72 free(history->buffer[history->begin]);
75 history->buffer[history->begin] = string_duplicate(command, NULL);
77 if (history->buffer[history->begin] == NULL) {
81 history->begin = next_begin;
82 history->cursor = next_begin;
87 const char *history_current(History *history)
90 return history->buffer[history->cursor];
93 void history_prev(History *history)
96 if (history->cursor == 0) {
97 history->cursor = history->capacity - 1;
103 void history_next(History *history)
106 history->cursor = (history->cursor + 1) % history->capacity;