-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_term.c
94 lines (78 loc) · 2.1 KB
/
io_term.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include <string.h>
#include <vterm.h>
#include <io/term.h>
#include <sh/api.h>
#include <shell.h>
#include <util/misc.h>
#define ASCII_VALID_START 0x20
#define ASCII_VALID_END 0x7E
void init_vt_lib(void) { vt_init(); }
SH_API_IO_IMPL char term_getch(void) { return mcInputchar(); }
SH_API_IO_IMPL void term_putch(const char c) { mcPrintchar(c); }
SH_API_IO_IMPL void term_set_cursor_xy(int x, int y) {
char x_str[10];
char y_str[10];
itoa(x, x_str, 10);
itoa(y, y_str, 10);
vt_move_cursor_xy(x_str, y_str);
}
SH_API_IO_IMPL void term_newline(void) {
mcPrintchar('\r');
mcPrintchar('\n');
vt_move_cursor_down("1");
}
SH_API_IO_IMPL void term_puts(const char *str) {
for (size_t i = 0; i < strlen(str); i++) {
mcPrintchar(str[i]);
}
}
SH_API_IO_IMPL char *term_gets(char *buff, size_t len) {
size_t buff_index = 0;
char in = 0;
/* Clear the buffer before using it */
memset(buff, 0, len);
/* Keep getting input until we get the enter key */
while (buff_index < len && in != 0x0d) {
in = mcInputchar();
/* Handle characters than can be displayed */
if (in >= ASCII_VALID_START && in <= ASCII_VALID_END) {
/* Add them to the input buffer */
buff[buff_index] = in;
buff_index++;
/* Print them to the screen so the user can see what they are typing */
mcPrintchar(in);
}
/* Handle Backspace (Win/Lin) or Del (Mac) */
if (in == 0x7F || in == 0x08) {
vt_backspace();
/* Remove the deleted character from the input buffer */
if (buff_index > 0) {
buff_index--;
}
buff[buff_index] = 0;
}
/* Handle escape sequences */
if (in == 0x1B && mcInputchar() == 0x5B) {
/* Handle arrow keys */
switch (mcInputchar()) {
case 0x41:
vt_move_cursor_up("1");
break;
case 0x42:
vt_move_cursor_down("1");
break;
case 0x43:
vt_move_cursor_left("1");
break;
case 0x44:
vt_move_cursor_right("1");
break;
}
}
}
term_newline();
return buff;
}