-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.go
1551 lines (1362 loc) · 47.1 KB
/
display.go
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
// Copyright 2022 wangqi. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package terminal
import (
"fmt"
"os"
"strings"
"github.com/ericwq/terminfo"
_ "github.com/ericwq/terminfo/base"
"github.com/ericwq/terminfo/dynamic"
"golang.org/x/exp/constraints"
)
// LookupTerminfo attempts to find a definition for the named $TERM falling
// back to attempting to parse the output from infocmp.
func LookupTerminfo(name string) (ti *terminfo.Terminfo, e error) {
ti, e = terminfo.LookupTerminfo(name)
if e != nil {
// ti, e = loadDynamicTerminfo(name)
ti, _, e := dynamic.LoadTerminfo(name)
if e != nil {
return nil, e
}
terminfo.AddTerminfo(ti)
}
return
}
// extract specified row from the resize screen.
// func getRowFrom(from []Cell, posY int, w int) (row []Cell) {
// start := posY * w
// end := start + w
// row = from[start:end]
// return row
// }
// func getRawRow(emu *Emulator, rowY int) (row []Cell) {
// start := emu.nCols * rowY
// end := start + emu.nCols
// row = emu.cf.cells[start:end]
// return row
// }
// return the specified row from terminal.
func getRow(emu *Emulator, posY int) (row []Cell) {
start := emu.cf.getViewRowIdx(posY)
end := start + emu.nCols
row = emu.cf.cells[start:end]
return row
}
func equalRow(a, b []Cell) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func equalSlice[T constraints.Ordered](a, b []T) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func firstScreen(oldE, newE *Emulator) bool {
return newE.cf.scrollHead == 0 && newE.cf.scrollHead == oldE.cf.scrollHead
}
func calculateRows(oldE, newE *Emulator) int {
// no head scroll
if firstScreen(oldE, newE) {
if newE.posY == oldE.posY {
if newE.posX != oldE.posX {
return 1
}
return 0
}
// if newE.posY == 0 { // got something like \x1b[H\x1b[2J\x1b[3J
// return newE.nRows
// }
return newE.posY - oldE.posY + 1
}
// head scrolled
if newE.cf.scrollHead > oldE.cf.scrollHead {
// new screen head is greater than old screen head
if newE.posY == oldE.posY && newE.posY == newE.nRows-1 {
return newE.cf.scrollHead - oldE.cf.scrollHead + 1
}
gap := oldE.nRows - oldE.posY + // old screen remains rows
(newE.cf.scrollHead - oldE.cf.scrollHead) // new screen moves rows
return gap
} else if newE.cf.scrollHead == oldE.cf.scrollHead {
if newE.posY == oldE.posY && newE.posY == newE.nRows-1 {
// just one line
if newE.posX != oldE.posX {
return 1
}
return 0
}
// new screen head is same as old screen head
return newE.posY - oldE.posY
}
// new screen head is smaller than old screen head (rewind)
return oldE.cf.marginBottom - oldE.cf.scrollHead + // old screen part
newE.cf.scrollHead + newE.posY + 1 + // new screen part
-oldE.posY // remove old screen exist rows
}
/*
* The following are some interesting questions I asked several month ago. As I know terminfo
* and terminal better, the answter is more clear and confident than before.
*
* do we need to package the the terminfo DB into application?
* - yes, mosh-server depends on ncurses-terminfo-base and ncurses-libs
* how to read terminfo DB? through ncurses lib or directly?
* - yes the answer is read through tcell.
* how to operate terminal? through direct escape sequence or through terminfo DB?
* how to replace the following functions? setupterm(), tigetnum(), tigetstr(), tigetflag()
*/
type Display struct {
// erase character is part of vt200 but not supported by tmux
// (or by "screen" terminfo entry, which is what tmux advertises)
hasECH bool
hasBCE bool // erases result in cell filled with background color
supportTitle bool // supports window title and icon name
smcup string // enter and exit alternate screen mode
rmcup string // enter and exit alternate screen mode
// ti *terminfo.Terminfo
// fields from FrameState
// cursorX, cursorY int
// currentRendition Renditions
// showCursorMode bool // mosh: cursorVisible
// logW *log.Logger
}
// https://github.com/gdamore/tcell the successor of termbox-go
// https://cs.opensource.google/go/x/term/+/master:README.md
// apk add mandoc man-pages ncurses-doc
// apk add ncurses-terminfo
// apk add ncurses-terminfo-base
// apk add ncurses
// https://ishuah.com/2021/03/10/build-a-terminal-emulator-in-100-lines-of-go/
//
// use TERM environment var to initialize display, if useEnvironment is true.
func NewDisplay(useEnvironment bool) (d *Display, e error) {
d = &Display{}
d.hasECH = true
d.hasBCE = true
d.supportTitle = true
// d.logW = log.New(os.Stderr, "WARN: ", log.Ldate|log.Ltime|log.Lshortfile)
if useEnvironment {
term := os.Getenv("TERM")
var ti *terminfo.Terminfo
ti, e = LookupTerminfo(term)
if e != nil {
return nil, e
}
// check for ECH
if ti.EraseChars != "" {
d.hasECH = true
}
// check for BCE
if ti.BackColorErase {
d.hasBCE = true
}
/* Check if we can set the window title and icon name. terminfo does not
have reliable information on this, so we hardcode a whitelist of
terminal type prefixes. */
d.supportTitle = false
titleTermTypes := []string{"xterm", "rxvt", "kterm", "Eterm", "alacritty", "screen", "tmux"}
if term != "" {
for _, tt := range titleTermTypes {
if strings.HasPrefix(term, tt) {
d.supportTitle = true
break
}
}
}
// TODO consider use MOSH_NO_TERM_INIT to control this behavior
d.smcup = ti.EnterCA
d.rmcup = ti.ExitCA
// d.ti = ti
// util.Log.Debug("NewDisplay",
// "smcup", d.smcup,
// "rmcup", d.rmcup,
// "term", term)
}
return d, nil
}
// compare two terminals and generate mix (grapheme and control sequence) sequence
// to rebuild the new terminal from the old one.
//
// - initialized: if false, it will redraw the whole terminal, otherwise only changed part.
// - oldE: the old terminal state.
// - newE: the new terminal state.
func (d *Display) NewFrame(initialized bool, oldE, newE *Emulator) string {
frame := new(FrameState)
frame.cursorX = 0
frame.cursorY = 0
frame.currentRendition = Renditions{}
frame.showCursorMode = oldE.showCursorMode
frame.lastFrame = oldE
frame.out = &strings.Builder{}
// ti := d.ti
// has bell been rung?
if newE.getBellCount() != oldE.getBellCount() {
// ti.TPuts(&b, ti.Bell)
frame.append("\a")
}
// has icon name or window title changed?
// Enhanced: has window title stack changed?
oldWTS := oldE.windowTitleStack
newWTS := newE.windowTitleStack
titleAndStackBothChange := false
if len(oldWTS) == len(newWTS) {
if len(newWTS) == windowTitleStackMax && !equalSlice(oldWTS, newWTS) {
// if len(newWTS) == windowTitleStackMax && !reflect.DeepEqual(oldWTS, newWTS) {
// reach stack max with difference
// change title first then stack
d.titleChanged(initialized, frame, oldE, newE)
frame.append("\x1B[22;0t")
titleAndStackBothChange = true
}
} else if len(newWTS) > len(oldWTS) {
// save title to stack
// change title first then stack
d.titleChanged(initialized, frame, oldE, newE)
frame.append("\x1B[22;0t")
titleAndStackBothChange = true
} else {
// restore title from stack
// change stack first then title
frame.append("\x1B[23;0t")
d.titleChanged(initialized, frame, oldE, newE)
titleAndStackBothChange = true
}
// has icon label or window title changed?
if !titleAndStackBothChange {
d.titleChanged(initialized, frame, oldE, newE)
}
// has clipboard changed?
if newE.selectionData != oldE.selectionData {
// the selectionData is in the form of "\x1B]52;%s;%s\x1B\\"
// see hdl_osc_52() for detail
frame.append(newE.selectionData)
}
// has reverse video state changed?
if !initialized || newE.reverseVideo != oldE.reverseVideo {
// set reverse video
if newE.reverseVideo {
frame.append("\x1B[?5h")
} else {
frame.append("\x1B[?5l")
}
}
// has size changed?
// the size of the display terminal isn't changed.
// the size of the received terminal is changed by ApplyString()
sizeChanged := false
if !initialized || newE.nCols != oldE.nCols || newE.nRows != oldE.nRows {
// TODO why reset scrolling region?
frame.append("\x1B[r") // smgtb, reset scrolling region, reset top/bottom margin
// ti.TPuts(&b, ti.AttrOff) // sgr0, "\x1B[0m" turn off all attribute modes
// ti.TPuts(&b, ti.Clear) // clear, "\x1B[H\x1B[2J" clear screen and home cursor
frame.append("\x1B[0m")
frame.append("\x1B[H\x1B[2J")
initialized = false // resize will force the initialized
frame.cursorX = 0
frame.cursorY = 0
frame.currentRendition = Renditions{}
sizeChanged = true
} else {
frame.cursorX = oldE.GetCursorCol()
frame.cursorY = oldE.GetCursorRow()
frame.currentRendition = oldE.GetRenditions()
}
// is cursor visibility initialized?
if !initialized {
// fmt.Printf("#NewFrame initialized=%t, d.showCursorMode=%t\n", initialized, d.showCursorMode)
frame.showCursorMode = false
// ti.TPuts(&b, ti.HideCursor) // civis, "\x1B[?25l" showCursorMode = false
frame.append("\x1B[?25l")
}
// check 1049 first
asbChanged := false
if newE.altScreen1049 != oldE.altScreen1049 {
asbChanged = true
if newE.altScreen1049 {
frame.append("\x1B[?1049h")
} else {
frame.append("\x1B[?1049l")
}
// fmt.Printf("Display.NewFrame newE.altScreen1049=%t\n", newE.altScreen1049)
} else {
// has the screen buffer mode changed?
// change screen buffer is something like resize, except resize remains partial content,
// screen buffer mode reset the whole screen.
if !initialized || newE.altScreenBufferMode != oldE.altScreenBufferMode {
asbChanged = true
if newE.altScreenBufferMode {
frame.append("\x1B[?1047h")
} else {
frame.append("\x1B[?1047l")
}
}
// has saved cursor changed?
// Let the target terminal decide what to save, here we just issue the control sequence.
//
if !initialized || newE.savedCursor_DEC.isSet != oldE.savedCursor_DEC.isSet {
if newE.savedCursor_DEC.isSet && !oldE.savedCursor_DEC.isSet {
frame.append("\x1B[?1048h") // decsc: VT100 use \x1B7
} else if !newE.savedCursor_DEC.isSet && oldE.savedCursor_DEC.isSet {
frame.append("\x1B[?1048l") // decrc: vt100 use \x1B8
}
}
}
// has the margin changed?
if !initialized || (newE.marginTop != oldE.marginTop || newE.marginBottom != oldE.marginBottom) {
if newE.cf.margin {
frame.append("\x1B[%d;%dr", newE.marginTop+1, newE.marginBottom) // new margin
} else {
frame.append("\x1B[r") // reset margin
}
}
// has the horizontal margin changed?
if !initialized || newE.horizMarginMode != oldE.horizMarginMode {
if newE.horizMarginMode {
frame.append("\x1B[?69h")
if newE.hMargin != oldE.hMargin || newE.nColsEff != oldE.nColsEff {
// decslrm set left/right margin
frame.append("\x1B[%d;%ds", newE.hMargin+1, newE.nColsEff)
}
} else {
frame.append("\x1B[?69l")
}
}
// has SCO saved cursor changed
if !initialized || newE.savedCursor_SCO.isSet != oldE.savedCursor_SCO.isSet {
if newE.savedCursor_SCO.isSet && !oldE.savedCursor_SCO.isSet {
frame.append("\x1B[s") // scosc
} else if !newE.savedCursor_SCO.isSet && oldE.savedCursor_SCO.isSet {
frame.append("\x1B[u") // scorc
}
}
d.replicateContent(initialized, oldE, newE, sizeChanged, asbChanged, frame)
// has cursor location changed?
if !initialized || newE.GetCursorRow() != frame.cursorY || newE.GetCursorCol() != frame.cursorX {
// TODO using cursor position from display or cursor position from terminal?
frame.appendMove(newE.GetCursorRow(), newE.GetCursorCol())
}
// has cursor visibility changed?
// during update row, appendSilentMove() might close the cursor,
// Here we open cursor based on the new terminal state.
// fmt.Printf("#NewFrame newE=%t, d=%t, oldE=%t, initialized=%t\n",
// newE.showCursorMode, d.showCursorMode, oldE.showCursorMode, initialized)
if !initialized || newE.showCursorMode != frame.showCursorMode {
if newE.showCursorMode {
frame.append("\x1B[?25h") // cvvis
} else {
frame.append("\x1B[?25l") // civis
}
}
// has cursor style changed?
if !initialized || newE.cf.cursor.showStyle != oldE.cf.cursor.showStyle {
Ps := 1 // default is blinking block
switch newE.cf.cursor.showStyle {
case CursorStyle_BlinkBlock:
Ps = 1
case CursorStyle_SteadyBlock:
Ps = 2
case CursorStyle_BlinkUnderline:
Ps = 3
case CursorStyle_SteadyUnderline:
Ps = 4
case CursorStyle_BlinkBar:
Ps = 5
case CursorStyle_SteadyBar:
Ps = 6
}
frame.append("\x1B[%d q", Ps)
}
// has cursor color changed to default?
if !initialized || newE.cf.cursor.color != oldE.cf.cursor.color {
if newE.cf.cursor.color == ColorDefault {
frame.append("\x1B]112\a")
}
}
// has renditions changed?
frame.updateRendition(newE.GetRenditions(), !initialized)
// has bracketed paste mode changed?
if !initialized || newE.bracketedPasteMode != oldE.bracketedPasteMode {
if newE.bracketedPasteMode {
frame.append("\x1B[?2004h")
} else {
frame.append("\x1B[?2004l")
}
}
// has mouse reporting mode changed?
if !initialized || newE.mouseTrk.mode != oldE.mouseTrk.mode {
if newE.mouseTrk.mode == MouseTrackingMode_Disable {
frame.append("\x1B[?1003l")
frame.append("\x1B[?1002l")
frame.append("\x1B[?1001l")
frame.append("\x1B[?1000l")
} else {
// close old mouse reporting mode
if oldE.mouseTrk.mode != MouseTrackingMode_Disable {
frame.append("\x1B[?%dl", oldE.mouseTrk.mode)
}
// open new mouse reporting mode
frame.append("\x1B[?%dh", newE.mouseTrk.mode)
}
}
// has mouse focus mode changed?
if !initialized || newE.mouseTrk.focusEventMode != oldE.mouseTrk.focusEventMode {
if newE.mouseTrk.focusEventMode {
frame.append("\x1B[?1004h")
} else {
frame.append("\x1B[?1004l")
}
}
// has mouse encoding mode changed?
if !initialized || newE.mouseTrk.enc != oldE.mouseTrk.enc {
if newE.mouseTrk.enc == MouseTrackingEnc_Default {
frame.append("\x1B[?1015l")
frame.append("\x1B[?1006l")
frame.append("\x1B[?1005l")
} else {
// close old mouse encoding mode
if oldE.mouseTrk.enc != MouseTrackingEnc_Default {
frame.append("\x1B[?%dl", oldE.mouseTrk.enc)
}
// open new mouse encoding mode
frame.append("\x1B[?%dh", newE.mouseTrk.enc)
}
}
// has auto wrap mode changed?
if !initialized || newE.autoWrapMode != oldE.autoWrapMode {
if newE.autoWrapMode {
frame.append("\x1B[?7h")
} else {
frame.append("\x1B[?7l")
}
}
// has auto newline mode changed?
if !initialized || newE.autoNewlineMode != oldE.autoNewlineMode {
if newE.autoNewlineMode {
frame.append("\x1B[20h")
} else {
frame.append("\x1B[20l")
}
}
// has keyboard action mode changed?
if !initialized || newE.keyboardLocked != oldE.keyboardLocked {
if newE.keyboardLocked {
frame.append("\x1B[2h")
} else {
frame.append("\x1B[2l")
}
}
// has insert mode changed?
if !initialized || newE.insertMode != oldE.insertMode {
if newE.insertMode {
frame.append("\x1B[4h")
} else {
frame.append("\x1B[4l")
}
}
// has local echo changed?
if !initialized || newE.localEcho != oldE.localEcho {
if newE.localEcho {
frame.append("\x1B[12l") // reverse order
} else {
frame.append("\x1B[12h") // reverse order
}
}
// has backspace send delete changed?
if !initialized || newE.bkspSendsDel != oldE.bkspSendsDel {
if newE.bkspSendsDel {
frame.append("\x1B[?67l") // DECRST reverse order
} else {
frame.append("\x1B[?67h") // DECSET reverse order
}
}
// has alt key as ESC changed?
if !initialized || newE.altSendsEscape != oldE.altSendsEscape {
if newE.altSendsEscape {
frame.append("\x1B[?1036h") // DECSET
} else {
frame.append("\x1B[?1036l") // DECRST
}
}
// has altScrollMode changed?
if !initialized || newE.altScrollMode != oldE.altScrollMode {
if newE.altScrollMode {
frame.append("\x1B[?1007h") // DECSET
} else {
frame.append("\x1B[?1007l") // DECRST
}
}
// has cursor key mode changed?
// Note: This depends on real terminal emulator to apply cursorKeyMode.
if !initialized || newE.cursorKeyMode != oldE.cursorKeyMode {
switch newE.cursorKeyMode {
case CursorKeyMode_Application:
frame.append("\x1B[?1h") // DECSET
case CursorKeyMode_ANSI:
frame.append("\x1B[?1l") // DECRST
}
}
// has origin mode changed?
if !initialized || newE.originMode != oldE.originMode {
switch newE.originMode {
case OriginMode_ScrollingRegion:
frame.append("\x1B[?6h") // DECSET
case OriginMode_Absolute:
frame.append("\x1B[?6l") // DECRST
}
}
// has keypad mode changed?
// Note: This depends on real terminal emulator to apply keypadMode.
if !initialized || newE.keypadMode != oldE.keypadMode {
switch newE.keypadMode {
case KeypadMode_Application:
frame.append("\x1B=") // DECKPAM
case KeypadMode_Normal:
frame.append("\x1B>") // DECKPNM
}
}
// has column mode changed? the column mode is out of date.
if !initialized || newE.colMode != oldE.colMode {
switch newE.colMode {
case ColMode_C132:
frame.append("\x1B[?3h") // DECSET
case ColMode_C80:
frame.append("\x1B[?3l") // DECRST
}
}
// has tab stop position changed?
if !initialized || !equalSlice(newE.tabStops, oldE.tabStops) {
// if !initialized || !reflect.DeepEqual(newE.tabStops, oldE.tabStops) {
if len(newE.tabStops) == 0 {
// clear tab stop if necessary
frame.append("\x1B[3g") // TBC
} else {
// save the cursor position
cursorY := frame.cursorY
cursorX := frame.cursorX
// rebuild the tab stop
for _, tabStop := range newE.tabStops {
frame.appendMove(0, tabStop) // CUP: move cursor to the tab stop position
frame.append("\x1BH") // HTS: set current position as tab stop
}
// restore the cursor position
frame.appendMove(cursorY, cursorX)
}
}
// has conformance level changed?
if !initialized || newE.compatLevel != oldE.compatLevel {
switch newE.compatLevel {
case CompatLevel_VT52:
frame.append("\x1B[?2l") // DECSET
case CompatLevel_VT100:
frame.append("\x1B[61\"p") // DECSCL
case CompatLevel_VT400:
frame.append("\x1B[64\"p") // DECSCL
}
}
// has key modifier encoding level changed?
// Note: This depends on real terminal emulator to apply modifyOtherKeys.
if !initialized || newE.modifyOtherKeys != oldE.modifyOtherKeys {
// the possible value for modifyOtherKeys is [0,1,2]
// fmt.Printf("#NewFrame modifyOtherKeys newE=%d, oldE=%d, initialized=%t\n",
// newE.modifyOtherKeys, oldE.modifyOtherKeys, initialized)
frame.append("\x1B[>4;%dm", newE.modifyOtherKeys)
}
// TODO do we need to consider cursor selection area.
return frame.output()
}
// func (d *Display) printFramebufferInfo(oldE, newE *Emulator) {
// util.Log.Debug("replicateContent",
// "columns [E]:", fmt.Sprintf("%3d vs. %3d", newE.nCols, oldE.nCols))
// util.Log.Debug("replicateContent",
// "rows [E]:", fmt.Sprintf("%3d vs. %3d", newE.nRows, oldE.nRows))
// util.Log.Debug("replicateContent",
// "position [E]:", fmt.Sprintf("(%3d,%3d) vs. (%3d,%3d)", newE.posY, newE.posX, oldE.posY, oldE.posX))
// util.Log.Debug("replicateContent",
// "saveLines :", fmt.Sprintf("%3d vs. %3d", newE.cf.saveLines, oldE.cf.saveLines))
// util.Log.Debug("replicateContent",
// "scrollHead :", fmt.Sprintf("%3d vs. %3d", newE.cf.scrollHead, oldE.cf.scrollHead))
// util.Log.Debug("replicateContent",
// "marginTop :", fmt.Sprintf("%3d vs. %3d", newE.cf.marginTop, oldE.cf.marginTop))
// util.Log.Debug("replicateContent",
// "marginBottom :", fmt.Sprintf("%3d vs. %3d", newE.cf.marginBottom, oldE.cf.marginBottom))
// util.Log.Debug("replicateContent",
// "historyRows :", fmt.Sprintf("%3d vs. %3d", newE.cf.historyRows, oldE.cf.historyRows))
// util.Log.Debug("replicateContent",
// "viewOffset :", fmt.Sprintf("%3d vs. %3d", newE.cf.viewOffset, oldE.cf.viewOffset))
// util.Log.Debug("replicateContent",
// "cursor :", fmt.Sprintf("(%3d,%3d) vs. (%3d,%3d)",
// newE.cf.cursor.posY, newE.cf.cursor.posX, oldE.cf.cursor.posY, oldE.cf.cursor.posX))
// util.Log.Debug("replicateContent",
// "damage :", fmt.Sprintf("(%3d,%3d) vs. (%3d,%3d)",
// newE.cf.damage.start, newE.cf.damage.end, oldE.cf.damage.start, oldE.cf.damage.end))
// }
// https://tomscii.sig7.se/zutty/doc/HACKING.html#Frame
func (d *Display) replicateContent(initialized bool, oldE, newE *Emulator, sizeChanged bool,
asbChanged bool, frame *FrameState) {
// d.printFramebufferInfo(oldE, newE)
// mark := "#start"
// prefix := frame.output()
// util.Log.Debug("replicateContent",
// "mark", mark,
// "fs.cursor", fmt.Sprintf("(%02d,%02d)", frame.cursorY, frame.cursorX),
// "altScreenBufferMode", newE.altScreenBufferMode,
// "diff1", prefix)
// util.Log.Debug("replicateContent",
// "newSize", len(newE.cf.cells),
// "oldSize", len(oldE.cf.cells),
// "newMarginBottom", newE.cf.marginBottom,
// "oldMarginBottom", oldE.cf.marginBottom)
var countRows int // stream mode replicate range
// TODO consider remove the scroll head check
// if !newE.altScreenBufferMode && newE.cf.scrollHead > 0 {
if !newE.altScreenBufferMode {
var oldRow []Cell
var newRow []Cell
var blankRow []Cell = make([]Cell, oldE.nCols)
// mark = "stream"
rawY := oldE.cf.getPhysicalRow(oldE.posY) // start row, it's physical row
frameY := oldE.posY // screen row
countRows = calculateRows(oldE, newE)
// util.Log.Debug("replicateContent",
// "oldHead", oldE.cf.scrollHead,
// "newHead", newE.cf.scrollHead,
// "oldY", oldE.posY,
// "newY", newE.posY,
// "oldX", oldE.posX,
// "newX", newE.posX)
// pre := frame.output()
wrap := false
for i := 0; i < countRows; i++ {
// oldRow = blankRow
if countRows == 1 {
oldRow = oldE.cf.getRow(rawY)
} else {
oldRow = blankRow
}
newRow = newE.cf.getRow(rawY)
wrap = d.putRow2(initialized, frame, newE, newRow, frameY, oldRow, wrap)
// util.Log.Debug("replicateContent","old", outputRow(oldRow, rawY, oldE.nCols))
// util.Log.Debug("replicateContent","new", outputRow(newRow, rawY, newE.nCols))
// util.Log.Debug("replicateContent",
// "fs.cursor", fmt.Sprintf("(%02d,%02d)", frame.cursorY, frame.cursorX),
// "rawY", rawY,
// "frameY", frameY,
// "count", i,
// "output", strings.TrimPrefix(frame.output(), pre))
// pre = frame.output()
// wrap around the end of the scrolling area
rawY += 1
if rawY == newE.cf.marginBottom {
rawY = newE.cf.marginTop
}
// rawY = oldE.cf.getPhysicalRow(rawY + 1)
frameY += 1
// if frameY >= newE.GetHeight() {
// frameY = newE.GetHeight()
// frame.cursorY = frameY - 1
// }
}
} else {
// mark = "screen"
d.replicateASB(initialized, oldE, newE, sizeChanged, asbChanged, frame)
}
// util.Log.Debug("replicateContent","diff2", strings.TrimPrefix(frame.output(), prefix))
// util.Log.Debug("replicateContent",
// "mark", mark,
// "fs.cursor", fmt.Sprintf("(%02d,%02d)", frame.cursorY, frame.cursorX),
// "oldHead", oldE.cf.scrollHead,
// "newHead", newE.cf.scrollHead,
// "lastRows", newE.lastRows,
// "countRows", countRows)
}
// replicate alternate screen buffer
func (d *Display) replicateASB(initialized bool, oldE, newE *Emulator, sizeChanged bool,
asbChanged bool, frame *FrameState) {
// util.Log.Debug("replicateASB", "asbChanged", asbChanged, "sizeChanged", sizeChanged)
/*
var resizeScreen []Cell
if asbChanged {
// old is normal screen buffer, new is alternate screen buffer
resizeScreen = make([]Cell, newE.nRows*newE.nCols)
newE.cf.unwrapCellStorage()
} else {
// both screens are alternate screen buffer
resizeScreen = oldE.cf.cells
// newE.cf.unwrapCellStorage()
}
if newE.nCols != oldE.nCols || newE.nRows != oldE.nRows {
util.Log.Warn("replicateASB","size", "changed")
// TODO resize processing
// resize and copy old screen
// we copy the old screen to avoid changing the same part.
// prepare place for the old screen
// oldScreen := make([]Cell, oldE.nCols*oldE.nRows)
// oldE.cf.fullCopyCells(oldScreen)
// prepare place for the resized screen
resizeScreen = make([]Cell, newE.nCols*newE.nRows)
nCopyCols := Min(oldE.nCols, newE.nCols) // minimal column length
nCopyRows := Min(oldE.nRows, newE.nRows) // minimal row length
// copy the old screen to the new place
for pY := 0; pY < nCopyRows; pY++ {
srcStartIdx := pY * nCopyCols
srcEndIdx := srcStartIdx + nCopyCols
dstStartIdx := pY * nCopyCols
copy(resizeScreen[dstStartIdx:], oldE.cf.cells[srcStartIdx:srcEndIdx])
}
// oldScreen = nil
// resize and copy old screen
}
*/
var frameY int
var oldRow []Cell
var newRow []Cell
/*
var linesScrolled int
var scrollHeight int
// shortcut -- has display moved up(text up, window down) by a certain number of lines?
// NOTE: not availble for alternate screen buffer changed.
// if initialized && !asbChanged && !newE.altScreenBufferMode {
if initialized {
for row := 0; row < newE.GetHeight(); row++ {
newRow = getRow(newE, 0)
oldRow = getRowFrom(resizeScreen, row, newE.nCols)
// oldRow = getRow(oldE, row)
if equalRow(newRow, oldRow) {
// fmt.Printf("new screen row 0 is the same as old screen row %d\n", row)
// if row 0, we're looking at ourselves and probably didn't scroll
if row == 0 {
break
}
// found a scroll: text up, window down
linesScrolled = row
scrollHeight = 1
// how big is the region that was scrolled?
for regionHeight := 1; linesScrolled+regionHeight < newE.GetHeight(); regionHeight++ {
newRow = getRow(newE, regionHeight)
oldRow = getRowFrom(resizeScreen, linesScrolled+regionHeight, newE.nCols)
// oldRow = getRow(oldE, regionHeight+linesScrolled)
if equalRow(newRow, oldRow) {
// fmt.Printf("new screen row %d is the same as old screen row %d\n",
// regionHeight, regionHeight+linesScrolled)
scrollHeight = regionHeight + 1
} else {
break
}
}
// fmt.Printf("new screen has %d same rows with the old screen, start from %d\n",
// scrollHeight, linesScrolled)
break
}
}
if scrollHeight > 0 {
frameY = scrollHeight
if linesScrolled > 0 {
// reset the renditions
frame.updateRendition(Renditions{}, true)
topMargin := 0
bottomMargin := topMargin + linesScrolled + scrollHeight - 1
// fmt.Printf("#NewFrame scrollHeight=%2d, linesScrolled=%2d, frameY=%2d, bottomMargin=%2d\n",
// scrollHeight, linesScrolled, frameY, bottomMargin)
// Common case: if we're already on the bottom line and we're scrolling the whole
// creen, just do a CR and LFs.
if scrollHeight+linesScrolled == newE.GetHeight() && frame.cursorY+1 == newE.GetHeight() {
frame.append("\r")
frame.append("\x1B[%dS", linesScrolled)
frame.cursorX = 0
} else {
// set scrolling region
frame.append("\x1B[%d;%dr", topMargin+1, bottomMargin+1)
// go to bottom of scrolling region
frame.cursorY = -1
frame.cursorX = -1
frame.appendSilentMove(bottomMargin, 0)
// scroll text up by <linesScrolled>
frame.append("\x1B[%dS", linesScrolled)
// reset scrolling region
frame.append("\x1B[r")
// invalidate cursor position after unsetting scrolling region
frame.cursorY = -1
frame.cursorX = -1
}
// // Now we need a proper blank row.
// blankRow := make([]Cell, newE.nCols)
// for i := range blankRow {
// // set both contents and renditions
// blankRow[i] = newE.attrs
// }
//
// // do the move in our local new screen
// for i := topMargin; i <= bottomMargin; i++ {
// dstStart := i * newE.nCols
//
// if i+linesScrolled <= bottomMargin {
// copy(resizeScreen[dstStart:], getRow(oldE, linesScrolled+i))
// // copy(resizeScreen[dstStart:], getRowFrom(resizeScreen, linesScrolled+i, newE.nCols))
// } else {
// copy(resizeScreen[dstStart:], blankRow[:])
// // fmt.Printf("row %d is blank\n", i)
// }
// }
//
}
}
}
*/
// util.Log.Debug("replicateASB",
// "newHead", newE.cf.scrollHead,
// "oldHead", oldE.cf.scrollHead,
// "new.cursor", fmt.Sprintf("(%02d,%02d)", newE.posY, newE.posX),
// "old.cursor", fmt.Sprintf("(%02d,%02d)", oldE.posY, oldE.posX),
// "fs.cursor", fmt.Sprintf("(%02d,%02d)", frame.cursorY, frame.cursorX),
// "frameY", frameY)
// pre := frame.output()
/*
// Now update the display, row by row
wrap := false
// for i := 0; i < countRows; i++ {
for ; frameY < newE.GetHeight(); frameY++ {
// for i := 0; i < newE.GetHeight(); i++ {
oldRow = getRowFrom(resizeScreen, frameY, oldE.nCols)
// newRow := getRow(newE, frameY)
newRow = newE.getRowAt(frameY)
wrap = d.putRow2(initialized, frame, newE, newRow, frameY, oldRow, wrap)
// fmt.Printf("#NewFrame frameY=%2d, seq=%q\n", frameY, strings.Replace(b.String(), seq, "", 1))
// seq = b.String()
util.Log.Debug("replicateASB","old", outputRow(oldRow, frameY, oldE.nCols))
util.Log.Debug("replicateASB","new", outputRow(newRow, frameY, newE.nCols))
util.Log.Debug("replicateASB",
"fs.cursor", fmt.Sprintf("(%02d,%02d)", frame.cursorY, frame.cursorX),
"frameY", frameY,
"output", strings.TrimPrefix(frame.output(), pre))
pre = frame.output()
// frameY++
}
*/
var blankRow []Cell = make([]Cell, oldE.nCols)
if asbChanged {
frameY = oldE.nRows - 1
} else {
// frameY = oldE.posY
frameY = 0
}
wrap := false
for i := 0; i < newE.nRows; i++ {
if asbChanged {
oldRow = blankRow
} else {
oldRow = oldE.getRowAt(i)
}
newRow = newE.getRowAt(i)
wrap = d.putRow2(initialized, frame, newE, newRow, frameY, oldRow, wrap)
// util.Log.Debug("replicateASB","old", outputRow(oldRow, frameY, oldE.nCols))
// util.Log.Debug("replicateASB","new", outputRow(newRow, frameY, newE.nCols))
// util.Log.Debug("replicateASB",
// "fs.cursor", fmt.Sprintf("(%02d,%02d)", frame.cursorY, frame.cursorX),
// "frameY", frameY,
// "row", i,
// "output", strings.TrimPrefix(frame.output(), pre))
// pre = frame.output()
frameY++
}
}
/*
func (d *Display) putRow(initialized bool, frame *FrameState,