-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.cc
287 lines (222 loc) · 5.21 KB
/
index.cc
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <napi.h>
#ifdef _WIN32
#include <windows.h>
int cursor_position(int *const rowptr, int *const colptr)
{
HANDLE console_handle;
CONSOLE_SCREEN_BUFFER_INFO console_info;
console_handle = CreateFileW(L"CONOUT$",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (console_handle == INVALID_HANDLE_VALUE)
return GetLastError();
if (!GetConsoleScreenBufferInfo(console_handle, &console_info))
return GetLastError();
/* Success! */
if (rowptr)
*rowptr = console_info.dwCursorPosition.Y + 1;
if (colptr)
*colptr = console_info.dwCursorPosition.X + 1;
/* Done. */
return 0;
}
#else
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#define RD_EOF -1
#define RD_EIO -2
static inline int rd(const int fd)
{
unsigned char buffer[4];
ssize_t n;
while (1)
{
n = read(fd, buffer, 1);
if (n > (ssize_t)0)
return buffer[0];
else if (n == (ssize_t)0)
return RD_EOF;
else if (n != (ssize_t)-1)
return RD_EIO;
else if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK)
return RD_EIO;
}
}
static inline int wr(const int fd, const char *const data, const size_t bytes)
{
const char *head = data;
const char *const tail = data + bytes;
ssize_t n;
while (head < tail)
{
n = write(fd, head, (size_t)(tail - head));
if (n > (ssize_t)0)
head += n;
else if (n != (ssize_t)-1)
return EIO;
else if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK)
return errno;
}
return 0;
}
/* Return a new file descriptor to the current TTY. */
int current_tty(void)
{
const char *dev;
int fd;
dev = ttyname(STDIN_FILENO);
if (!dev)
dev = ttyname(STDOUT_FILENO);
if (!dev)
dev = ttyname(STDERR_FILENO);
if (!dev)
{
errno = ENOTTY;
return -1;
}
do
{
fd = open(dev, O_RDWR | O_NOCTTY);
} while (fd == -1 && errno == EINTR);
if (fd == -1)
return -1;
return fd;
}
/* As the tty for current cursor position.
* This function returns 0 if success, errno code otherwise.
* Actual errno will be unchanged.
*/
int cursor_position(int *const rowptr, int *const colptr)
{
struct termios saved, temporary;
int tty, retval, result, rows, cols, saved_errno;
tty = current_tty();
/* Bad tty? */
if (tty == -1)
return ENOTTY;
saved_errno = errno;
/* Save current terminal settings. */
do
{
result = tcgetattr(tty, &saved);
} while (result == -1 && errno == EINTR);
if (result == -1)
{
retval = errno;
errno = saved_errno;
return retval;
}
/* Get current terminal settings for basis, too. */
do
{
result = tcgetattr(tty, &temporary);
} while (result == -1 && errno == EINTR);
if (result == -1)
{
retval = errno;
errno = saved_errno;
return retval;
}
/* Disable ICANON, ECHO, and CREAD. */
temporary.c_lflag &= ~ICANON;
temporary.c_lflag &= ~ECHO;
temporary.c_cflag &= ~CREAD;
/* This loop is only executed once. When broken out,
* the terminal settings will be restored, and the function
* will return retval to caller. It's better than goto.
*/
do
{
/* Set modified settings. */
do
{
result = tcsetattr(tty, TCSANOW, &temporary);
} while (result == -1 && errno == EINTR);
if (result == -1)
{
retval = errno;
break;
}
/* Request cursor coordinates from the terminal. */
retval = wr(tty, "\033[6n", 4);
if (retval)
break;
/* Assume coordinate reponse parsing fails. */
retval = EIO;
/* Expect an ESC. */
result = rd(tty);
if (result != 27)
break;
/* Expect [ after the ESC. */
result = rd(tty);
if (result != '[')
break;
/* Parse rows. */
rows = 0;
result = rd(tty);
while (result >= '0' && result <= '9')
{
rows = 10 * rows + result - '0';
result = rd(tty);
}
if (result != ';')
break;
/* Parse cols. */
cols = 0;
result = rd(tty);
while (result >= '0' && result <= '9')
{
cols = 10 * cols + result - '0';
result = rd(tty);
}
if (result != 'R')
break;
/* Success! */
if (rowptr)
*rowptr = rows;
if (colptr)
*colptr = cols;
retval = 0;
} while (0);
/* Restore saved terminal settings. */
do
{
result = tcsetattr(tty, TCSANOW, &saved);
} while (result == -1 && errno == EINTR);
if (result == -1 && !retval)
retval = errno;
/* Done. */
close(tty);
return retval;
}
#endif
Napi::Object Method(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
Napi::Object pos = Napi::Object::New(env);
int row, col;
row = 0;
col = 0;
if (cursor_position(&row, &col))
{
return pos;
}
if (row < 1 || col < 1)
{
return pos;
}
pos.Set("row", row);
pos.Set("col", col);
return pos;
}
Napi::Object Init(Napi::Env env, Napi::Object exports)
{
exports.Set(Napi::String::New(env, "sync"), Napi::Function::New(env, Method));
return exports;
}
NODE_API_MODULE(pos, Init)