-
-
Notifications
You must be signed in to change notification settings - Fork 303
/
tscreen.go
2149 lines (1948 loc) · 51.4 KB
/
tscreen.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 2024 The TCell Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !(js && wasm)
// +build !js !wasm
package tcell
import (
"bytes"
"encoding/base64"
"errors"
"io"
"os"
"strconv"
"strings"
"sync"
"time"
"unicode/utf8"
"golang.org/x/term"
"golang.org/x/text/transform"
"github.com/gdamore/tcell/v2/terminfo"
)
// NewTerminfoScreen returns a Screen that uses the stock TTY interface
// and POSIX terminal control, combined with a terminfo description taken from
// the $TERM environment variable. It returns an error if the terminal
// is not supported for any reason.
//
// For terminals that do not support dynamic resize events, the $LINES
// $COLUMNS environment variables can be set to the actual window size,
// otherwise defaults taken from the terminal database are used.
func NewTerminfoScreen() (Screen, error) {
return NewTerminfoScreenFromTty(nil)
}
// 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)
if e != nil {
return nil, e
}
terminfo.AddTerminfo(ti)
}
return
}
// NewTerminfoScreenFromTtyTerminfo returns a Screen using a custom Tty
// implementation and custom terminfo specification.
// If the passed in tty is nil, then a reasonable default (typically /dev/tty)
// is presumed, at least on UNIX hosts. (Windows hosts will typically fail this
// call altogether.)
// If passed terminfo is nil, then TERM environment variable is queried for
// terminal specification.
func NewTerminfoScreenFromTtyTerminfo(tty Tty, ti *terminfo.Terminfo) (s Screen, e error) {
if ti == nil {
ti, e = LookupTerminfo(os.Getenv("TERM"))
if e != nil {
return
}
}
t := &tScreen{ti: ti, tty: tty}
t.keyexist = make(map[Key]bool)
t.keycodes = make(map[string]*tKeyCode)
if len(ti.Mouse) > 0 {
t.mouse = []byte(ti.Mouse)
}
t.prepareKeys()
t.buildAcsMap()
t.resizeQ = make(chan bool, 1)
t.fallback = make(map[rune]string)
for k, v := range RuneFallbacks {
t.fallback[k] = v
}
return &baseScreen{screenImpl: t}, nil
}
// NewTerminfoScreenFromTty returns a Screen using a custom Tty implementation.
// If the passed in tty is nil, then a reasonable default (typically /dev/tty)
// is presumed, at least on UNIX hosts. (Windows hosts will typically fail this
// call altogether.)
func NewTerminfoScreenFromTty(tty Tty) (Screen, error) {
return NewTerminfoScreenFromTtyTerminfo(tty, nil)
}
// tKeyCode represents a combination of a key code and modifiers.
type tKeyCode struct {
key Key
mod ModMask
}
// tScreen represents a screen backed by a terminfo implementation.
type tScreen struct {
ti *terminfo.Terminfo
tty Tty
h int
w int
fini bool
cells CellBuffer
buffering bool // true if we are collecting writes to buf instead of sending directly to out
buf bytes.Buffer
curstyle Style
style Style
resizeQ chan bool
quit chan struct{}
keyexist map[Key]bool
keycodes map[string]*tKeyCode
keychan chan []byte
keytimer *time.Timer
keyexpire time.Time
cx int
cy int
mouse []byte
clear bool
cursorx int
cursory int
acs map[rune]string
charset string
encoder transform.Transformer
decoder transform.Transformer
fallback map[rune]string
colors map[Color]Color
palette []Color
truecolor bool
escaped bool
buttondn bool
finiOnce sync.Once
enablePaste string
disablePaste string
enterUrl string
exitUrl string
setWinSize string
enableFocus string
disableFocus string
doubleUnder string
curlyUnder string
dottedUnder string
dashedUnder string
underColor string
underRGB string
underFg string
cursorStyles map[CursorStyle]string
cursorStyle CursorStyle
cursorColor Color
cursorRGB string
cursorFg string
saved *term.State
stopQ chan struct{}
eventQ chan Event
running bool
wg sync.WaitGroup
mouseFlags MouseFlags
pasteEnabled bool
focusEnabled bool
setTitle string
saveTitle string
restoreTitle string
title string
setClipboard string
sync.Mutex
}
func (t *tScreen) Init() error {
if e := t.initialize(); e != nil {
return e
}
t.keychan = make(chan []byte, 10)
t.keytimer = time.NewTimer(time.Millisecond * 50)
t.charset = "UTF-8"
t.charset = getCharset()
if enc := GetEncoding(t.charset); enc != nil {
t.encoder = enc.NewEncoder()
t.decoder = enc.NewDecoder()
} else {
return ErrNoCharset
}
ti := t.ti
// environment overrides
w := ti.Columns
h := ti.Lines
if i, _ := strconv.Atoi(os.Getenv("LINES")); i != 0 {
h = i
}
if i, _ := strconv.Atoi(os.Getenv("COLUMNS")); i != 0 {
w = i
}
if t.ti.SetFgBgRGB != "" || t.ti.SetFgRGB != "" || t.ti.SetBgRGB != "" {
t.truecolor = true
}
// A user who wants to have his themes honored can
// set this environment variable.
if os.Getenv("TCELL_TRUECOLOR") == "disable" {
t.truecolor = false
}
nColors := t.nColors()
if nColors > 256 {
nColors = 256 // clip to reasonable limits
}
t.colors = make(map[Color]Color, nColors)
t.palette = make([]Color, nColors)
for i := 0; i < nColors; i++ {
t.palette[i] = Color(i) | ColorValid
// identity map for our builtin colors
t.colors[Color(i)|ColorValid] = Color(i) | ColorValid
}
t.quit = make(chan struct{})
t.eventQ = make(chan Event, 10)
t.Lock()
t.cx = -1
t.cy = -1
t.style = StyleDefault
t.cells.Resize(w, h)
t.cursorx = -1
t.cursory = -1
t.resize()
t.Unlock()
if err := t.engage(); err != nil {
return err
}
return nil
}
func (t *tScreen) prepareKeyMod(key Key, mod ModMask, val string) {
if val != "" {
// Do not override codes that already exist
if _, exist := t.keycodes[val]; !exist {
t.keyexist[key] = true
t.keycodes[val] = &tKeyCode{key: key, mod: mod}
}
}
}
func (t *tScreen) prepareKeyModReplace(key Key, replace Key, mod ModMask, val string) {
if val != "" {
// Do not override codes that already exist
if old, exist := t.keycodes[val]; !exist || old.key == replace {
t.keyexist[key] = true
t.keycodes[val] = &tKeyCode{key: key, mod: mod}
}
}
}
func (t *tScreen) prepareKeyModXTerm(key Key, val string) {
if strings.HasPrefix(val, "\x1b[") && strings.HasSuffix(val, "~") {
// Drop the trailing ~
val = val[:len(val)-1]
// These suffixes are calculated assuming Xterm style modifier suffixes.
// Please see https://invisible-island.net/xterm/ctlseqs/ctlseqs.pdf for
// more information (specifically "PC-Style Function Keys").
t.prepareKeyModReplace(key, key+12, ModShift, val+";2~")
t.prepareKeyModReplace(key, key+48, ModAlt, val+";3~")
t.prepareKeyModReplace(key, key+60, ModAlt|ModShift, val+";4~")
t.prepareKeyModReplace(key, key+24, ModCtrl, val+";5~")
t.prepareKeyModReplace(key, key+36, ModCtrl|ModShift, val+";6~")
t.prepareKeyMod(key, ModAlt|ModCtrl, val+";7~")
t.prepareKeyMod(key, ModShift|ModAlt|ModCtrl, val+";8~")
t.prepareKeyMod(key, ModMeta, val+";9~")
t.prepareKeyMod(key, ModMeta|ModShift, val+";10~")
t.prepareKeyMod(key, ModMeta|ModAlt, val+";11~")
t.prepareKeyMod(key, ModMeta|ModAlt|ModShift, val+";12~")
t.prepareKeyMod(key, ModMeta|ModCtrl, val+";13~")
t.prepareKeyMod(key, ModMeta|ModCtrl|ModShift, val+";14~")
t.prepareKeyMod(key, ModMeta|ModCtrl|ModAlt, val+";15~")
t.prepareKeyMod(key, ModMeta|ModCtrl|ModAlt|ModShift, val+";16~")
} else if strings.HasPrefix(val, "\x1bO") && len(val) == 3 {
val = val[2:]
t.prepareKeyModReplace(key, key+12, ModShift, "\x1b[1;2"+val)
t.prepareKeyModReplace(key, key+48, ModAlt, "\x1b[1;3"+val)
t.prepareKeyModReplace(key, key+24, ModCtrl, "\x1b[1;5"+val)
t.prepareKeyModReplace(key, key+36, ModCtrl|ModShift, "\x1b[1;6"+val)
t.prepareKeyModReplace(key, key+60, ModAlt|ModShift, "\x1b[1;4"+val)
t.prepareKeyMod(key, ModAlt|ModCtrl, "\x1b[1;7"+val)
t.prepareKeyMod(key, ModShift|ModAlt|ModCtrl, "\x1b[1;8"+val)
t.prepareKeyMod(key, ModMeta, "\x1b[1;9"+val)
t.prepareKeyMod(key, ModMeta|ModShift, "\x1b[1;10"+val)
t.prepareKeyMod(key, ModMeta|ModAlt, "\x1b[1;11"+val)
t.prepareKeyMod(key, ModMeta|ModAlt|ModShift, "\x1b[1;12"+val)
t.prepareKeyMod(key, ModMeta|ModCtrl, "\x1b[1;13"+val)
t.prepareKeyMod(key, ModMeta|ModCtrl|ModShift, "\x1b[1;14"+val)
t.prepareKeyMod(key, ModMeta|ModCtrl|ModAlt, "\x1b[1;15"+val)
t.prepareKeyMod(key, ModMeta|ModCtrl|ModAlt|ModShift, "\x1b[1;16"+val)
}
}
func (t *tScreen) prepareXtermModifiers() {
if t.ti.Modifiers != terminfo.ModifiersXTerm {
return
}
t.prepareKeyModXTerm(KeyRight, t.ti.KeyRight)
t.prepareKeyModXTerm(KeyLeft, t.ti.KeyLeft)
t.prepareKeyModXTerm(KeyUp, t.ti.KeyUp)
t.prepareKeyModXTerm(KeyDown, t.ti.KeyDown)
t.prepareKeyModXTerm(KeyInsert, t.ti.KeyInsert)
t.prepareKeyModXTerm(KeyDelete, t.ti.KeyDelete)
t.prepareKeyModXTerm(KeyPgUp, t.ti.KeyPgUp)
t.prepareKeyModXTerm(KeyPgDn, t.ti.KeyPgDn)
t.prepareKeyModXTerm(KeyHome, t.ti.KeyHome)
t.prepareKeyModXTerm(KeyEnd, t.ti.KeyEnd)
t.prepareKeyModXTerm(KeyF1, t.ti.KeyF1)
t.prepareKeyModXTerm(KeyF2, t.ti.KeyF2)
t.prepareKeyModXTerm(KeyF3, t.ti.KeyF3)
t.prepareKeyModXTerm(KeyF4, t.ti.KeyF4)
t.prepareKeyModXTerm(KeyF5, t.ti.KeyF5)
t.prepareKeyModXTerm(KeyF6, t.ti.KeyF6)
t.prepareKeyModXTerm(KeyF7, t.ti.KeyF7)
t.prepareKeyModXTerm(KeyF8, t.ti.KeyF8)
t.prepareKeyModXTerm(KeyF9, t.ti.KeyF9)
t.prepareKeyModXTerm(KeyF10, t.ti.KeyF10)
t.prepareKeyModXTerm(KeyF11, t.ti.KeyF11)
t.prepareKeyModXTerm(KeyF12, t.ti.KeyF12)
}
func (t *tScreen) prepareBracketedPaste() {
// Another workaround for lack of reporting in terminfo.
// We assume if the terminal has a mouse entry, that it
// offers bracketed paste. But we allow specific overrides
// via our terminal database.
if t.ti.EnablePaste != "" {
t.enablePaste = t.ti.EnablePaste
t.disablePaste = t.ti.DisablePaste
t.prepareKey(keyPasteStart, t.ti.PasteStart)
t.prepareKey(keyPasteEnd, t.ti.PasteEnd)
} else if t.ti.Mouse != "" || t.ti.XTermLike {
t.enablePaste = "\x1b[?2004h"
t.disablePaste = "\x1b[?2004l"
t.prepareKey(keyPasteStart, "\x1b[200~")
t.prepareKey(keyPasteEnd, "\x1b[201~")
}
}
func (t *tScreen) prepareUnderlines() {
if t.ti.DoubleUnderline != "" {
t.doubleUnder = t.ti.DoubleUnderline
} else if t.ti.XTermLike {
t.doubleUnder = "\x1b[4:2m"
}
if t.ti.CurlyUnderline != "" {
t.curlyUnder = t.ti.CurlyUnderline
} else if t.ti.XTermLike {
t.curlyUnder = "\x1b[4:3m"
}
if t.ti.DottedUnderline != "" {
t.dottedUnder = t.ti.DottedUnderline
} else if t.ti.XTermLike {
t.dottedUnder = "\x1b[4:4m"
}
if t.ti.DashedUnderline != "" {
t.dashedUnder = t.ti.DashedUnderline
} else if t.ti.XTermLike {
t.dashedUnder = "\x1b[4:5m"
}
// Underline colors. We're not going to rely upon terminfo for this
// Essentially all terminals that support the curly underlines are
// expected to also support coloring them too - which reflects actual
// practice since these were introduced at about the same time.
if t.ti.UnderlineColor != "" {
t.underColor = t.ti.UnderlineColor
} else if t.ti.CurlyUnderline != "" {
t.underColor = "\x1b[58:5:%p1%dm"
}
if t.ti.UnderlineColorRGB != "" {
// An interesting wart here is that in order to facilitate
// using just a single parameter, the Setulc parameter takes
// the 24-bit color as an integer rather than separate bytes.
// This matches the "new" style direct color approach that
// ncurses took, even though everyone else when another way.
t.underRGB = t.ti.UnderlineColorRGB
} else if t.ti.CurlyUnderline != "" {
t.underRGB = "\x1b[58:2::%p1%d:%p2%d:%p3%dm"
}
if t.ti.UnderlineColorReset != "" {
t.underFg = t.ti.UnderlineColorReset
} else if t.ti.CurlyUnderline != "" {
t.underFg = "\x1b[59m"
}
}
func (t *tScreen) prepareExtendedOSC() {
// Linux is a special beast - because it has a mouse entry, but does
// not swallow these OSC commands properly.
if strings.Contains(t.ti.Name, "linux") {
return
}
// More stuff for limits in terminfo. This time we are applying
// the most common OSC (operating system commands). Generally
// terminals that don't understand these will ignore them.
// Again, we condition this based on mouse capabilities.
if t.ti.EnterUrl != "" {
t.enterUrl = t.ti.EnterUrl
t.exitUrl = t.ti.ExitUrl
} else if t.ti.Mouse != "" || t.ti.XTermLike {
t.enterUrl = "\x1b]8;%p2%s;%p1%s\x1b\\"
t.exitUrl = "\x1b]8;;\x1b\\"
}
if t.ti.SetWindowSize != "" {
t.setWinSize = t.ti.SetWindowSize
} else if t.ti.Mouse != "" || t.ti.XTermLike {
t.setWinSize = "\x1b[8;%p1%p2%d;%dt"
}
if t.ti.EnableFocusReporting != "" {
t.enableFocus = t.ti.EnableFocusReporting
} else if t.ti.Mouse != "" || t.ti.XTermLike {
t.enableFocus = "\x1b[?1004h"
}
if t.ti.DisableFocusReporting != "" {
t.disableFocus = t.ti.DisableFocusReporting
} else if t.ti.Mouse != "" || t.ti.XTermLike {
t.disableFocus = "\x1b[?1004l"
}
if t.ti.SetWindowTitle != "" {
t.setTitle = t.ti.SetWindowTitle
} else if t.ti.XTermLike {
t.saveTitle = "\x1b[22;2t"
t.restoreTitle = "\x1b[23;2t"
// this also tries to request that UTF-8 is allowed in the title
t.setTitle = "\x1b[>2t\x1b]2;%p1%s\x1b\\"
}
if t.setClipboard == "" && t.ti.XTermLike {
// this string takes a base64 string and sends it to the clipboard.
// it will also be able to retrieve the clipboard using "?" as the
// sent string, when we support that.
t.setClipboard = "\x1b]52;c;%p1%s\x1b\\"
}
}
func (t *tScreen) prepareCursorStyles() {
// Another workaround for lack of reporting in terminfo.
// We assume if the terminal has a mouse entry, that it
// offers bracketed paste. But we allow specific overrides
// via our terminal database.
if t.ti.CursorDefault != "" {
t.cursorStyles = map[CursorStyle]string{
CursorStyleDefault: t.ti.CursorDefault,
CursorStyleBlinkingBlock: t.ti.CursorBlinkingBlock,
CursorStyleSteadyBlock: t.ti.CursorSteadyBlock,
CursorStyleBlinkingUnderline: t.ti.CursorBlinkingUnderline,
CursorStyleSteadyUnderline: t.ti.CursorSteadyUnderline,
CursorStyleBlinkingBar: t.ti.CursorBlinkingBar,
CursorStyleSteadyBar: t.ti.CursorSteadyBar,
}
} else if t.ti.Mouse != "" || t.ti.XTermLike {
t.cursorStyles = map[CursorStyle]string{
CursorStyleDefault: "\x1b[0 q",
CursorStyleBlinkingBlock: "\x1b[1 q",
CursorStyleSteadyBlock: "\x1b[2 q",
CursorStyleBlinkingUnderline: "\x1b[3 q",
CursorStyleSteadyUnderline: "\x1b[4 q",
CursorStyleBlinkingBar: "\x1b[5 q",
CursorStyleSteadyBar: "\x1b[6 q",
}
}
if t.ti.CursorColorRGB != "" {
// if it was X11 style with just a single %p1%s, then convert
t.cursorRGB = t.ti.CursorColorRGB
}
if t.ti.CursorColorReset != "" {
t.cursorFg = t.ti.CursorColorReset
}
if t.cursorRGB == "" {
t.cursorRGB = "\x1b]12;%p1%s\007"
t.cursorFg = "\x1b]112\007"
}
// convert XTERM style color names to RGB color code. We have no way to do palette colors
t.cursorRGB = strings.Replace(t.cursorRGB, "%p1%s", "#%p1%02x%p2%02x%p3%02x", 1)
}
func (t *tScreen) prepareKey(key Key, val string) {
t.prepareKeyMod(key, ModNone, val)
}
func (t *tScreen) prepareKeys() {
ti := t.ti
if strings.HasPrefix(ti.Name, "xterm") {
// assume its some form of XTerm clone
t.ti.XTermLike = true
ti.XTermLike = true
}
t.prepareKey(KeyBackspace, ti.KeyBackspace)
t.prepareKey(KeyF1, ti.KeyF1)
t.prepareKey(KeyF2, ti.KeyF2)
t.prepareKey(KeyF3, ti.KeyF3)
t.prepareKey(KeyF4, ti.KeyF4)
t.prepareKey(KeyF5, ti.KeyF5)
t.prepareKey(KeyF6, ti.KeyF6)
t.prepareKey(KeyF7, ti.KeyF7)
t.prepareKey(KeyF8, ti.KeyF8)
t.prepareKey(KeyF9, ti.KeyF9)
t.prepareKey(KeyF10, ti.KeyF10)
t.prepareKey(KeyF11, ti.KeyF11)
t.prepareKey(KeyF12, ti.KeyF12)
t.prepareKey(KeyF13, ti.KeyF13)
t.prepareKey(KeyF14, ti.KeyF14)
t.prepareKey(KeyF15, ti.KeyF15)
t.prepareKey(KeyF16, ti.KeyF16)
t.prepareKey(KeyF17, ti.KeyF17)
t.prepareKey(KeyF18, ti.KeyF18)
t.prepareKey(KeyF19, ti.KeyF19)
t.prepareKey(KeyF20, ti.KeyF20)
t.prepareKey(KeyF21, ti.KeyF21)
t.prepareKey(KeyF22, ti.KeyF22)
t.prepareKey(KeyF23, ti.KeyF23)
t.prepareKey(KeyF24, ti.KeyF24)
t.prepareKey(KeyF25, ti.KeyF25)
t.prepareKey(KeyF26, ti.KeyF26)
t.prepareKey(KeyF27, ti.KeyF27)
t.prepareKey(KeyF28, ti.KeyF28)
t.prepareKey(KeyF29, ti.KeyF29)
t.prepareKey(KeyF30, ti.KeyF30)
t.prepareKey(KeyF31, ti.KeyF31)
t.prepareKey(KeyF32, ti.KeyF32)
t.prepareKey(KeyF33, ti.KeyF33)
t.prepareKey(KeyF34, ti.KeyF34)
t.prepareKey(KeyF35, ti.KeyF35)
t.prepareKey(KeyF36, ti.KeyF36)
t.prepareKey(KeyF37, ti.KeyF37)
t.prepareKey(KeyF38, ti.KeyF38)
t.prepareKey(KeyF39, ti.KeyF39)
t.prepareKey(KeyF40, ti.KeyF40)
t.prepareKey(KeyF41, ti.KeyF41)
t.prepareKey(KeyF42, ti.KeyF42)
t.prepareKey(KeyF43, ti.KeyF43)
t.prepareKey(KeyF44, ti.KeyF44)
t.prepareKey(KeyF45, ti.KeyF45)
t.prepareKey(KeyF46, ti.KeyF46)
t.prepareKey(KeyF47, ti.KeyF47)
t.prepareKey(KeyF48, ti.KeyF48)
t.prepareKey(KeyF49, ti.KeyF49)
t.prepareKey(KeyF50, ti.KeyF50)
t.prepareKey(KeyF51, ti.KeyF51)
t.prepareKey(KeyF52, ti.KeyF52)
t.prepareKey(KeyF53, ti.KeyF53)
t.prepareKey(KeyF54, ti.KeyF54)
t.prepareKey(KeyF55, ti.KeyF55)
t.prepareKey(KeyF56, ti.KeyF56)
t.prepareKey(KeyF57, ti.KeyF57)
t.prepareKey(KeyF58, ti.KeyF58)
t.prepareKey(KeyF59, ti.KeyF59)
t.prepareKey(KeyF60, ti.KeyF60)
t.prepareKey(KeyF61, ti.KeyF61)
t.prepareKey(KeyF62, ti.KeyF62)
t.prepareKey(KeyF63, ti.KeyF63)
t.prepareKey(KeyF64, ti.KeyF64)
t.prepareKey(KeyInsert, ti.KeyInsert)
t.prepareKey(KeyDelete, ti.KeyDelete)
t.prepareKey(KeyHome, ti.KeyHome)
t.prepareKey(KeyEnd, ti.KeyEnd)
t.prepareKey(KeyUp, ti.KeyUp)
t.prepareKey(KeyDown, ti.KeyDown)
t.prepareKey(KeyLeft, ti.KeyLeft)
t.prepareKey(KeyRight, ti.KeyRight)
t.prepareKey(KeyPgUp, ti.KeyPgUp)
t.prepareKey(KeyPgDn, ti.KeyPgDn)
t.prepareKey(KeyHelp, ti.KeyHelp)
t.prepareKey(KeyPrint, ti.KeyPrint)
t.prepareKey(KeyCancel, ti.KeyCancel)
t.prepareKey(KeyExit, ti.KeyExit)
t.prepareKey(KeyBacktab, ti.KeyBacktab)
t.prepareKeyMod(KeyRight, ModShift, ti.KeyShfRight)
t.prepareKeyMod(KeyLeft, ModShift, ti.KeyShfLeft)
t.prepareKeyMod(KeyUp, ModShift, ti.KeyShfUp)
t.prepareKeyMod(KeyDown, ModShift, ti.KeyShfDown)
t.prepareKeyMod(KeyHome, ModShift, ti.KeyShfHome)
t.prepareKeyMod(KeyEnd, ModShift, ti.KeyShfEnd)
t.prepareKeyMod(KeyPgUp, ModShift, ti.KeyShfPgUp)
t.prepareKeyMod(KeyPgDn, ModShift, ti.KeyShfPgDn)
t.prepareKeyMod(KeyRight, ModCtrl, ti.KeyCtrlRight)
t.prepareKeyMod(KeyLeft, ModCtrl, ti.KeyCtrlLeft)
t.prepareKeyMod(KeyUp, ModCtrl, ti.KeyCtrlUp)
t.prepareKeyMod(KeyDown, ModCtrl, ti.KeyCtrlDown)
t.prepareKeyMod(KeyHome, ModCtrl, ti.KeyCtrlHome)
t.prepareKeyMod(KeyEnd, ModCtrl, ti.KeyCtrlEnd)
// Sadly, xterm handling of keycodes is somewhat erratic. In
// particular, different codes are sent depending on application
// mode is in use or not, and the entries for many of these are
// simply absent from terminfo on many systems. So we insert
// a number of escape sequences if they are not already used, in
// order to have the widest correct usage. Note that prepareKey
// will not inject codes if the escape sequence is already known.
// We also only do this for terminals that have the application
// mode present.
// Cursor mode
if ti.EnterKeypad != "" {
t.prepareKey(KeyUp, "\x1b[A")
t.prepareKey(KeyDown, "\x1b[B")
t.prepareKey(KeyRight, "\x1b[C")
t.prepareKey(KeyLeft, "\x1b[D")
t.prepareKey(KeyEnd, "\x1b[F")
t.prepareKey(KeyHome, "\x1b[H")
t.prepareKey(KeyDelete, "\x1b[3~")
t.prepareKey(KeyHome, "\x1b[1~")
t.prepareKey(KeyEnd, "\x1b[4~")
t.prepareKey(KeyPgUp, "\x1b[5~")
t.prepareKey(KeyPgDn, "\x1b[6~")
// Application mode
t.prepareKey(KeyUp, "\x1bOA")
t.prepareKey(KeyDown, "\x1bOB")
t.prepareKey(KeyRight, "\x1bOC")
t.prepareKey(KeyLeft, "\x1bOD")
t.prepareKey(KeyHome, "\x1bOH")
}
t.prepareKey(keyPasteStart, ti.PasteStart)
t.prepareKey(keyPasteEnd, ti.PasteEnd)
t.prepareXtermModifiers()
t.prepareBracketedPaste()
t.prepareCursorStyles()
t.prepareUnderlines()
t.prepareExtendedOSC()
outer:
// Add key mappings for control keys.
for i := 0; i < ' '; i++ {
// Do not insert direct key codes for ambiguous keys.
// For example, ESC is used for lots of other keys, so
// when parsing this we don't want to fast path handling
// of it, but instead wait a bit before parsing it as in
// isolation.
for esc := range t.keycodes {
if []byte(esc)[0] == byte(i) {
continue outer
}
}
t.keyexist[Key(i)] = true
mod := ModCtrl
switch Key(i) {
case KeyBS, KeyTAB, KeyESC, KeyCR:
// directly type-able- no control sequence
mod = ModNone
}
t.keycodes[string(rune(i))] = &tKeyCode{key: Key(i), mod: mod}
}
}
func (t *tScreen) Fini() {
t.finiOnce.Do(t.finish)
}
func (t *tScreen) finish() {
close(t.quit)
t.finalize()
}
func (t *tScreen) SetStyle(style Style) {
t.Lock()
if !t.fini {
t.style = style
}
t.Unlock()
}
func (t *tScreen) encodeRune(r rune, buf []byte) []byte {
nb := make([]byte, 6)
ob := make([]byte, 6)
num := utf8.EncodeRune(ob, r)
ob = ob[:num]
dst := 0
var err error
if enc := t.encoder; enc != nil {
enc.Reset()
dst, _, err = enc.Transform(nb, ob, true)
}
if err != nil || dst == 0 || nb[0] == '\x1a' {
// Combining characters are elided
if len(buf) == 0 {
if acs, ok := t.acs[r]; ok {
buf = append(buf, []byte(acs)...)
} else if fb, ok := t.fallback[r]; ok {
buf = append(buf, []byte(fb)...)
} else {
buf = append(buf, '?')
}
}
} else {
buf = append(buf, nb[:dst]...)
}
return buf
}
func (t *tScreen) sendFgBg(fg Color, bg Color, attr AttrMask) AttrMask {
ti := t.ti
if ti.Colors == 0 {
// foreground vs background, we calculate luminance
// and possibly do a reverse video
if !fg.Valid() {
return attr
}
v, ok := t.colors[fg]
if !ok {
v = FindColor(fg, []Color{ColorBlack, ColorWhite})
t.colors[fg] = v
}
switch v {
case ColorWhite:
return attr
case ColorBlack:
return attr ^ AttrReverse
}
}
if fg == ColorReset || bg == ColorReset {
t.TPuts(ti.ResetFgBg)
}
if t.truecolor {
if ti.SetFgBgRGB != "" && fg.IsRGB() && bg.IsRGB() {
r1, g1, b1 := fg.RGB()
r2, g2, b2 := bg.RGB()
t.TPuts(ti.TParm(ti.SetFgBgRGB,
int(r1), int(g1), int(b1),
int(r2), int(g2), int(b2)))
return attr
}
if fg.IsRGB() && ti.SetFgRGB != "" {
r, g, b := fg.RGB()
t.TPuts(ti.TParm(ti.SetFgRGB, int(r), int(g), int(b)))
fg = ColorDefault
}
if bg.IsRGB() && ti.SetBgRGB != "" {
r, g, b := bg.RGB()
t.TPuts(ti.TParm(ti.SetBgRGB,
int(r), int(g), int(b)))
bg = ColorDefault
}
}
if fg.Valid() {
if v, ok := t.colors[fg]; ok {
fg = v
} else {
v = FindColor(fg, t.palette)
t.colors[fg] = v
fg = v
}
}
if bg.Valid() {
if v, ok := t.colors[bg]; ok {
bg = v
} else {
v = FindColor(bg, t.palette)
t.colors[bg] = v
bg = v
}
}
if fg.Valid() && bg.Valid() && ti.SetFgBg != "" {
t.TPuts(ti.TParm(ti.SetFgBg, int(fg&0xff), int(bg&0xff)))
} else {
if fg.Valid() && ti.SetFg != "" {
t.TPuts(ti.TParm(ti.SetFg, int(fg&0xff)))
}
if bg.Valid() && ti.SetBg != "" {
t.TPuts(ti.TParm(ti.SetBg, int(bg&0xff)))
}
}
return attr
}
func (t *tScreen) drawCell(x, y int) int {
ti := t.ti
mainc, combc, style, width := t.cells.GetContent(x, y)
if !t.cells.Dirty(x, y) {
return width
}
if y == t.h-1 && x == t.w-1 && t.ti.AutoMargin && ti.DisableAutoMargin == "" && ti.InsertChar != "" {
// our solution is somewhat goofy.
// we write to the second to the last cell what we want in the last cell, then we
// insert a character at that 2nd to last position to shift the last column into
// place, then we rewrite that 2nd to last cell. Old terminals suck.
t.TPuts(ti.TGoto(x-1, y))
defer func() {
t.TPuts(ti.TGoto(x-1, y))
t.TPuts(ti.InsertChar)
t.cy = y
t.cx = x - 1
t.cells.SetDirty(x-1, y, true)
_ = t.drawCell(x-1, y)
t.TPuts(t.ti.TGoto(0, 0))
t.cy = 0
t.cx = 0
}()
} else if t.cy != y || t.cx != x {
t.TPuts(ti.TGoto(x, y))
t.cx = x
t.cy = y
}
if style == StyleDefault {
style = t.style
}
if style != t.curstyle {
fg, bg, attrs := style.fg, style.bg, style.attrs
t.TPuts(ti.AttrOff)
attrs = t.sendFgBg(fg, bg, attrs)
if attrs&AttrBold != 0 {
t.TPuts(ti.Bold)
}
if us, uc := style.ulStyle, style.ulColor; us != UnderlineStyleNone {
if t.underColor != "" || t.underRGB != "" {
if uc == ColorReset {
t.TPuts(t.underFg)
} else if uc.IsRGB() {
if t.underRGB != "" {
r, g, b := uc.RGB()
t.TPuts(ti.TParm(t.underRGB, int(r), int(g), int(b)))
} else {
if v, ok := t.colors[uc]; ok {
uc = v
} else {
v = FindColor(uc, t.palette)
t.colors[uc] = v
uc = v
}
t.TPuts(ti.TParm(t.underColor, int(uc&0xff)))
}
} else if uc.Valid() {
t.TPuts(ti.TParm(t.underColor, int(uc&0xff)))
}
}
t.TPuts(ti.Underline) // to ensure everyone gets at least a basic underline
switch us {
case UnderlineStyleDouble:
t.TPuts(t.doubleUnder)
case UnderlineStyleCurly:
t.TPuts(t.curlyUnder)
case UnderlineStyleDotted:
t.TPuts(t.dottedUnder)
case UnderlineStyleDashed:
t.TPuts(t.dashedUnder)
}
}
if attrs&AttrReverse != 0 {
t.TPuts(ti.Reverse)
}
if attrs&AttrBlink != 0 {
t.TPuts(ti.Blink)
}
if attrs&AttrDim != 0 {
t.TPuts(ti.Dim)
}
if attrs&AttrItalic != 0 {
t.TPuts(ti.Italic)
}
if attrs&AttrStrikeThrough != 0 {
t.TPuts(ti.StrikeThrough)
}
// URL string can be long, so don't send it unless we really need to
if t.enterUrl != "" && t.curstyle != style {
if style.url != "" {
t.TPuts(ti.TParm(t.enterUrl, style.url, style.urlId))
} else {
t.TPuts(t.exitUrl)
}
}
t.curstyle = style
}
// now emit runes - taking care to not overrun width with a
// wide character, and to ensure that we emit exactly one regular
// character followed up by any residual combing characters
if width < 1 {
width = 1
}
var str string
buf := make([]byte, 0, 6)
buf = t.encodeRune(mainc, buf)
for _, r := range combc {
buf = t.encodeRune(r, buf)
}
str = string(buf)
if width > 1 && str == "?" {
// No FullWidth character support
str = "? "
t.cx = -1
}
if x > t.w-width {
// too wide to fit; emit a single space instead
width = 1
str = " "
}
t.writeString(str)
t.cx += width
t.cells.SetDirty(x, y, false)
if width > 1 {
t.cx = -1
}
return width
}
func (t *tScreen) ShowCursor(x, y int) {
t.Lock()
t.cursorx = x
t.cursory = y
t.Unlock()
}
func (t *tScreen) SetCursor(cs CursorStyle, cc Color) {
t.Lock()
t.cursorStyle = cs
t.cursorColor = cc
t.Unlock()
}
func (t *tScreen) HideCursor() {
t.ShowCursor(-1, -1)
}
func (t *tScreen) showCursor() {
x, y := t.cursorx, t.cursory
w, h := t.cells.Size()
if x < 0 || y < 0 || x >= w || y >= h {
t.hideCursor()
return
}
t.TPuts(t.ti.TGoto(x, y))
t.TPuts(t.ti.ShowCursor)
if t.cursorStyles != nil {
if esc, ok := t.cursorStyles[t.cursorStyle]; ok {
t.TPuts(esc)
}
}
if t.cursorRGB != "" {
if t.cursorColor == ColorReset {
t.TPuts(t.cursorFg)
} else if t.cursorColor.Valid() {
r, g, b := t.cursorColor.RGB()
t.TPuts(t.ti.TParm(t.cursorRGB, int(r), int(g), int(b)))
}
}
t.cx = x
t.cy = y
}
// writeString sends a string to the terminal. The string is sent as-is and
// this function does not expand inline padding indications (of the form
// $<[delay]> where [delay] is msec). In order to have these expanded, use
// TPuts. If the screen is "buffering", the string is collected in a buffer,
// with the intention that the entire buffer be sent to the terminal in one