-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.c
More file actions
180 lines (165 loc) · 4.68 KB
/
Copy pathinput.c
File metadata and controls
180 lines (165 loc) · 4.68 KB
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
#include <string.h>
#include "common.h"
#include "render.h"
#define CTRL_KEY(c) ((c) & 0x1f)
void save_file()
{
FILE* file = fopen(E.filename, "w");
fprintf(file, "%d\n%d\n", E.num_rows, E.num_cols);
for (int row = 0; row < E.num_rows; row++)
{
for (int col = 0; col < E.num_cols; col++)
fprintf(file, "%c", E.state[row][col]);
fprintf(file, "\n");
}
fclose(file);
}
int read_key_press()
{
char c;
read(STDIN_FILENO, &c, 1);
if (c == '\x1b')
{
char seq[3]; // if ESC, read in sequence
if (read(STDIN_FILENO, &seq[0], 1) != 1)
return '\x1b';
if (read(STDIN_FILENO, &seq[1], 1) != 1)
return '\x1b';
if (seq[0] == '[')
{
if (seq[1] >= '0' && seq[1] <= '9')
{
if (read(STDIN_FILENO, &seq[2], 1) != 1)
return '\x1b';
if (seq[2] == '~')
{
switch (seq[1])
{
case '1': return HOME_KEY;
case '3': return DEL_KEY;
case '4': return END_KEY;
case '5': return PAGE_UP;
case '6': return PAGE_DOWN;
case '7': return HOME_KEY;
case '8': return END_KEY;
}
}
}
else
{
switch (seq[1])
{
case 'A': return ARROW_UP;
case 'B': return ARROW_DOWN;
case 'C': return ARROW_RIGHT;
case 'D': return ARROW_LEFT;
case 'H': return HOME_KEY;
case 'F': return END_KEY;
}
}
}
else if (seq[0] == 'O')
{
switch (seq[1])
{
case 'H': return HOME_KEY;
case 'F': return END_KEY;
}
}
return '\x1b';
}
else return c;
}
void create_file()
{
#define MAX_FILENAME_LEN 64
E.filename = calloc(MAX_FILENAME_LEN, sizeof(char));
E.editing_filename = 1;
draw_screen(); // to render leading '>'
char c = read_key_press();
for (int i = 0; (c != '\r') && (i < MAX_FILENAME_LEN - 1); c = read_key_press())
{
if (c == BACKSPACE) E.filename[--i] = '\0';
else E.filename[i++] = c;
draw_screen();
}
E.editing_filename = 0;
save_file();
}
// EXTERNAL:
void get_input()
{
#define QUIT_TIMES 2
static int quit_times = QUIT_TIMES;
int c = read_key_press();
switch(c)
{
// handle arrow keys
case ARROW_UP:
if (E.cursor_row > 0) E.cursor_row--;
break;
case ARROW_DOWN:
if (E.cursor_row < E.num_rows - 1) E.cursor_row++;
break;
case ARROW_LEFT:
if (E.cursor_col > 0) E.cursor_col--;
break;
case ARROW_RIGHT:
if (E.cursor_col < E.num_cols - 1) E.cursor_col++;
break;
// handle colors
case 'l':
case 'r':
case 'g':
case 'y':
case 'b':
case 'm':
case 'c':
case 'w':
case 'L':
case 'R':
case 'G':
case 'Y':
case 'B':
case 'M':
case 'C':
case 'W':
if (E.state[E.cursor_row][E.cursor_col] != c)
{
E.has_been_edited = 1;
E.state[E.cursor_row][E.cursor_col] = c;
}
break;
case BACKSPACE: // erase
if (E.state[E.cursor_row][E.cursor_col] != '0')
{
E.has_been_edited = 1;
E.state[E.cursor_row][E.cursor_col] = '0';
}
break;
// handle meta stuff
case CTRL_KEY('s'): // save
E.has_been_edited = 0;
if (E.filename) save_file();
else create_file();
break;
case CTRL_KEY('q'): // quit
if (E.has_been_edited && quit_times) // give warning in case of unsaved changes
{
snprintf(
E.status_message,
80,
"WARNING: Unsaved changes. Press CTRL+Q %d more times to quit.",
quit_times--
);
E.status_message_bg = YELLOW;
E.status_message_fg = "30m"; // black
return; // this return avoids the reset of quit_times at end of get_input
}
exit(0);
break;
default: break;
}
quit_times = QUIT_TIMES;
memset(E.status_message, 0, 80);
}