9 static void set_cursor(int x);
12 static int nlines = 16;
13 static int ncolumns = 80;
15 #define INPUTQ_SIZE 64
16 static int inpq_front, inpq_back;
17 static int keybuf[INPUTQ_SIZE];
19 static const char *prompt = "> ";
20 static char input[512]; /* current input line */
21 static int input_size;
23 static int input_win; /* first char to show in the input "window" */
27 static int max_hist_lines = 64;
29 static int cur_hist_item = -1;
33 static int max_output_lines = 128;
34 static int output_next;
37 static void (*cmd_handler)(const char*, void*);
38 static void *cmd_handler_cls;
41 static struct dtx_font *font;
44 void con_set_font(struct dtx_font *fnt, int sz)
50 int con_set_history_size(int hsz)
54 if(hsz < 0) return -1;
57 memmove(hist, hist + hist_next - hsz, hsz);
61 if(!(tmp = realloc(hist, hsz * sizeof *hist))) {
62 fprintf(stderr, "failed to change console history to %d: %s\n", hsz, strerror(errno));
70 int con_set_output_buffer_size(int hsz)
74 if(hsz < 0) return -1;
76 if(hsz < output_next) {
77 memmove(output, output + output_next - hsz, hsz);
81 if(!(tmp = realloc(output, hsz * sizeof *output))) {
82 fprintf(stderr, "failed to change console outputory to %d: %s\n", hsz, strerror(errno));
86 max_output_lines = hsz;
90 void con_set_command_func(void (*func)(const char*, void*), void *cls)
93 cmd_handler_cls = cls;
96 void con_set_echo(int e)
101 int con_get_echo(void)
116 void con_set_visible(int v)
121 int con_is_visible(void)
126 void con_set_size(int lines, int columns)
132 int con_get_size_lines(void)
137 int con_get_size_columns(void)
147 while(inpq_front != inpq_back) {
148 int c = keybuf[inpq_front];
149 inpq_front = (inpq_front + 1) % INPUTQ_SIZE;
158 if(input_size > 0 && cmd_handler) {
159 cmd_handler(input, cmd_handler_cls);
162 /* append the input string into the history buffer */
163 if(!(str = malloc(strlen(input) + 1))) {
164 fprintf(stderr, "failed to add input to history buffer: %s\n", strerror(errno));
167 if(hist[hist_next]) free(hist[hist_next]);
168 hist[hist_next] = str;
169 hist_next = (hist_next + 1) & (max_hist_lines - 1);
177 /* TODO completion */
182 if(cursor == input_size) {
184 set_cursor(cursor - 1);
187 } else if(cursor > 0) {
188 memmove(input + cursor, input + cursor + 1, input_size - cursor - 1);
190 set_cursor(cursor - 1);
202 set_cursor(cursor - 1);
208 if(cursor < input_size) {
209 set_cursor(cursor + 1);
220 set_cursor(input_size);
226 /* TODO scroll output buffer */
230 if((c < 256 && isprint(c))) {
231 if(cursor == input_size) {
232 input[input_size++] = c;
234 memmove(input + cursor + 1, input + cursor, input_size - cursor);
237 set_cursor(cursor + 1);
245 static void set_cursor(int x)
249 if(x < 0 || x > input_size) {
254 max_chars = ncolumns - strlen(prompt) - 1;
256 if(cursor < input_win) {
258 } else if(cursor > input_win + max_chars) {
259 input_win = cursor - max_chars;
260 if(input_win < 0) input_win = 0;