7 #include "system/error.h"
20 History *create_history(size_t capacity)
27 History *history = PUSH_LT(
29 malloc(sizeof(History)),
31 if (history == NULL) {
32 throw_error(ERROR_TYPE_LIBC);
37 history->capacity = capacity;
41 history->buffer = PUSH_LT(lt, calloc(capacity, sizeof(char*)), free);
42 if (history->buffer == NULL) {
43 throw_error(ERROR_TYPE_LIBC);
50 void destroy_history(History *history)
54 for (size_t i = 0; i < history->capacity; ++i) {
55 if (history->buffer[i] != NULL) {
56 free(history->buffer[i]);
60 RETURN_LT0(history->lt);
63 int history_push(History *history, const char *command)
68 const size_t next_begin = (history->begin + 1) % history->capacity;
70 if (history->buffer[history->begin] != NULL) {
71 free(history->buffer[history->begin]);
74 history->buffer[history->begin] = string_duplicate(command, NULL);
76 if (history->buffer[history->begin] == NULL) {
80 history->begin = next_begin;
81 history->cursor = next_begin;
86 const char *history_current(History *history)
89 return history->buffer[history->cursor];
92 void history_prev(History *history)
95 if (history->cursor == 0) {
96 history->cursor = history->capacity - 1;
102 void history_next(History *history)
105 history->cursor = (history->cursor + 1) % history->capacity;