1
2
9
10
11
12
13
14
17
18
21
22
31
32
37
38
45
46
47
48
49
50
51
52
53
56
59
62
63
65
66
67
68
69
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
136
144
147
148
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
195
196
197
198
199
200
201
202
203
204
205
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
227
230
233
236
239
242
245
248
251
254
257
258
259
260
261
262
265
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
297
298
303
304
309
310
311
312
313
314
315
318
321
322
323
324
325
326
328
330
332
334
336
338
340
342
344
346
347
348
349
350
351
354
355
356
357
358
359
360
361
362
363
366
367
368
372
373
374
375
/* ... */
static DWORD orig_consolemode = 0;
static int flushOutput(struct current *current);
static void outputNewline(struct current *current);
static void refreshStart(struct current *current)
{
}{ ... }
static void refreshEnd(struct current *current)
{
}{ ... }
static void refreshStartChars(struct current *current)
{
assert(current->output == NULL);
current->output = sb_alloc();
#ifdef USE_UTF8
current->ubuflen = 0;
#endif
}{ ... }
static void refreshNewline(struct current *current)
{
DRL("<nl>");
outputNewline(current);
}{ ... }
static void refreshEndChars(struct current *current)
{
assert(current->output);
flushOutput(current);
sb_free(current->output);
current->output = NULL;
}{ ... }
static int enableRawMode(struct current *current) {
DWORD n;
INPUT_RECORD irec;
current->outh = GetStdHandle(STD_OUTPUT_HANDLE);
current->inh = GetStdHandle(STD_INPUT_HANDLE);
if (!PeekConsoleInput(current->inh, &irec, 1, &n)) {
return -1;
}if (!PeekConsoleInput(current->inh, &irec, 1, &n)) { ... }
if (getWindowSize(current) != 0) {
return -1;
}if (getWindowSize(current) != 0) { ... }
if (GetConsoleMode(current->inh, &orig_consolemode)) {
SetConsoleMode(current->inh, ENABLE_PROCESSED_INPUT);
}if (GetConsoleMode(current->inh, &orig_consolemode)) { ... }
#ifdef USE_UTF8
SetConsoleCP(65001);/* ... */
#endif
return 0;
}{ ... }
static void disableRawMode(struct current *current)
{
SetConsoleMode(current->inh, orig_consolemode);
}{ ... }
void linenoiseClearScreen(void)
{
struct current current;
current.outh = GetStdHandle(STD_OUTPUT_HANDLE);
if (getWindowSize(¤t) == 0) {
COORD topleft = { 0, 0 };
DWORD n;
FillConsoleOutputCharacter(current.outh, ' ',
current.cols * current.rows, topleft, &n);
FillConsoleOutputAttribute(current.outh,
FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN,
current.cols * current.rows, topleft, &n);
SetConsoleCursorPosition(current.outh, topleft);
}if (getWindowSize(¤t) == 0) { ... }
}{ ... }
static void cursorToLeft(struct current *current)
{
COORD pos;
DWORD n;
pos.X = 0;
pos.Y = (SHORT)current->y;
FillConsoleOutputAttribute(current->outh,
FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN, current->cols, pos, &n);
current->x = 0;
}{ ... }
#ifdef USE_UTF8
static void flush_ubuf(struct current *current)
{
COORD pos;
DWORD nwritten;
pos.Y = (SHORT)current->y;
pos.X = (SHORT)current->x;
SetConsoleCursorPosition(current->outh, pos);
WriteConsoleW(current->outh, current->ubuf, current->ubuflen, &nwritten, 0);
current->x += current->ubufcols;
current->ubuflen = 0;
current->ubufcols = 0;
}flush_ubuf (struct current *current) { ... }
static void add_ubuf(struct current *current, int ch)
{
static const int halfShift = 10;
static const DWORD halfBase = 0x0010000UL;
static const DWORD halfMask = 0x3FFUL;
#define UNI_SUR_HIGH_START 0xD800
#define UNI_SUR_HIGH_END 0xDBFF
#define UNI_SUR_LOW_START 0xDC00
#define UNI_SUR_LOW_END 0xDFFF
#define UNI_MAX_BMP 0x0000FFFF
5 defines
if (ch > UNI_MAX_BMP) {
/* ... */
ch -= halfBase;
current->ubuf[current->ubuflen++] = (WORD)((ch >> halfShift) + UNI_SUR_HIGH_START);
current->ubuf[current->ubuflen++] = (WORD)((ch & halfMask) + UNI_SUR_LOW_START);
}if (ch > UNI_MAX_BMP) { ... }
else {
current->ubuf[current->ubuflen++] = ch;
}else { ... }
current->ubufcols += utf8_width(ch);
if (current->ubuflen >= UBUF_MAX_CHARS) {
flush_ubuf(current);
}if (current->ubuflen >= UBUF_MAX_CHARS) { ... }
}add_ubuf (struct current *current, int ch) { ... }
/* ... */#endif
static int flushOutput(struct current *current)
{
const char *pt = sb_str(current->output);
int len = sb_len(current->output);
#ifdef USE_UTF8
/* ... */
while (len) {
int ch;
int n = utf8_tounicode(pt, &ch);
pt += n;
len -= n;
add_ubuf(current, ch);
}while (len) { ... }
flush_ubuf(current);/* ... */
#else
DWORD nwritten;
COORD pos;
pos.Y = (SHORT)current->y;
pos.X = (SHORT)current->x;
SetConsoleCursorPosition(current->outh, pos);
WriteConsoleA(current->outh, pt, len, &nwritten, 0);
current->x += len;/* ... */
#endif
sb_clear(current->output);
return 0;
}{ ... }
static int outputChars(struct current *current, const char *buf, int len)
{
if (len < 0) {
len = strlen(buf);
}if (len < 0) { ... }
assert(current->output);
sb_append_len(current->output, buf, len);
return 0;
}{ ... }
static void outputNewline(struct current *current)
{
if (current->y + 1 == current->rows) {
outputChars(current, "\n", 1);
}if (current->y + 1 == current->rows) { ... }
flushOutput(current);
current->x = 0;
current->y++;
}{ ... }
static void setOutputHighlight(struct current *current, const int *props, int nprops)
{
int colour = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN;
int bold = 0;
int reverse = 0;
int i;
for (i = 0; i < nprops; i++) {
switch (props[i]) {
case 0:
colour = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN;
bold = 0;
reverse = 0;
break;case 0:
case 1:
bold = FOREGROUND_INTENSITY;
break;case 1:
case 7:
reverse = 1;
break;case 7:
case 30:
colour = 0;
break;case 30:
case 31:
colour = FOREGROUND_RED;
break;case 31:
case 32:
colour = FOREGROUND_GREEN;
break;case 32:
case 33:
colour = FOREGROUND_RED | FOREGROUND_GREEN;
break;case 33:
case 34:
colour = FOREGROUND_BLUE;
break;case 34:
case 35:
colour = FOREGROUND_RED | FOREGROUND_BLUE;
break;case 35:
case 36:
colour = FOREGROUND_BLUE | FOREGROUND_GREEN;
break;case 36:
case 37:
colour = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN;
break;case 37:
}switch (props[i]) { ... }
}for (i = 0; i < nprops; i++) { ... }
flushOutput(current);
if (reverse) {
SetConsoleTextAttribute(current->outh, BACKGROUND_INTENSITY);
}if (reverse) { ... }
else {
SetConsoleTextAttribute(current->outh, colour | bold);
}else { ... }
}{ ... }
static void eraseEol(struct current *current)
{
COORD pos;
DWORD n;
pos.X = (SHORT) current->x;
pos.Y = (SHORT) current->y;
FillConsoleOutputCharacter(current->outh, ' ', current->cols - current->x, pos, &n);
}{ ... }
static void setCursorXY(struct current *current)
{
COORD pos;
pos.X = (SHORT) current->x;
pos.Y = (SHORT) current->y;
SetConsoleCursorPosition(current->outh, pos);
}{ ... }
static void setCursorPos(struct current *current, int x)
{
current->x = x;
setCursorXY(current);
}{ ... }
static void cursorUp(struct current *current, int n)
{
current->y -= n;
setCursorXY(current);
}{ ... }
static void cursorDown(struct current *current, int n)
{
current->y += n;
setCursorXY(current);
}{ ... }
static int fd_read(struct current *current)
{
while (1) {
INPUT_RECORD irec;
DWORD n;
if (WaitForSingleObject(current->inh, INFINITE) != WAIT_OBJECT_0) {
break;
}if (WaitForSingleObject(current->inh, INFINITE) != WAIT_OBJECT_0) { ... }
if (!ReadConsoleInputW(current->inh, &irec, 1, &n)) {
break;
}if (!ReadConsoleInputW(current->inh, &irec, 1, &n)) { ... }
if (irec.EventType == KEY_EVENT) {
KEY_EVENT_RECORD *k = &irec.Event.KeyEvent;
if (k->bKeyDown || k->wVirtualKeyCode == VK_MENU) {
if (k->dwControlKeyState & ENHANCED_KEY) {
switch (k->wVirtualKeyCode) {
case VK_LEFT:
return SPECIAL_LEFT;case VK_LEFT:
case VK_RIGHT:
return SPECIAL_RIGHT;case VK_RIGHT:
case VK_UP:
return SPECIAL_UP;case VK_UP:
case VK_DOWN:
return SPECIAL_DOWN;case VK_DOWN:
case VK_INSERT:
return SPECIAL_INSERT;case VK_INSERT:
case VK_DELETE:
return SPECIAL_DELETE;case VK_DELETE:
case VK_HOME:
return SPECIAL_HOME;case VK_HOME:
case VK_END:
return SPECIAL_END;case VK_END:
case VK_PRIOR:
return SPECIAL_PAGE_UP;case VK_PRIOR:
case VK_NEXT:
return SPECIAL_PAGE_DOWN;case VK_NEXT:
}switch (k->wVirtualKeyCode) { ... }
}if (k->dwControlKeyState & ENHANCED_KEY) { ... }
else if (k->wVirtualKeyCode == VK_CONTROL)
continue;
else {
return k->uChar.UnicodeChar;
}else { ... }
}if (k->bKeyDown || k->wVirtualKeyCode == VK_MENU) { ... }
}if (irec.EventType == KEY_EVENT) { ... }
}while (1) { ... }
return -1;
}{ ... }
static int getWindowSize(struct current *current)
{
CONSOLE_SCREEN_BUFFER_INFO info;
if (!GetConsoleScreenBufferInfo(current->outh, &info)) {
return -1;
}if (!GetConsoleScreenBufferInfo(current->outh, &info)) { ... }
current->cols = info.dwSize.X;
current->rows = info.dwSize.Y;
if (current->cols <= 0 || current->rows <= 0) {
current->cols = 80;
return -1;
}if (current->cols <= 0 || current->rows <= 0) { ... }
current->y = info.dwCursorPosition.Y;
current->x = info.dwCursorPosition.X;
return 0;
}{ ... }