-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirty-term.h
261 lines (221 loc) · 5.39 KB
/
dirty-term.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#ifdef _WIN32
#include <conio.h>
#elif defined __linux__
#include <termios.h>
static struct termios old, current;
void initTermios(int echo)
{
tcgetattr(0, &old);
current = old;
current.c_lflag &= ~ICANON;
if (echo)
current.c_lflag |= ECHO;
else
current.c_lflag &= ~ECHO;
tcsetattr(0, TCSANOW, ¤t);
}
void resetTermios(void)
{
tcsetattr(0, TCSANOW, &old);
}
char getch_(int echo)
{
char ch;
initTermios(echo);
ch = getchar();
resetTermios();
return ch;
}
char getch(void)
{
return getch_(0);
}
char getche(void)
{
return getch_(1);
}
#endif
// Defines whether the input string starts with the given prefix and returns its length
int dtprefix(const char *str, const char *pre)
{
int i = 0;
while (*pre)
{
if (*pre++ != *str++)
return 0;
i += 1;
}
return i;
}
// The few keyboard constants that are useful
enum CMD_ARROW_KEY
{
ARROW_UP = 'A',
ARROW_DOWN = 'B',
ARROW_LEFT = 'C',
ARROW_RIGHT = 'D',
KEY_TAB = '\t'
};
// Return of user input
struct dtinput_t
{
char *input; // The input
size_t len; // The length of the input
};
// Releases the memory used by the user input
void release_dtinput(struct dtinput_t *input)
{
assert(input != NULL);
free(input->input);
}
// Allows automatic completion of user entries
void auto_complete(const char prompt[], char **buff, const char **list,
const size_t len)
{
if (!len) return;
assert(buff != NULL);
assert(*buff != NULL);
assert(list != NULL);
const size_t size = len * sizeof(int);
int *indexes = malloc(size);
memset(indexes, -1, size);
int *pres = malloc(size);
memset(pres, -1, size);
const char *tok = strrchr(*buff, ' ');
for (int i = 0; i < len; i++)
{
const int pre = dtprefix(list[i], !tok ? *buff : tok + 1);
if (pre)
{
indexes[i] = i;
pres[i] = pre;
}
}
int nDiff = 0;
int fst = 0;
for (int i = 0; i < len; i++)
{
if (indexes[i] != -1)
{
nDiff++;
fst = i;
}
}
if (nDiff == 1)
{
strcat(*buff, list[fst] + pres[fst]);
printf("%s", list[fst] + pres[fst]);
goto end;
}
if (nDiff == 0)
goto end;
printf("\n");
for (int i = 0; i < len; i++)
if (indexes[i] != -1)
printf("\t%s\t", list[indexes[i]]);
printf("\n%s %s", !prompt ? "" : prompt, *buff);
end:
free(pres);
free(indexes);
}
// Data structure used for the input history
struct dthistory_t
{
char **list; // List of user input
size_t len; // Length of the list
};
// Liberate the memory of the history
void release_dt_history(struct dthistory_t *histroy)
{
assert(histroy != NULL);
for (int i = 0; i < histroy->len; i++)
free(histroy->list[i]);
free(histroy->list);
}
// Set a new history
struct dthistory_t new_dthistory()
{
return (struct dthistory_t){malloc(sizeof(char *)), 0};
}
// Adds the given entry to the history
void add_to_history(struct dthistory_t *history, const char *input)
{
assert(history != NULL);
history->list[history->len++] = strdup(input);
history->list = realloc(history->list, (history->len + 1) * sizeof(char *));
}
// Clears the i characters from the terminal
void erase_term_buff(const int i)
{
for (int j = 0; j < i; j++) printf("\b");
for (int j = 0; j < i; j++) printf(" ");
for (int j = 0; j < i; j++) printf("\b");
}
// Allows to move through the history
void move_history(int *i, char **buff, const int buff_size, const char *h)
{
erase_term_buff(*i);
const int hlen = strlen(h);
printf("%s", h);
memset(*buff, 0, buff_size);
strcpy(*buff, h);
*i = hlen;
}
// Returns the user input and handle the few features
struct dtinput_t get_input(const char prompt[], struct dthistory_t *history, const char **to_complete, const size_t len)
{
const size_t buff_size = 250;
char *buff = malloc(buff_size);
memset(buff, 0, buff_size);
int i = 0;
int hist_index = history->len;
printf("%s ", !prompt ? "" : prompt);
do
{
const char curr = getch();
if (curr == '\n') break;
if (curr == KEY_TAB)
{
auto_complete(prompt, &buff, to_complete, len);
continue;
}
if (curr == (char)127) // Handle the DEL key
{
if (i == 0)
continue;
buff[--i] = 0;
printf("\b \b");
continue;
}
if (curr == '\033')
{
getch();
switch (getch())
{
case ARROW_UP:
{
if (hist_index == 0)
continue;
char *h = history->list[--hist_index];
move_history(&i, &buff, buff_size, h);
continue;
}
case ARROW_DOWN:
{
if (hist_index + 1 >= history->len)
continue;
char *h = history->list[++hist_index];
move_history(&i, &buff, buff_size, h);
continue;
}
}
}
buff[i++] = curr;
putchar(curr);
} while (i < buff_size);
return (struct dtinput_t){buff, i};
}