-
Notifications
You must be signed in to change notification settings - Fork 6
/
buffer.d
416 lines (318 loc) · 6.79 KB
/
buffer.d
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
module cui.buffer;
import core.string;
import core.main;
import core.definitions;
import data.list;
import synch.semaphore;
import io.console;
import cui.widget;
// Section: Console
// Description: This console control is a line buffer that is similar to having a terminal inside a terminal. It is basically a terminal window. Extending this class will allow for other types of terminals and emulations. For instance, the VT100 control is a buffer control with VT100 ANSI emulation.
class CuiBuffer : CuiWidget
{
this( uint x, uint y, uint width, uint height)
{
_drawLock = new Semaphore(1);
_x = x;
_y = y;
_r = _x + width;
_b = _y + height;
_w = width;
_h = height;
_buffer = new List!(BufferLine)();
for (uint i; i<_h; i++)
{
BufferLine bf = new BufferLine;
bf.line = new emptyChar[width];
bf.bgclr = new bgColor[width];
bf.fgclr = new fgColor[width];
_buffer.add(bf);
}
position(0,0);
}
override void onKeyChar(dchar chr)
{
static char lastchr;
if (chr == 10)
{
if (lastchr != 13)
{
chr = 13;
}
}
if (chr == 8)
{
if (_curx != _x)
{
position((_curx-_x)-1, (_cury-_y));
writeChar(' ');
position((_curx-_x)-1, (_cury-_y));
}
else
{
position(_w-1, (_cury-_y)-1);
writeChar(' ');
position(_w-1, (_cury-_y)-1);
}
}
else if (chr == 13)
{
if (_cury + 1 == _b)
{
_linefeed();
position(0, (_cury-_y));
}
else
{
position(0, (_cury-_y)+1);
}
}
else
{
writeChar(chr);
}
lastchr = chr;
}
override void onAdd()
{
}
override void onInit()
{
redraw();
}
override void onMouseWheelY(int amt)
{
// + amt : subtract from _firstVisible
// - amt : add to _firstVisible
amt *= 5;
uint _oldFV = _firstVisible;
if (amt > 0 && amt > _firstVisible)
{
_firstVisible = 0;
}
else
{
_firstVisible -= amt;
}
if (_firstVisible > _buffer.length() - _h)
{
_firstVisible = _buffer.length() - _h;
}
if (_oldFV != _firstVisible)
{
redraw();
}
}
override void onDraw() {
_drawLock.down();
Console.position(0,30);
Console.put("a ", _curx, " ", _cury);
// Blank out the space of the buffer
uint curx = 0;
uint cury = _firstVisible;
fgColor fgclr;
bgColor bgclr;
fgclr = _buffer[cury].fgclr[0];
bgclr = _buffer[cury].bgclr[0];
Console.setColor(fgclr, bgclr);
BufferLine bf;
for (uint y = _y; y < _b; y++, cury++)
{
Console.position(0,30);
Console.put("y ", _curx, " ", _cury);
Console.position(_x, y);
bf = _buffer[cury];
curx = 0;
for (curx = 0; curx < _w; curx++)
{
if (bf.fgclr[curx] != fgclr || bf.bgclr[curx] != bgclr)
{
Console.setColor(bf.fgclr[curx], bf.bgclr[curx]);
fgclr = bf.fgclr[curx];
bgclr = bf.bgclr[curx];
}
Console.put(bf.line[curx]);
}
}
_drawLock.up();
}
void redraw() {
onDraw();
}
// Methods
void setColor(fgColor fgclr, bgColor bgclr)
{
_drawLock.down();
_curfg = fgclr;
_curbg = bgclr;
_drawLock.up();
}
void writeChar(dchar chr)
{
static int a = 0;
Console.position((a * 10),29);
Console.put(_curx, " ", _cury);
if (_cury >= _b)
{
_linefeed();
Console.position((a * 10),29);
Console.put("lf: ", _curx, " ", _cury);
}
a++;
a %= 3;
_drawLock.down();
Console.position(_curx, _cury);
Console.setColor(_curfg, _curbg);
Console.put(chr);
uint idx_y = (_cury-_y)+_firstVisible;
uint idx_x = (_curx-_x);
_buffer[idx_y].line[idx_x] = cast(emptyChar)chr;
_buffer[idx_y].bgclr[idx_x] = _curbg;
_buffer[idx_y].fgclr[idx_x] = _curfg;
_curx++;
_drawLock.up();
if (_curx == _r)
{
Console.position((a * 10),29);
Console.put("lf: ", _curx, " ", _cury);
_linefeed();
Console.position((a * 10),29);
Console.put("af: ", _curx, " ", _cury);
}
}
void writeChar(uint x, uint y, dchar chr)
{
_drawLock.down();
_buffer[y].line[x] = cast(emptyChar)chr;
_buffer[y].bgclr[x] = _curbg;
_buffer[y].fgclr[x] = _curfg;
Console.position(x+_x, y+_y);
Console.setColor(_curfg, _curbg);
Console.put(chr);
_drawLock.up();
}
void position(uint x, uint y)
{
_drawLock.down();
_curx = x + _x;
_cury = y + _y;
if (_curx >= _r)
{
_curx = _r-1;
}
if (_cury >= _b)
{
_cury = _b-1;
}
Console.position(_curx, _cury);
_drawLock.up();
}
void setRelative(int x, int y)
{
uint new_x = (_curx-_x) + x;
uint new_y = (_cury-_y) + y;
position(new_x, new_y);
}
void getPosition(out uint x, out uint y)
{
x = _curx - _x;
y = _cury - _y;
}
protected:
void _linefeed()
{
Console.position((4 * 10),29);
Console.put("af: ", _curx, " ", _cury);
_drawLock.down();
bool needRedraw = false;
// met right edge; goto next line
// linefeed
_cury++;
while (_cury >= _b)
{
Console.position((4 * 10),29);
Console.put("bf: ", _curx, " ", _cury);
// we have reached the bottom
// we have to move the first line
// to the buffer
BufferLine bl;
for(uint i=0; i<_linesToScroll;i++)
{
bl = null;
while (bl is null)
{
bl = new BufferLine();
}
Console.position((4 * 10),29);
Console.put("ef: ", _curx, " ", _cury);
bl.fgclr = new fgColor[_w];
Console.position((4 * 10),29);
Console.put("ff: ", _curx, " ", _cury);
bl.bgclr = new bgColor[_w];
Console.position((4 * 10),29);
Console.put("df: ", _curx, " ", _cury);
bl.line = new emptyChar[_w];
Console.position((4 * 10),29);
Console.put("gf: ", _curx, " ", _cury);
_buffer.add(bl);
Console.position((4 * 10),29);
Console.put("hf: ", _curx, " ", _cury);
}
Console.position((4 * 10),29);
Console.put("cf: ", _curx, " ", _cury);
_firstVisible += _linesToScroll;
// redraw the entire view
needRedraw = true;
// position ourselves at the new
// line, which is just the previous
// line again
_cury -= _linesToScroll;
}
// carriage return
_curx=_x;
_drawLock.up();
if (needRedraw) { redraw(); }
}
void _screenfeed()
{
_drawLock.down();
// we have reached the bottom
// we have to move the first line
// to the buffer
for(uint i=0; i<_h;i++)
{
BufferLine bl = new BufferLine();
bl.line = new emptyChar[_w];
bl.fgclr = new fgColor[_w];
bl.bgclr = new bgColor[_w];
_buffer.add(bl);
}
_firstVisible += _h;
// go home
_cury = _y;
_curx = _x;
_drawLock.up();
redraw();
}
uint _x = 0;
uint _y = 0;
uint _r = 0;
uint _b = 0;
uint _w = 0;
uint _h = 0;
typedef dchar emptyChar = ' ';
// keep a line buffer of previous lines, for scrollback, etc
class BufferLine {
emptyChar[] line = null;
fgColor[] fgclr = null;
bgColor[] bgclr = null;
}
List!(BufferLine) _buffer;
uint _firstVisible; // first line visible
fgColor _curfg = fgColor.White;
bgColor _curbg = bgColor.Black;
uint _curx;
uint _cury;
uint _linesToScroll = 7;
Semaphore _drawLock;
}