-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15-term.lua
More file actions
1608 lines (1537 loc) · 69.4 KB
/
Copy path15-term.lua
File metadata and controls
1608 lines (1537 loc) · 69.4 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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--- terminal
-- @section terminal
--- Returns a new TTY object.
---@param term table The CraftOS terminal object to render on
---@param width number The width of the TTY
---@param height number The height of the TTY
---@return TTY tty The new TTY object
function terminal.makeTTY(term, width, height)
---@class TTY
local retval = {
isTTY = true,
flags = {
cbreak = false,
delay = true,
echo = true,
keypad = false,
nlcr = true,
raw = false,
},
cursor = {x = 1, y = 1},
cursorBlink = true,
colors = {fg = '0', bg = 'f', bold = false},
size = {width = width, height = height},
dirtyLines = {},
palette = {},
dirtyPalette = {},
buffer = "",
preBuffer = "",
isLocked = false,
isGraphics = false,
textBuffer = {},
graphicsBuffer = {},
frontmostProcess = nil,
processList = {},
eof = false,
term = term,
}
for y = 1, height do
retval[y] = {(' '):rep(width), ('0'):rep(width), ('f'):rep(width)}
retval.dirtyLines[y] = true
end
for i = 0, 15 do
retval.palette[i] = {_G.term.nativePaletteColor(2^i)}
retval.dirtyPalette[i] = true
end
return retval
end
do
local term_width, term_height = term.getSize()
--- Stores all virtual TTYs for the main screen.
TTY = {
terminal.makeTTY(term, term_width, term_height),
terminal.makeTTY(term, term_width, term_height),
terminal.makeTTY(term, term_width, term_height),
terminal.makeTTY(term, term_width, term_height),
terminal.makeTTY(term, term_width, term_height),
terminal.makeTTY(term, term_width, term_height),
terminal.makeTTY(term, term_width, term_height),
terminal.makeTTY(term, term_width, term_height)
}
end
--- Stores the TTY that is currently shown on screen.
currentTTY = TTY[1]
--- Stores all TTYs that have been created in user mode.
terminal.userTTYs = {}
do
local n = args.console:match "^tty(%d+)$"
if n then KERNEL.stdout, KERNEL.stderr, KERNEL.stdin = TTY[tonumber(n)], TTY[tonumber(n)], TTY[tonumber(n)] end
end
--- Stores what modifier keys are currently being held.
keysHeld = {ctrl = false, alt = false, shift = false}
eventHooks.term_resize = eventHooks.term_resize or {}
eventHooks.char = eventHooks.char or {}
eventHooks.paste = eventHooks.paste or {}
eventHooks.key = eventHooks.key or {}
eventHooks.key_up = eventHooks.key_up or {}
eventHooks.term_resize[#eventHooks.term_resize+1] = function()
local w, h = term.getSize()
for i = 1, 8 do terminal.resize(TTY[i], w, h) end
end
eventHooks.char[#eventHooks.char+1] = function(ev)
if not currentTTY.isLocked then
if currentTTY.flags.cbreak then currentTTY.buffer = currentTTY.buffer .. ev[2]
else currentTTY.preBuffer = currentTTY.preBuffer .. ev[2] end
if currentTTY.flags.echo then terminal.write(currentTTY, ev[2]) terminal.redraw(currentTTY) end
end
end
eventHooks.paste[#eventHooks.paste+1] = function(ev)
if not currentTTY.isLocked then
if currentTTY.flags.cbreak then currentTTY.buffer = currentTTY.buffer .. ev[2]
else currentTTY.preBuffer = currentTTY.preBuffer .. ev[2] end
if currentTTY.flags.echo then terminal.write(currentTTY, ev[2]) terminal.redraw(currentTTY) end
end
end
eventHooks.key[#eventHooks.key+1] = function(ev)
if not currentTTY.isLocked then
if ev[2] == keys.enter then
if currentTTY.flags.cbreak then
currentTTY.buffer = currentTTY.buffer .. "\n"
else
currentTTY.buffer = currentTTY.buffer .. currentTTY.preBuffer .. "\n"
currentTTY.preBuffer = ""
end
if currentTTY.flags.echo then terminal.write(currentTTY, "\n") terminal.redraw(currentTTY) end
elseif ev[2] == keys.backspace then
if currentTTY.flags.cbreak then
-- TODO: uh, what is this supposed to be?
elseif #currentTTY.preBuffer > 0 then
currentTTY.preBuffer = currentTTY.preBuffer:sub(1, -2)
if currentTTY.flags.echo then terminal.write(currentTTY, "\b \b") terminal.redraw(currentTTY) end
end
end
end
if ev[2] == keys.leftCtrl or ev[2] == keys.rightCtrl then keysHeld.ctrl = true
elseif ev[2] == keys.leftAlt or ev[2] == keys.rightAlt then keysHeld.alt = true
elseif ev[2] == keys.leftShift or ev[2] == keys.rightShift then keysHeld.shift = true end
if not currentTTY.flags.raw and currentTTY.frontmostProcess and keysHeld.ctrl and not keysHeld.alt and not keysHeld.shift then
if ev[2] == keys.c then killProcess(currentTTY.frontmostProcess.id, 2) terminal.write(currentTTY, "^C")
elseif ev[2] == keys.backslash then killProcess(currentTTY.frontmostProcess.id, 3) terminal.write(currentTTY, "^\\")
elseif ev[2] == keys.z then killProcess(currentTTY.frontmostProcess.id, 19) terminal.write(currentTTY, "^Z")
elseif ev[2] == keys.d then currentTTY.eof = true terminal.write(currentTTY, "^D")
elseif ev[2] == keys.l and currentTTY.cursor.y > 1 then
local y = currentTTY.cursor.y - 1 -- minifier error
terminal.write(currentTTY, "\x1b[" .. y .. "T\x1b[1;" .. currentTTY.cursor.x .. "H")
-- TODO: fill in other cool keys
end
end
if keysHeld.ctrl and not keysHeld.alt and keysHeld.shift then
local changed = true
if ev[2] == keys.f1 then currentTTY = TTY[1]
elseif ev[2] == keys.f2 then currentTTY = TTY[2]
elseif ev[2] == keys.f3 then currentTTY = TTY[3]
elseif ev[2] == keys.f4 then currentTTY = TTY[4]
elseif ev[2] == keys.f5 then currentTTY = TTY[5]
elseif ev[2] == keys.f6 then currentTTY = TTY[6]
elseif ev[2] == keys.f7 then currentTTY = TTY[7]
elseif ev[2] == keys.f8 then currentTTY = TTY[8]
elseif ev[2] == keys.left then for i = 1, 8 do if currentTTY == TTY[i] then currentTTY = TTY[(i+7)%8] break end end
elseif ev[2] == keys.right then for i = 1, 8 do if currentTTY == TTY[i] then currentTTY = TTY[(i+1)%8] break end end
else changed = false end
if changed then terminal.redraw(currentTTY, true) end
end
end
eventHooks.key_up[#eventHooks.key_up+1] = function(ev)
if ev[2] == keys.leftCtrl or ev[2] == keys.rightCtrl then keysHeld.ctrl = false
elseif ev[2] == keys.leftAlt or ev[2] == keys.rightAlt then keysHeld.alt = false
elseif ev[2] == keys.leftShift or ev[2] == keys.rightShift then keysHeld.shift = false end
end
--- Redraws the specified TTY if on-screen.
-- @tparam TTY tty The TTY to redraw
-- @tparam boolean full Whether to draw the full screen, or just the changed regions
function terminal.redraw(tty, full)
if _HEADLESS and tty == TTY[1] and not tty.isLocked then return end -- in headless mode, terminal.write draws directly
if tty.process then tty.process.eventQueue[#tty.process.eventQueue+1] = {"tty_redraw", {id = tty.id}} return
elseif currentTTY ~= tty and not tty.isMonitor then return end
local term = tty.term
local buffer = tty
if tty.isLocked then
if tty.isGraphics then
term.setGraphicsMode(2)
if term.setFrozen then term.setFrozen(true) end
if full then
term.clear()
term.drawPixels(0, 0, tty.graphicsBuffer)
for i = 0, 255 do term.setPaletteColor(i, tty.graphicsBuffer.palette[i][1], tty.graphicsBuffer.palette[i][2], tty.graphicsBuffer.palette[i][3]) end
else
if tty.graphicsBuffer.frozen then
if term.setFrozen then term.setFrozen(false) end
return
end
for _, v in ipairs(tty.graphicsBuffer.dirtyRects) do
if v.color then term.setPixel(v.x, v.y, v.color, v.width, v.height)
else term.drawPixels(v.x, v.y, v) end
end
for i in pairs(tty.graphicsBuffer.dirtyPalette) do term.setPaletteColor(i, tty.graphicsBuffer.palette[i][1], tty.graphicsBuffer.palette[i][2],tty.graphicsBuffer.palette[i][3]) end
end
if term.setFrozen then term.setFrozen(false) end
buffer.dirtyRects, buffer.dirtyPalette = {}, {}
return
end
if term.setGraphicsMode then term.setGraphicsMode(false) end
buffer = tty.textBuffer
elseif tty.isGraphics then
term.setGraphicsMode(false)
tty.isGraphics = false
end
term.setCursorBlink(false)
if full then
term.clear()
for y = 1, tty.size.height do
term.setCursorPos(1, y)
term.blit(buffer[y][1], buffer[y][2], buffer[y][3])
end
for i = 0, 15 do term.setPaletteColor(2^i, buffer.palette[i][1], buffer.palette[i][2], buffer.palette[i][3]) end
else
for y in pairs(buffer.dirtyLines) do
if not buffer[y] then error(debug.traceback(y)) end
term.setCursorPos(1, y)
if #buffer[y][1] ~= #buffer[y][2] or #buffer[y][2] ~= #buffer[y][3] then
syslog.log({level = "critical"}, "Bug in text writer! Inequal lengths: " .. #buffer[y][1] .. ", " .. #buffer[y][2] .. ", " .. #buffer[y][3])
error("Invalid lengths")
end
term.blit(buffer[y][1], buffer[y][2], buffer[y][3])
end
for i in pairs(buffer.dirtyPalette) do term.setPaletteColor(2^i, buffer.palette[i][1], buffer.palette[i][2], buffer.palette[i][3]) end
end
term.setCursorPos(buffer.cursor.x, buffer.cursor.y)
term.setCursorBlink(buffer.cursorBlink)
term.setTextColor(2^tonumber(buffer.colors.fg, 16))
buffer.dirtyLines, buffer.dirtyPalette = {}, {}
end
--- Resizes the TTY.
-- @tparam TTY tty The TTY to resize
-- @tparam number width The new width
-- @tparam number height The new height
function terminal.resize(tty, width, height)
if width > tty.size.width then
for y = 1, tty.size.height do
tty[y][1] = tty[y][1] .. (' '):rep(width - tty.size.width)
tty[y][2] = tty[y][2] .. tty.colors.fg:rep(width - tty.size.width)
tty[y][3] = tty[y][3] .. tty.colors.bg:rep(width - tty.size.width)
tty.dirtyLines[y] = true
end
if tty.isLocked then
if tty.isGraphics then
for y = 1, tty.size.height * 9 do
tty.graphicsBuffer[y] = tty.graphicsBuffer[y] .. ('\15'):rep((width - tty.size.width) * 6)
end
tty.graphicsBuffer.dirtyRects[#tty.graphicsBuffer.dirtyRects+1] = {
x = tty.size.width * 6 + 1, y = 1,
width = (width - tty.size.width) * 6, height = tty.size.height * 9
}
else
for y = 1, tty.size.height do
tty.textBuffer[y][1] = tty.textBuffer[y][1] .. (' '):rep(width - tty.size.width)
tty.textBuffer[y][2] = tty.textBuffer[y][2] .. tty.textBuffer.colors.fg:rep(width - tty.size.width)
tty.textBuffer[y][3] = tty.textBuffer[y][3] .. tty.textBuffer.colors.bg:rep(width - tty.size.width)
tty.textBuffer.dirtyLines[y] = true
end
end
end
elseif width < tty.size.width then
for y = 1, tty.size.height do
tty[y][1] = tty[y][1]:sub(1, width)
tty[y][2] = tty[y][2]:sub(1, width)
tty[y][3] = tty[y][3]:sub(1, width)
tty.dirtyLines[y] = true
end
if tty.isLocked then
if tty.isGraphics then
for y = 1, tty.size.height * 9 do
tty.graphicsBuffer[y] = tty.graphicsBuffer[y]:sub(1, width * 6)
end
else
for y = 1, tty.size.height do
tty.textBuffer[y][1] = tty.textBuffer[y][1]:sub(1, width)
tty.textBuffer[y][2] = tty.textBuffer[y][2]:sub(1, width)
tty.textBuffer[y][3] = tty.textBuffer[y][3]:sub(1, width)
end
end
end
end
tty.size.width = width
if tty.cursor.x > width then tty.cursor.x = width end
if height > tty.size.height then
for y = tty.size.height + 1, height do
tty[y] = {(' '):rep(width), tty.colors.fg:rep(width), tty.colors.bg:rep(width)}
tty.dirtyLines[y] = true
end
if tty.isLocked then
if tty.isGraphics then
for y = tty.size.height * 9 + 1, height * 9 do
tty.graphicsBuffer[y] = ('\15'):rep(width * 6)
end
tty.graphicsBuffer.dirtyRects[#tty.graphicsBuffer.dirtyRects+1] = {
x = 1, y = tty.size.height * 9 + 1,
width = tty.size.width * 6, height = (height - tty.size.height) * 9
}
else
for y = tty.size.height + 1, height do
tty.textBuffer[y] = {(' '):rep(width), tty.textBuffer.colors.fg:rep(width), tty.textBuffer.colors.bg:rep(width)}
tty.textBuffer.dirtyLines[y] = true
end
end
end
elseif height < tty.size.height then
for y = height + 1, tty.size.height do
tty[y] = nil
tty.dirtyLines[y] = nil
end
if tty.isLocked then
if tty.isGraphics then
for y = height * 9 + 1, tty.size.height * 9 do
tty.graphicsBuffer[y] = nil
end
else
for y = height + 1, tty.size.height do
tty.textBuffer[y] = nil
tty.textBuffer.dirtyLines[y] = nil
end
end
end
end
tty.size.height = height
if tty.cursor.y > height then tty.cursor.y = height end
end
local function nextline(tty)
local cursor = tty.cursor
local y = cursor.y + 1
cursor.y = y
local size = tty.size
local height = size.height
if y > height then
--table.remove(tty, 1)
local dirtyLines = tty.dirtyLines
for i = 1, height - 1 do
tty[i] = tty[i+1]
dirtyLines[i] = true
end
local width = size.width
local colors = tty.colors
tty[height] = {(' '):rep(width), colors.fg:rep(width), colors.bg:rep(width)}
dirtyLines[height] = true
cursor.y = height
end
end
-- TODO: We could probably optimize a lot of the table indexes here to speed things up.
-- This will become important as things start needing to write quickly.
-- The biggest improvement will likely come from caching `tty.cursor.<x|y>`.
local CSI = {
['@'] = function(tty, params)
local p = params[1] or 1
if p == 0 then p = 1 end
local xp, yp = p % tty.size.width, math.floor(p / tty.size.width)
local n = {
tty[tty.cursor.y][1]:sub(tty.size.width - xp + 1),
tty[tty.cursor.y][2]:sub(tty.size.width - xp + 1),
tty[tty.cursor.y][3]:sub(tty.size.width - xp + 1)
}
tty[tty.cursor.y][1] = tty[tty.cursor.y][1]:sub(1, tty.cursor.x - 1) .. (" "):rep(p) .. tty[tty.cursor.y+yp][1]:sub(tty.cursor.x, tty.size.width - xp)
tty[tty.cursor.y][2] = tty[tty.cursor.y][2]:sub(1, tty.cursor.x - 1) .. tty.colors.fg:rep(p) .. tty[tty.cursor.y+yp][2]:sub(tty.cursor.x, tty.size.width - xp)
tty[tty.cursor.y][3] = tty[tty.cursor.y][3]:sub(1, tty.cursor.x - 1) .. tty.colors.bg:rep(p) .. tty[tty.cursor.y+yp][3]:sub(tty.cursor.x, tty.size.width - xp)
tty.dirtyLines[tty.cursor.y] = true
for y = tty.cursor.y + yp + 1, tty.size.height do
local nn = {
tty[y-yp][1]:sub(tty.size.width - p + 1),
tty[y-yp][2]:sub(tty.size.width - p + 1),
tty[y-yp][3]:sub(tty.size.width - p + 1)
}
tty[y][1] = n[1] .. tty[y-yp][1]:sub(1, tty.size.width - xp)
tty[y][2] = n[2] .. tty[y-yp][2]:sub(1, tty.size.width - xp)
tty[y][3] = n[3] .. tty[y-yp][3]:sub(1, tty.size.width - xp)
tty.dirtyLines[y] = true
n = nn
end
for y = tty.cursor.y + 1, tty.cursor.y + yp do
tty[y][1] = (" "):rep(tty.size.width)
tty[y][2] = tty.colors.fg:rep(tty.size.width)
tty[y][3] = tty.colors.bg:rep(tty.size.width)
tty.dirtyLines[y] = true
end
end, -- ICH
A = function(tty, params)
local p = params[1] or 1
if p == 0 then p = 1 end
tty.cursor.y = math.max(tty.cursor.y - p, 1)
end, -- CUU
B = function(tty, params)
local p = params[1] or 1
if p == 0 then p = 1 end
tty.cursor.y = math.min(tty.cursor.y + p, tty.size.height)
end, -- CUD
C = function(tty, params)
local p = params[1] or 1
if p == 0 then p = 1 end
tty.cursor.y = math.min(math.max(tty.cursor.y + math.floor((tty.cursor.x - 1 + p) / tty.size.width), 1), tty.size.height)
tty.cursor.x = (tty.cursor.x - 1 + p) % tty.size.width + 1
end, -- CUF
D = function(tty, params)
local p = params[1] or 1
if p == 0 then p = 1 end
tty.cursor.y = math.min(math.max(tty.cursor.y + math.floor((tty.cursor.x - 1 - p) / tty.size.width), 1), tty.size.height)
tty.cursor.x = (tty.cursor.x - 1 - p) % tty.size.width + 1
end, -- CUB
E = function(tty, params)
local p = params[1] or 1
if p == 0 then p = 1 end
tty.cursor.y = math.min(tty.cursor.y + p, tty.size.height)
tty.cursor.x = 1
end, -- CNL
F = function(tty, params)
local p = params[1] or 1
if p == 0 then p = 1 end
tty.cursor.y = math.max(tty.cursor.y - p, 1)
tty.cursor.x = 1
end, -- CPL
G = function(tty, params)
local p = params[1] or 1
if p == 0 then p = 1 end
tty.cursor.x = math.min(p, tty.size.width)
end, -- CHA
H = function(tty, params)
local r, c = params[1] or 1, params[2] or 1
if r == 0 then r = 1 end
if c == 0 then c = 1 end
tty.cursor.x, tty.cursor.y = math.min(c, tty.size.width), math.min(r, tty.size.height)
end, -- CUP
I = function(tty, params) end, -- CHT
J = function(tty, params)
local n = params[1] or 0
if n == 0 then
tty[tty.cursor.y][1] = tty[tty.cursor.y][1]:sub(1, tty.cursor.x - 1) .. (" "):rep(tty.size.width - tty.cursor.x)
tty[tty.cursor.y][2] = tty[tty.cursor.y][2]:sub(1, tty.cursor.x - 1) .. tty.colors.fg:rep(tty.size.width - tty.cursor.x)
tty[tty.cursor.y][3] = tty[tty.cursor.y][3]:sub(1, tty.cursor.x - 1) .. tty.colors.bg:rep(tty.size.width - tty.cursor.x)
tty.dirtyLines[tty.cursor.y] = true
for y = tty.cursor.y + 1, tty.size.height do
tty[y][1] = (" "):rep(tty.size.width)
tty[y][2] = tty.colors.fg:rep(tty.size.width)
tty[y][3] = tty.colors.bg:rep(tty.size.width)
tty.dirtyLines[y] = true
end
elseif n == 1 then
tty[tty.cursor.y][1] = (" "):rep(tty.cursor.x) .. tty[tty.cursor.y][1]:sub(tty.cursor.x)
tty[tty.cursor.y][2] = tty.colors.fg:rep(tty.cursor.x) .. tty[tty.cursor.y][2]:sub(tty.cursor.x)
tty[tty.cursor.y][3] = tty.colors.bg:rep(tty.cursor.x) .. tty[tty.cursor.y][3]:sub(tty.cursor.x)
tty.dirtyLines[tty.cursor.y] = true
for y = tty.cursor.y - 1, 1, -1 do
tty[y][1] = (" "):rep(tty.size.width)
tty[y][2] = tty.colors.fg:rep(tty.size.width)
tty[y][3] = tty.colors.bg:rep(tty.size.width)
tty.dirtyLines[y] = true
end
elseif n == 2 then
for y = 1, tty.size.height do
tty[y][1] = (" "):rep(tty.size.width)
tty[y][2] = tty.colors.fg:rep(tty.size.width)
tty[y][3] = tty.colors.bg:rep(tty.size.width)
tty.dirtyLines[y] = true
end
-- NOTE: if we ever want scroll support in the console, add n == 3 to clear the scrollback buffer
-- this will probably never happen, but who knows?
end
end, -- ED
K = function(tty, params)
local n = params[1] or 0
if n == 0 then
tty[tty.cursor.y][1] = tty[tty.cursor.y][1]:sub(1, tty.cursor.x - 1) .. (" "):rep(tty.size.width - tty.cursor.x)
tty[tty.cursor.y][2] = tty[tty.cursor.y][2]:sub(1, tty.cursor.x - 1) .. tty.colors.fg:rep(tty.size.width - tty.cursor.x)
tty[tty.cursor.y][3] = tty[tty.cursor.y][3]:sub(1, tty.cursor.x - 1) .. tty.colors.bg:rep(tty.size.width - tty.cursor.x)
tty.dirtyLines[tty.cursor.y] = true
elseif n == 1 then
tty[tty.cursor.y][1] = (" "):rep(tty.cursor.x) .. tty[tty.cursor.y][1]:sub(tty.cursor.x)
tty[tty.cursor.y][2] = tty.colors.fg:rep(tty.cursor.x) .. tty[tty.cursor.y][2]:sub(tty.cursor.x)
tty[tty.cursor.y][3] = tty.colors.bg:rep(tty.cursor.x) .. tty[tty.cursor.y][3]:sub(tty.cursor.x)
tty.dirtyLines[tty.cursor.y] = true
elseif n == 2 then
tty[tty.cursor.y][1] = (" "):rep(tty.size.width)
tty[tty.cursor.y][2] = tty.colors.fg:rep(tty.size.width)
tty[tty.cursor.y][3] = tty.colors.bg:rep(tty.size.width)
tty.dirtyLines[tty.cursor.y] = true
end
end, -- EL
L = function(tty, params) end, -- IL
M = function(tty, params) end, -- DL
N = function(tty, params) end, -- EF
O = function(tty, params) end, -- EA
P = function(tty, params)
local p = params[1] or 1
if p == 0 then p = 1 end
local xp, yp = p % tty.size.width, math.floor(p / tty.size.width)
local n = {
(" "):rep(xp),
tty.colors.fg:rep(xp),
tty.colors.bg:rep(xp)
}
for y = tty.size.height - yp, tty.cursor.y + 1, -1 do
local nn = {
tty[y+yp][1]:sub(1, xp),
tty[y+yp][2]:sub(1, xp),
tty[y+yp][3]:sub(1, xp)
}
tty[y][1] = tty[y+yp][1]:sub(xp + 1) .. n[1]
tty[y][2] = tty[y+yp][2]:sub(xp + 1) .. n[2]
tty[y][3] = tty[y+yp][3]:sub(xp + 1) .. n[3]
tty.dirtyLines[y] = true
n = nn
end
for y = tty.size.height - yp + 1, tty.size.height do
tty[y][1] = (" "):rep(tty.size.width)
tty[y][2] = tty.colors.fg:rep(tty.size.width)
tty[y][3] = tty.colors.bg:rep(tty.size.width)
tty.dirtyLines[y] = true
end
tty[tty.cursor.y][1] = tty[tty.cursor.y][1]:sub(1, tty.cursor.x - 1) .. tty[tty.cursor.y+yp][1]:sub(tty.cursor.x + xp, tty.size.width) .. n[1]
tty[tty.cursor.y][2] = tty[tty.cursor.y][2]:sub(1, tty.cursor.x - 1) .. tty[tty.cursor.y+yp][2]:sub(tty.cursor.x + xp, tty.size.width) .. n[2]
tty[tty.cursor.y][3] = tty[tty.cursor.y][3]:sub(1, tty.cursor.x - 1) .. tty[tty.cursor.y+yp][3]:sub(tty.cursor.x + xp, tty.size.width) .. n[3]
tty.dirtyLines[tty.cursor.y] = true
end, -- DCH
Q = function(tty, params) end, -- SSE
R = function(tty, params) end, -- CPR
S = function(tty, params)
local n = params[1] or 0
if n == 0 then n = 1 end
-- TODO: possibly optimize this?
for _ = 1, n do
table.insert(tty, 1, {(' '):rep(tty.size.width), tty.colors.fg:rep(tty.size.width), tty.colors.bg:rep(tty.size.width)})
tty[tty.size.height + 1] = nil
end
for y = 1, tty.size.height do tty.dirtyLines[y] = true end
end, -- SU
T = function(tty, params)
local n = params[1] or 0
if n == 0 then n = 1 end
-- TODO: possibly optimize this?
for _ = 1, n do
table.remove(tty, 1)
tty[tty.size.height] = {(' '):rep(tty.size.width), tty.colors.fg:rep(tty.size.width), tty.colors.bg:rep(tty.size.width)}
end
for y = 1, tty.size.height do tty.dirtyLines[y] = true end
end, -- SD
U = function(tty, params) end, -- NP
V = function(tty, params) end, -- PP
W = function(tty, params) end, -- CTC
X = function(tty, params) end, -- ECH
Y = function(tty, params) end, -- CVT
Z = function(tty, params) end, -- CBT
['['] = function(tty, params) end, -- SRS
['\\'] = function(tty, params) end, -- PTX
[']'] = function(tty, params) end, -- SDS
['^'] = function(tty, params) end, -- SIMD
['_'] = function(tty, params) end, -- N/A
['`'] = function(tty, params) end, -- HPA
a = function(tty, params) end, -- HPR
b = function(tty, params) end, -- REP
c = function(tty, params) end, -- DA
d = function(tty, params) end, -- VPA
e = function(tty, params) end, -- VPR
f = function(tty, params) end, -- HVP
g = function(tty, params) end, -- TBC
h = function(tty, params)
if params[1] == 25 then tty.cursorBlink = true end
end, -- SM
i = function(tty, params) end, -- MC
j = function(tty, params) end, -- HPB
k = function(tty, params) end, -- VPB
l = function(tty, params)
if params[1] == 25 then tty.cursorBlink = false end
end, -- RM
m = function(tty, params)
local n, m = params[1] or 0, params[2]
if n == 0 then tty.colors.fg, tty.colors.bg, tty.colors.bold = '0', 'f', false
elseif n == 1 then tty.colors.bold = true
elseif n == 7 or n == 27 then tty.colors.fg, tty.colors.bg = tty.colors.bg, tty.colors.fg
elseif n == 22 then tty.colors.bold = false
elseif n >= 30 and n <= 37 then tty.colors.fg = ("%x"):format(15 - (n - 30) - (tty.colors.bold and 8 or 0))
elseif n == 39 then tty.colors.fg = '0'
elseif n >= 40 and n <= 47 then tty.colors.bg = ("%x"):format(15 - (n - 40) - (tty.colors.bold and 8 or 0))
elseif n == 49 then tty.colors.bg = 'f'
elseif n >= 90 and n <= 97 then tty.colors.fg = ("%x"):format(15 - (n - 90) - 8)
elseif n >= 100 and n <= 107 then tty.colors.bg = ("%x"):format(15 - (n - 100) - 8) end
if m ~= nil then
if m == 0 then tty.colors.fg, tty.colors.bg = '0', 'f'
elseif m == 1 then tty.colors.bold = true
elseif m == 7 or m == 27 then tty.colors.fg, tty.colors.bg = tty.colors.bg, tty.colors.fg
elseif m == 22 then tty.colors.bold = false
elseif m >= 30 and m <= 37 then tty.colors.fg = ("%x"):format(15 - (m - 30) - (tty.colors.bold and 8 or 0))
elseif m == 39 then tty.colors.fg = '0'
elseif m >= 40 and m <= 47 then tty.colors.bg = ("%x"):format(15 - (m - 40) - (tty.colors.bold and 8 or 0))
elseif m == 49 then tty.colors.bg = 'f'
elseif m >= 90 and m <= 97 then tty.colors.fg = ("%x"):format(15 - (m - 90) - 8)
elseif m >= 100 and m <= 107 then tty.colors.bg = ("%x"):format(15 - (m - 100) - 8) end
end
end, -- SGR
n = function(tty, params)
-- TODO: send the cursor position to stdin
end, -- DSR
o = function(tty, params) end, -- DAQ
}
for i = 0x70, 0x7F do CSI[string.char(i)] = function(tty, params) end end
--- Writes some text to a TTY's text buffer, allowing ANSI escapes.
-- @tparam TTY tty The TTY to write to
-- @tparam string text The text to write
function terminal.write(tty, text)
if _HEADLESS and tty == TTY[1] then return term.write(text) end
local start, size = 1, 0
local function commit(x)
if size == 0 then
start, size = x, 0
return
end
while tty.cursor.x + size > tty.size.width do
tty[tty.cursor.y][1] = tty[tty.cursor.y][1]:sub(1, tty.cursor.x - 1) .. text:sub(start, start + tty.size.width - tty.cursor.x)
tty[tty.cursor.y][2] = tty[tty.cursor.y][2]:sub(1, tty.cursor.x - 1) .. tty.colors.fg:rep(tty.size.width - tty.cursor.x + 1)
tty[tty.cursor.y][3] = tty[tty.cursor.y][3]:sub(1, tty.cursor.x - 1) .. tty.colors.bg:rep(tty.size.width - tty.cursor.x + 1)
tty.dirtyLines[tty.cursor.y] = true
start = start + tty.size.width - tty.cursor.x + 1
size = size - (tty.size.width - tty.cursor.x + 1)
tty.cursor.x = 1
nextline(tty)
end
tty[tty.cursor.y][1] = tty[tty.cursor.y][1]:sub(1, tty.cursor.x - 1) .. text:sub(start, start + size - 1) .. tty[tty.cursor.y][1]:sub(tty.cursor.x + size)
tty[tty.cursor.y][2] = tty[tty.cursor.y][2]:sub(1, tty.cursor.x - 1) .. tty.colors.fg:rep(size) .. tty[tty.cursor.y][2]:sub(tty.cursor.x + size)
tty[tty.cursor.y][3] = tty[tty.cursor.y][3]:sub(1, tty.cursor.x - 1) .. tty.colors.bg:rep(size) .. tty[tty.cursor.y][3]:sub(tty.cursor.x + size)
tty.dirtyLines[tty.cursor.y] = true
tty.cursor.x = tty.cursor.x + size
start, size = x, 0
end
local state = 0
local params, nextParam
for x, c, n in text:gmatch "()(.)()" do
if state == 0 then
if c == '\a' then
commit(n)
local spk = hardware.find("speaker")
if spk then hardware.call(spk, "playNote", "pling") end
elseif c == '\b' then
commit(n)
if tty.cursor.x == 1 then
if tty.cursor.y > 1 then tty.cursor.x, tty.cursor.y = tty.size.width, tty.cursor.y - 1 end
else tty.cursor.x = tty.cursor.x - 1 end
elseif c == '\t' then
commit(n)
tty.cursor.x = math.floor((tty.cursor.x - 1) / 8) * 8 + 9
if tty.cursor.x > tty.size.width then
tty.cursor.x = 1
nextline(tty)
end
elseif c == '\n' then
commit(n)
nextline(tty)
if tty.flags.nlcr then tty.cursor.x = 1 end
elseif c == '\f' then
commit(n)
nextline(tty)
elseif c == '\r' then
commit(n)
tty.cursor.x = 1
elseif c == '\27' then
state = 1
else
size = size + 1
end
elseif state == 1 then
-- TODO: Implement whatever of these are applicable
if false then
--[[ elseif c == 'B' then -- BPH
elseif c == 'C' then -- NBH
elseif c == 'E' then -- NEL
elseif c == 'F' then -- SSA
elseif c == 'G' then -- ESA
elseif c == 'H' then -- HTS
elseif c == 'I' then -- HTJ
elseif c == 'J' then -- VTS
elseif c == 'K' then -- PLD
elseif c == 'L' then -- PLU
elseif c == 'M' then -- RI
elseif c == 'N' then -- SS2
elseif c == 'O' then -- SS3
elseif c == 'P' then -- DCS
elseif c == 'Q' then -- PU1
elseif c == 'R' then -- PU2
elseif c == 'S' then -- STS
elseif c == 'T' then -- CCH
elseif c == 'U' then -- MW
elseif c == 'V' then -- SPA
elseif c == 'W' then -- EPA
elseif c == 'X' then -- SOS
elseif c == 'Z' then -- SCI ]]
elseif c == '[' then -- CSI
state = 2
params, nextParam = {}, 0
--[[ elseif c == '\\' then -- ST ]]
elseif c == ']' then -- OSC
if text:byte(n) == 0x50 then
state = 4
params = {}
else
state = 3
params, nextParam = {}, 0
end
--[[ elseif c == '^' then -- PM
elseif c == '_' then -- APC ]]
else
commit(n)
state = 0
end
elseif state == 2 then
if c >= '@' and c <= '\127' then
commit(n)
params[#params+1] = nextParam
CSI[c](tty, params)
state = 0
elseif c >= '0' and c <= '?' then
if c <= '9' then
nextParam = nextParam * 10 + tonumber(c)
elseif c == ';' then
params[#params+1], nextParam = nextParam, 0
end
else
commit(n)
state = 0
end
elseif state == 3 then
-- TODO: properly handle this
if c == '\\' and text:byte(x - 1) == '\27' then
commit(n)
state = 0
end
elseif state == 4 then
if #params == 0 then
params[1] = tonumber(c, 16) or 0
elseif #params == 1 and not nextParam then
nextParam = (tonumber(c, 16) or 0) * 16
elseif #params == 1 then
params[2], nextParam = nextParam + (tonumber(c, 16) or 0), nil
elseif #params == 2 and not nextParam then
nextParam = (tonumber(c, 16) or 0) * 16
elseif #params == 2 then
params[3], nextParam = nextParam + (tonumber(c, 16) or 0), nil
elseif #params == 3 and not nextParam then
nextParam = (tonumber(c, 16) or 0) * 16
elseif #params == 3 then
commit(n)
params[4], nextParam = nextParam + (tonumber(c, 16) or 0), nil
tty.palette[params[1]] = {params[2] / 255, params[3] / 255, params[4] / 255}
tty.dirtyPalette[params[1]] = true
state = 0
end
end
end
commit()
end
function syscalls.write(process, thread, ...)
if not process.stdout then return end
--[[if process.stdout.isTTY and process ~= process.stdout.frontmostProcess then
syscalls.kill(KERNEL, nil, process.id, 22)
if process.paused then return kSyscallYield, "write" end
end]]
local function write(t)
if process.stdout.isTTY then terminal.write(process.stdout, t)
else process.stdout.write(t) end
end
local args = table.pack(...)
for i = 1, args.n do
if i > 1 then write("\t") end
write(tostring(args[i])) -- TODO: avoid __tostring injection
end
if process.stdout.isTTY then terminal.redraw(process.stdout) end
end
function syscalls.writeerr(process, thread, ...)
if not process.stderr then return end
--[[if process.stderr.isTTY and process ~= process.stderr.frontmostProcess then
syscalls.kill(KERNEL, nil, process.id, 22)
if process.paused then return kSyscallYield, "writeerr" end
end]]
local function write(t)
if process.stderr.isTTY then terminal.write(process.stderr, t)
else process.stderr.write(t) end
end
local args = table.pack(...)
for i = 1, args.n do
if i > 1 then write("\t") end
write(tostring(args[i])) -- TODO: avoid __tostring injection
end
if process.stderr.isTTY then terminal.redraw(process.stderr) end
end
function syscalls.read(process, thread, n)
expect(1, n, "number")
if process.stdin then
--[[if process.stdin.isTTY and process ~= process.stdin.frontmostProcess then
syscalls.kill(KERNEL, nil, process.id, 21)
if process.paused then return kSyscallYield, "readline" end
end]]
if process.stdin.eof then
process.stdin.eof = false
return nil
end
while #process.stdin.buffer < n do
if process.stdin.eof then
process.stdin.eof = false
return nil
end
if process.stdin.isTTY and not process.stdin.flags.delay then return nil end
if process.stdin.read then
local s = process.stdin.read(n - #process.stdin.buffer)
if not s then return nil end
process.stdin.buffer = process.stdin.buffer .. s
else return kSyscallYield, "read", n end
end
local s = process.stdin.buffer:sub(1, n - 1)
process.stdin.buffer = process.stdin.buffer:sub(n)
return s
else return nil end
end
function syscalls.readline(process, thread)
if process.stdin then
--[[if process.stdin.isTTY and process ~= process.stdin.frontmostProcess then
syscalls.kill(KERNEL, nil, process.id, 21)
if process.paused then return kSyscallYield, "readline" end
end]]
if process.stdin.eof then
process.stdin.eof = false
return nil
end
while not process.stdin.buffer:find("\n") do
if process.stdin.eof then
process.stdin.eof = false
return nil
end
if process.stdin.isTTY and not process.stdin.flags.delay then return nil end
if process.stdin.read then
local s = process.stdin.read()
if not s then return nil end
process.stdin.buffer = process.stdin.buffer .. s
else return kSyscallYield, "readline" end
end
local n = process.stdin.buffer:find("\n")
local s = process.stdin.buffer:sub(1, n - 1)
process.stdin.buffer = process.stdin.buffer:sub(n + 1)
return s
else return nil end
end
function syscalls.termctl(process, thread, flags)
expect(1, flags, "table", "nil")
if not process.stdout or not process.stdout.isTTY then return nil end
--[[if process ~= process.stdout.frontmostProcess then
syscalls.kill(KERNEL, nil, process.id, 22)
if process.paused then return kSyscallYield, "termctl", flags end
end]]
if flags then
expect.field(flags, "cbreak", "boolean", "nil")
expect.field(flags, "delay", "boolean", "nil")
expect.field(flags, "echo", "boolean", "nil")
expect.field(flags, "keypad", "boolean", "nil")
expect.field(flags, "nlcr", "boolean", "nil")
expect.field(flags, "raw", "boolean", "nil")
for k, v in pairs(flags) do if process.stdout.flags[k] ~= nil then process.stdout.flags[k] = v end end
end
local t = deepcopy(process.stdout.flags)
t.hasgfx = term.getGraphicsMode ~= nil
return t
end
function terminal.openterm(tty, process)
if tty.isLocked then
if not tty.isGraphics and tty.frontmostProcess == process then return tty.screenHandle end
return nil, "Terminal already in use"
end
local size = tty.size
local buffer = {
cursor = {x = 1, y = 1},
cursorBlink = false,
colors = {fg = '0', bg = 'f'},
palette = {},
dirtyLines = {},
dirtyPalette = {},
}
tty.textBuffer = buffer
tty.isLocked = true
tty.isGraphics = false
for y = 1, size.height do
buffer[y] = {(' '):rep(size.width), ('0'):rep(size.width), ('f'):rep(size.width)}
buffer.dirtyLines[y] = true
end
for i = 0, 15 do
buffer.palette[i] = {term.nativePaletteColor(2^i)}
buffer.dirtyPalette[i] = true
end
tty.processList[#tty.processList+1] = tty.frontmostProcess
tty.frontmostProcess = process
local win = setmetatable({}, {__name = "Terminal"})
local redraw = terminal.redraw
local expect = expect
tty.screenHandle = win
function win.close()
if not win then error("terminal is already closed", 2) end
win = nil
tty.isLocked = false
tty.frontmostProcess = table.remove(tty.processList)
tty.screenHandle = nil
redraw(tty, true)
end
function win.write(text)
if not win then error("terminal is already closed", 2) end
text = tostring(text)
expect(1, text, "string")
if buffer.cursor.y < 1 or buffer.cursor.y > size.height then return
elseif buffer.cursor.x > size.width or buffer.cursor.x + #text < 1 then
buffer.cursor.x = buffer.cursor.x + #text
return
elseif buffer.cursor.x < 1 then
text = text:sub(-buffer.cursor.x + 2)
buffer.cursor.x = 1
end
local ntext = #text
if buffer.cursor.x + #text > size.width then text = text:sub(1, size.width - buffer.cursor.x + 1) end
buffer[buffer.cursor.y][1] = buffer[buffer.cursor.y][1]:sub(1, buffer.cursor.x - 1) .. text .. buffer[buffer.cursor.y][1]:sub(buffer.cursor.x + #text)
buffer[buffer.cursor.y][2] = buffer[buffer.cursor.y][2]:sub(1, buffer.cursor.x - 1) .. buffer.colors.fg:rep(#text) .. buffer[buffer.cursor.y][2]:sub(buffer.cursor.x + #text)
buffer[buffer.cursor.y][3] = buffer[buffer.cursor.y][3]:sub(1, buffer.cursor.x - 1) .. buffer.colors.bg:rep(#text) .. buffer[buffer.cursor.y][3]:sub(buffer.cursor.x + #text)
buffer.cursor.x = buffer.cursor.x + ntext
buffer.dirtyLines[buffer.cursor.y] = true
--redraw(tty)
end
function win.blit(text, fg, bg)
if not win then error("terminal is already closed", 2) end
text = tostring(text)
expect(1, text, "string")
expect(2, fg, "string")
expect(3, bg, "string")
if #text ~= #fg or #fg ~= #bg then error("Arguments must be the same length", 2) end
if buffer.cursor.y < 1 or buffer.cursor.y > size.height then return
elseif buffer.cursor.x > size.width or buffer.cursor.x < 1 - #text then
buffer.cursor.x = buffer.cursor.x + #text
redraw(tty)
return
elseif buffer.cursor.x < 1 then
text, fg, bg = text:sub(-buffer.cursor.x + 2), fg:sub(-buffer.cursor.x + 2), bg:sub(-buffer.cursor.x + 2)
buffer.cursor.x = 1
end
local ntext = #text
if buffer.cursor.x + #text > size.width then text, fg, bg = text:sub(1, size.width - buffer.cursor.x + 1), fg:sub(1, size.width - buffer.cursor.x + 1), bg:sub(1, size.width - buffer.cursor.x + 1) end
buffer[buffer.cursor.y][1] = buffer[buffer.cursor.y][1]:sub(1, buffer.cursor.x - 1) .. text .. buffer[buffer.cursor.y][1]:sub(buffer.cursor.x + #text)
buffer[buffer.cursor.y][2] = buffer[buffer.cursor.y][2]:sub(1, buffer.cursor.x - 1) .. fg .. buffer[buffer.cursor.y][2]:sub(buffer.cursor.x + #fg)
buffer[buffer.cursor.y][3] = buffer[buffer.cursor.y][3]:sub(1, buffer.cursor.x - 1) .. bg .. buffer[buffer.cursor.y][3]:sub(buffer.cursor.x + #bg)
buffer.cursor.x = buffer.cursor.x + ntext
buffer.dirtyLines[buffer.cursor.y] = true
--redraw(tty)
end
function win.clear()
if not win then error("terminal is already closed", 2) end
for y = 1, size.height do
buffer[y] = {(' '):rep(size.width), buffer.colors.fg:rep(size.width), buffer.colors.bg:rep(size.width)}
buffer.dirtyLines[y] = true
end
--redraw(tty)
end
function win.clearLine()
if not win then error("terminal is already closed", 2) end
if buffer.cursor.y >= 1 and buffer.cursor.y <= size.height then
buffer[buffer.cursor.y] = {(' '):rep(size.width), buffer.colors.fg:rep(size.width), buffer.colors.bg:rep(size.width)}
buffer.dirtyLines[buffer.cursor.y] = true
--redraw(tty)
end
end
function win.getCursorPos()
if not win then error("terminal is already closed", 2) end
return buffer.cursor.x, buffer.cursor.y
end
function win.setCursorPos(cx, cy)
if not win then error("terminal is already closed", 2) end
expect(1, cx, "number")
expect(2, cy, "number")
if cx == buffer.cursor.x and cy == buffer.cursor.y then return end
buffer.cursor.x, buffer.cursor.y = math.floor(cx), math.floor(cy)
--redraw(tty)
end
function win.getCursorBlink()
if not win then error("terminal is already closed", 2) end
return buffer.cursorBlink
end
function win.setCursorBlink(b)
if not win then error("terminal is already closed", 2) end
expect(1, b, "boolean")
buffer.cursorBlink = b
--redraw(tty)
end
function win.isColor()
if not win then error("terminal is already closed", 2) end
return true
end