-
Notifications
You must be signed in to change notification settings - Fork 2
/
tc_history.h
272 lines (237 loc) · 6.59 KB
/
tc_history.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* tc_history.h: Simple terminal history handling.
*
* DEPENDS: tc_string
* VERSION: 0.0.3 (2016-09-11)
* LICENSE: CC0 & Boost (dual-licensed)
* AUTHOR: Tim Cas
* URL: https://github.com/darkuranium/tclib
*
* VERSION HISTORY:
* 0.0.3 added TC_MALLOC and friends
* 0.0.2 made the library compile with a C++ compiler
* 0.0.1 initial public release
*
* TODOs:
* - read history from file or stream
* - multiline support
*/
#ifndef TC_HISTORY_H_
#define TC_HISTORY_H_
#include "tc_string.h"
#ifdef __cplusplus
extern "C" {
#endif
struct TC__HistoryEntry;
typedef struct TC_History
{
int maxlen;
size_t mem, head, tail;
struct TC__HistoryEntry* entries;
size_t vpos, hpos;
} TC_History;
TC_History* tchist_init(TC_History* hist, int maxlen);
void tchist_deinit(TC_History* hist);
TC_String* tchist_get_string(TC_History* hist);
size_t tchist_get_hpos(TC_History* hist);
void tchist_cmd_vmove_full(TC_History* hist, int down);
void tchist_cmd_hmove_full(TC_History* hist, int right);
void tchist_cmd_vmove(TC_History* hist, int down);
void tchist_cmd_hmove(TC_History* hist, int right);
void tchist_str_input(TC_History* hist, const char* str, int len, int insert);
void tchist_str_delete(TC_History* hist, int len);
void tchist_str_clear(TC_History* hist);
TC_String* tchist_exec(TC_History* hist);
#ifdef __cplusplus
}
#endif
#endif /* TC_HISTORY_H_ */
#ifdef TC_HISTORY_IMPLEMENTATION
#undef TC_HISTORY_IMPLEMENTATION
#include <stdlib.h>
#ifndef TC__STATIC_CAST
#ifdef __cplusplus
#define TC__STATIC_CAST(T,v) static_cast<T>(v)
#else
#define TC__STATIC_CAST(T,v) ((T)(v))
#endif
#endif /* TC__STATIC_CAST */
/* no cast done to preserve undefined function warnings in C */
#ifndef TC__VOID_CAST
#ifdef __cplusplus
#define TC__VOID_CAST(T,v) TC__STATIC_CAST(T,v)
#else
#define TC__VOID_CAST(T,v) (v)
#endif
#endif /* TC__VOID_CAST */
#ifndef TC_MALLOC
#define TC_MALLOC(size) malloc(size)
#endif /* TC_MALLOC */
#ifndef TC_FREE
#define TC_FREE(ptr) free(ptr)
#endif /* TC_FREE */
typedef struct TC__HistoryEntry
{
TC_String orig, edit;
} TC__HistoryEntry;
static size_t tchist__get_tail_idx(TC_History* hist)
{
return (hist->tail + hist->mem - 1) % hist->mem;
}
static size_t tchist__get_head_idx(TC_History* hist)
{
return hist->head;
}
static size_t tchist__get_prev_idx(TC_History* hist, size_t idx)
{
if(idx == hist->head)
return idx;
return (idx + hist->mem - 1) % hist->mem;
}
static size_t tchist__get_next_idx(TC_History* hist, size_t idx)
{
if((idx + 1) % hist->mem == hist->tail)
return idx;
return (idx + 1) % hist->mem;
}
TC_History* tchist_init(TC_History* hist, int maxlen)
{
#ifdef __cplusplus
/* goddamnit for making me do this because you absolutely need -Wextra! (you know who you are) */
static const TC__HistoryEntry eentry = TC__HistoryEntry();
#else
static const TC__HistoryEntry eentry;
#endif
if(!hist) return NULL;
hist->maxlen = maxlen;
hist->mem = maxlen >= 0 ? maxlen + 1 : 1;
hist->head = 0;
hist->tail = 1;
hist->vpos = tchist__get_tail_idx(hist);
hist->hpos = 0;
hist->entries = TC__VOID_CAST(TC__HistoryEntry*,TC_MALLOC(hist->mem * sizeof(*hist->entries)));
size_t i;
for(i = 0; i < hist->mem; i++)
hist->entries[i] = eentry;
return hist;
}
void tchist_deinit(TC_History* hist)
{
if(!hist) return;
size_t i;
for(i = 0; i < hist->mem; i++)
{
tcstr_deinit(&hist->entries[i].orig);
tcstr_deinit(&hist->entries[i].edit);
}
TC_FREE(hist->entries);
}
TC_String* tchist_get_string(TC_History* hist)
{
return &hist->entries[hist->vpos].edit;
}
size_t tchist_get_hpos(TC_History* hist)
{
return hist->hpos;
}
void tchist_cmd_vmove_full(TC_History* hist, int down)
{
if(down > 0)
hist->vpos = tchist__get_tail_idx(hist);
else if(down < 0)
hist->vpos = tchist__get_head_idx(hist);
tchist_cmd_hmove_full(hist, +1);
}
void tchist_cmd_hmove_full(TC_History* hist, int right)
{
if(right > 0)
hist->hpos = hist->entries[hist->vpos].edit.len;
else if(right < 0)
hist->hpos = 0;
}
void tchist_cmd_vmove(TC_History* hist, int down)
{
while(down > 0)
{
hist->vpos = tchist__get_next_idx(hist, hist->vpos);
down--;
}
while(down < 0)
{
hist->vpos = tchist__get_prev_idx(hist, hist->vpos);
down++;
}
tchist_cmd_hmove_full(hist, +1);
}
void tchist_cmd_hmove(TC_History* hist, int right)
{
while(right > 0)
{
hist->hpos = tcstr_utf8_next_off(&hist->entries[hist->vpos].edit, hist->hpos);
right--;
}
while(right < 0)
{
hist->hpos = tcstr_utf8_prev_off(&hist->entries[hist->vpos].edit, hist->hpos);
right++;
}
}
void tchist_str_input(TC_History* hist, const char* str, int len, int insert)
{
if(len < 0) len = strlen(str);
tcstr_splices(&hist->entries[hist->vpos].edit, hist->hpos, insert ? 0 : len, str, len);
hist->hpos += len;
}
void tchist_str_delete(TC_History* hist, int len)
{
TC_String* str = tchist_get_string(hist);
size_t npos;
while(len > 0) /* delete */
{
npos = tcstr_utf8_next_off(str, hist->hpos);
tcstr_splices(str, hist->hpos, npos - hist->hpos, NULL, 0);
len--;
}
while(len < 0) /* backspace */
{
npos = tcstr_utf8_prev_off(str, hist->hpos);
tcstr_splices(str, npos, hist->hpos - npos, NULL, 0);
hist->hpos = npos;
len++;
}
}
void tchist_str_clear(TC_History* hist)
{
tcstr_reinit(&hist->entries[hist->vpos].edit, NULL);
}
TC_String* tchist_exec(TC_History* hist)
{
static char emptystr[] = "";
static TC_String empty = { 0, emptystr };
TC__HistoryEntry* tentry = &hist->entries[tchist__get_tail_idx(hist)];
TC__HistoryEntry* entry = &hist->entries[hist->vpos];
TC_String* str = tchist_get_string(hist);
if(!str->len)
{
tcstr_reinit(&entry->edit, &entry->orig);
tchist_cmd_hmove_full(hist, -1);
return ∅
}
if(entry == tentry)
tcstr_reinit(&entry->orig, &entry->edit);
else
{
tcstr_reinit(&tentry->orig, &entry->edit);
tcstr_reinit(&tentry->edit, &entry->edit);
tcstr_reinit(&entry->edit, &entry->orig);
}
hist->tail = (hist->tail + 1) % hist->mem;
tentry = &hist->entries[tchist__get_tail_idx(hist)];
tcstr_reinits(&tentry->orig, NULL, 0);
tcstr_reinits(&tentry->edit, NULL, 0);
if(hist->tail == hist->head)
hist->head = (hist->head + 1) % hist->mem;
tchist_cmd_hmove_full(hist, -1);
return tchist_get_string(hist);
}
#endif /* TC_HISTORY_IMPLEMENTATION */