]> git.lizzy.rs Git - nothing.git/blob - src/ui/history.c
Merge pull request #505 from tsoding/108
[nothing.git] / src / ui / history.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4
5 #include "history.h"
6 #include "str.h"
7 #include "system/lt.h"
8 #include "system/nth_alloc.h"
9
10 struct History
11 {
12     Lt *lt;
13
14     char **buffer;
15     size_t begin;
16     size_t capacity;
17     size_t cursor;
18 };
19
20 History *create_history(size_t capacity)
21 {
22     Lt *lt = create_lt();
23     if (lt == NULL) {
24         return NULL;
25     }
26
27     History *history = PUSH_LT(
28         lt,
29         nth_alloc(sizeof(History)),
30         free);
31     if (history == NULL) {
32         RETURN_LT(lt, NULL);
33     }
34     history->lt = lt;
35
36     history->capacity = capacity;
37     history->begin = 0;
38     history->cursor = 0;
39
40     history->buffer = PUSH_LT(lt, nth_calloc(capacity, sizeof(char*)), free);
41     if (history->buffer == NULL) {
42         RETURN_LT(lt, NULL);
43     }
44
45     return history;
46 }
47
48 void destroy_history(History *history)
49 {
50     assert(history);
51
52     for (size_t i = 0; i < history->capacity; ++i) {
53         if (history->buffer[i] != NULL) {
54             free(history->buffer[i]);
55         }
56     }
57
58     RETURN_LT0(history->lt);
59 }
60
61 int history_push(History *history, const char *command)
62 {
63     assert(history);
64     assert(command);
65
66     const size_t next_begin = (history->begin + 1) % history->capacity;
67
68     if (history->buffer[history->begin] != NULL) {
69         free(history->buffer[history->begin]);
70     }
71
72     history->buffer[history->begin] = string_duplicate(command, NULL);
73
74     if (history->buffer[history->begin] == NULL) {
75         return -1;
76     }
77
78     history->begin = next_begin;
79     history->cursor = next_begin;
80
81     return 0;
82 }
83
84 const char *history_current(History *history)
85 {
86     assert(history);
87     return history->buffer[history->cursor];
88 }
89
90 void history_prev(History *history)
91 {
92     assert(history);
93     if (history->cursor == 0) {
94         history->cursor = history->capacity - 1;
95     } else {
96         history->cursor--;
97     }
98 }
99
100 void history_next(History *history)
101 {
102     assert(history);
103     history->cursor = (history->cursor + 1) % history->capacity;
104 }