Debian Codegraph @

src/tui/tcell.go

raw
1 //go:build tcell || windows
2
3 package tui
4
5 import (
6 "os"
7 "regexp"
8 "strings"
9 "time"
10
11 "github.com/gdamore/tcell/v2"
12 "github.com/junegunn/fzf/src/util"
13
14 "github.com/rivo/uniseg"
15 )
16
17 func HasFullscreenRenderer() bool {
18 return true
19 }
20
21 var DefaultBorderShape BorderShape = BorderSharp
22
23 func asTcellColor(color Color) tcell.Color {
24 if color == colDefault {
25 return tcell.ColorDefault
26 }
27
28 value := uint64(tcell.ColorValid) + uint64(color)
29 if color.is24() {
30 value = value | uint64(tcell.ColorIsRGB)
31 }
32 return tcell.Color(value)
33 }
34
35 func (p ColorPair) style() tcell.Style {
36 style := tcell.StyleDefault
37 return style.Foreground(asTcellColor(p.Fg())).Background(asTcellColor(p.Bg()))
38 }
39
40 type TcellWindow struct {
41 color bool
42 windowType WindowType
43 top int
44 left int
45 width int
46 height int
47 normal ColorPair
48 lastX int
49 lastY int
50 moveCursor bool
51 borderStyle BorderStyle
52 uri *string
53 params *string
54 showCursor bool
55 wrapSign string
56 wrapSignWidth int
57 tabstop int
58 }
59
60 func (w *TcellWindow) Top() int {
61 return w.top
62 }
63
64 func (w *TcellWindow) Left() int {
65 return w.left
66 }
67
68 func (w *TcellWindow) Width() int {
69 return w.width
70 }
71
72 func (w *TcellWindow) Height() int {
73 return w.height
74 }
75
76 func (w *TcellWindow) Refresh() {
77 if w.moveCursor {
78 if w.showCursor {
79 _screen.ShowCursor(w.left+w.lastX, w.top+w.lastY)
80 }
81 w.moveCursor = false
82 }
83 w.lastX = 0
84 w.lastY = 0
85 }
86
87 func (w *TcellWindow) FinishFill() {
88 // NO-OP
89 }
90
91 const (
92 Bold Attr = Attr(tcell.AttrBold)
93 Dim = Attr(tcell.AttrDim)
94 Blink = Attr(tcell.AttrBlink)
95 Reverse = Attr(tcell.AttrReverse)
96 Underline = Attr(tcell.AttrUnderline)
97 StrikeThrough = Attr(tcell.AttrStrikeThrough)
98 Italic = Attr(tcell.AttrItalic)
99 )
100
101 func (r *FullscreenRenderer) Bell() {
102 _screen.Beep()
103 }
104
105 func (r *FullscreenRenderer) HideCursor() {
106 r.showCursor = false
107 }
108
109 func (r *FullscreenRenderer) ShowCursor() {
110 r.showCursor = true
111 }
112
113 func (r *FullscreenRenderer) PassThrough(str string) {
114 // No-op
115 // https://github.com/gdamore/tcell/pull/650#issuecomment-1806442846
116 }
117
118 func (r *FullscreenRenderer) Resize(maxHeightFunc func(int) int) {}
119
120 func (r *FullscreenRenderer) DefaultTheme() *ColorTheme {
121 s, e := r.getScreen()
122 if e != nil {
123 return Default16
124 }
125 if s.Colors() >= 256 {
126 return Dark256
127 }
128 return Default16
129 }
130
131 var (
132 _colorToAttribute = []tcell.Color{
133 tcell.ColorBlack,
134 tcell.ColorRed,
135 tcell.ColorGreen,
136 tcell.ColorYellow,
137 tcell.ColorBlue,
138 tcell.ColorDarkMagenta,
139 tcell.ColorLightCyan,
140 tcell.ColorWhite,
141 }
142 )
143
144 func (c Color) Style() tcell.Color {
145 if c <= colDefault {
146 return tcell.ColorDefault
147 } else if c >= colBlack && c <= colWhite {
148 return _colorToAttribute[int(c)]
149 } else {
150 return tcell.Color(c)
151 }
152 }
153
154 // handle the following as private members of FullscreenRenderer instance
155 // they are declared here to prevent introducing tcell library in non-windows builds
156 var (
157 _screen tcell.Screen
158 _prevMouseButton tcell.ButtonMask
159 _initialResize bool = true
160 )
161
162 func (r *FullscreenRenderer) getScreen() (tcell.Screen, error) {
163 if _screen == nil {
164 s, e := tcell.NewScreen()
165 if e != nil {
166 return nil, e
167 }
168 if !r.showCursor {
169 s.HideCursor()
170 }
171 _screen = s
172 }
173 return _screen, nil
174 }
175
176 func (r *FullscreenRenderer) initScreen() error {
177 s, e := r.getScreen()
178 if e != nil {
179 return e
180 }
181 if e = s.Init(); e != nil {
182 return e
183 }
184 s.EnablePaste()
185 if r.mouse {
186 s.EnableMouse()
187 } else {
188 s.DisableMouse()
189 }
190
191 return nil
192 }
193
194 func (r *FullscreenRenderer) Init() error {
195 if os.Getenv("TERM") == "cygwin" {
196 os.Setenv("TERM", "")
197 }
198
199 if err := r.initScreen(); err != nil {
200 return err
201 }
202
203 return nil
204 }
205
206 func (r *FullscreenRenderer) Top() int {
207 return 0
208 }
209
210 func (r *FullscreenRenderer) MaxX() int {
211 ncols, _ := _screen.Size()
212 return int(ncols)
213 }
214
215 func (r *FullscreenRenderer) MaxY() int {
216 _, nlines := _screen.Size()
217 return int(nlines)
218 }
219
220 func (w *TcellWindow) X() int {
221 return w.lastX
222 }
223
224 func (w *TcellWindow) Y() int {
225 return w.lastY
226 }
227
228 func (r *FullscreenRenderer) Clear() {
229 _screen.Sync()
230 _screen.Clear()
231 }
232
233 func (r *FullscreenRenderer) NeedScrollbarRedraw() bool {
234 return true
235 }
236
237 func (r *FullscreenRenderer) ShouldEmitResizeEvent() bool {
238 return true
239 }
240
241 func (r *FullscreenRenderer) Refresh() {
242 // noop
243 }
244
245 // TODO: Pixel width and height not implemented
246 func (r *FullscreenRenderer) Size() TermSize {
247 cols, lines := _screen.Size()
248 return TermSize{lines, cols, 0, 0}
249 }
250
251 func (r *FullscreenRenderer) GetChar(cancellable bool) Event {
252 ev := _screen.PollEvent()
253 switch ev := ev.(type) {
254 case *tcell.EventPaste:
255 if ev.Start() {
256 return Event{BracketedPasteBegin, 0, nil}
257 }
258 return Event{BracketedPasteEnd, 0, nil}
259 case *tcell.EventResize:
260 // Ignore the first resize event
261 // https://github.com/gdamore/tcell/blob/v2.7.0/TUTORIAL.md?plain=1#L18
262 if _initialResize {
263 _initialResize = false
264 return Event{Invalid, 0, nil}
265 }
266 return Event{Resize, 0, nil}
267
268 // process mouse events:
269 case *tcell.EventMouse:
270 // mouse down events have zeroed buttons, so we can't use them
271 // mouse up event consists of two events, 1. (main) event with modifier and other metadata, 2. event with zeroed buttons
272 // so mouse click is three consecutive events, but the first and last are indistinguishable from movement events (with released buttons)
273 // dragging has same structure, it only repeats the middle (main) event appropriately
274 x, y := ev.Position()
275
276 mod := ev.Modifiers()
277 ctrl := (mod & tcell.ModCtrl) > 0
278 alt := (mod & tcell.ModAlt) > 0
279 shift := (mod & tcell.ModShift) > 0
280
281 // since we dont have mouse down events (unlike LightRenderer), we need to track state in prevButton
282 prevButton, button := _prevMouseButton, ev.Buttons()
283 _prevMouseButton = button
284 drag := prevButton == button
285
286 switch {
287 case button&tcell.WheelDown != 0:
288 return Event{Mouse, 0, &MouseEvent{y, x, -1, false, false, false, ctrl, alt, shift}}
289 case button&tcell.WheelUp != 0:
290 return Event{Mouse, 0, &MouseEvent{y, x, +1, false, false, false, ctrl, alt, shift}}
291 case button&tcell.Button1 != 0:
292 double := false
293 if !drag {
294 // all potential double click events put their coordinates in the clicks array
295 // double click event has two conditions, temporal and spatial, the first is checked here
296 now := time.Now()
297 if now.Sub(r.prevDownTime) < doubleClickDuration {
298 r.clicks = append(r.clicks, [2]int{x, y})
299 } else {
300 r.clicks = [][2]int{{x, y}}
301 }
302 r.prevDownTime = now
303
304 // detect double clicks (also check for spatial condition)
305 n := len(r.clicks)
306 double = n > 1 && r.clicks[n-2][0] == r.clicks[n-1][0] && r.clicks[n-2][1] == r.clicks[n-1][1]
307 if double {
308 // make sure two consecutive double clicks require four clicks
309 r.clicks = [][2]int{}
310 }
311 }
312 // fire single or double click event
313 return Event{Mouse, 0, &MouseEvent{y, x, 0, true, !double, double, ctrl, alt, shift}}
314 case button&tcell.Button2 != 0:
315 return Event{Mouse, 0, &MouseEvent{y, x, 0, false, true, false, ctrl, alt, shift}}
316 default:
317 // double and single taps on Windows don't quite work due to
318 // the console acting on the events and not allowing us
319 // to consume them.
320 left := button&tcell.Button1 != 0
321 down := left || button&tcell.Button3 != 0
322 double := false
323
324 // No need to report mouse movement events when no button is pressed
325 if drag {
326 return Event{Invalid, 0, nil}
327 }
328 return Event{Mouse, 0, &MouseEvent{y, x, 0, left, down, double, ctrl, alt, shift}}
329 }
330
331 // process keyboard:
332 case *tcell.EventKey:
333 mods := ev.Modifiers()
334 none := mods == tcell.ModNone
335 alt := (mods & tcell.ModAlt) > 0
336 ctrl := (mods & tcell.ModCtrl) > 0
337 shift := (mods & tcell.ModShift) > 0
338 ctrlAlt := ctrl && alt
339 altShift := alt && shift
340 ctrlShift := ctrl && shift
341 ctrlAltShift := ctrl && alt && shift
342
343 keyfn := func(r rune) Event {
344 if alt {
345 return CtrlAltKey(r)
346 }
347 return EventType(CtrlA.Int() - 'a' + int(r)).AsEvent()
348 }
349 switch ev.Key() {
350 // section 1: Ctrl+(Alt)+[a-z]
351 case tcell.KeyCtrlA:
352 return keyfn('a')
353 case tcell.KeyCtrlB:
354 return keyfn('b')
355 case tcell.KeyCtrlC:
356 return keyfn('c')
357 case tcell.KeyCtrlD:
358 return keyfn('d')
359 case tcell.KeyCtrlE:
360 return keyfn('e')
361 case tcell.KeyCtrlF:
362 return keyfn('f')
363 case tcell.KeyCtrlG:
364 return keyfn('g')
365 case tcell.KeyCtrlH:
366 switch ev.Rune() {
367 case 0:
368 if ctrlAlt {
369 return Event{CtrlAltBackspace, 0, nil}
370 }
371 if ctrl {
372 return Event{CtrlBackspace, 0, nil}
373 }
374 case rune(tcell.KeyCtrlH):
375 switch {
376 case ctrl:
377 return keyfn('h')
378 case alt:
379 return Event{AltBackspace, 0, nil}
380 case none, shift:
381 return Event{Backspace, 0, nil}
382 }
383 }
384 case tcell.KeyCtrlI:
385 return keyfn('i')
386 case tcell.KeyCtrlJ:
387 return keyfn('j')
388 case tcell.KeyCtrlK:
389 return keyfn('k')
390 case tcell.KeyCtrlL:
391 return keyfn('l')
392 case tcell.KeyCtrlM:
393 return keyfn('m')
394 case tcell.KeyCtrlN:
395 return keyfn('n')
396 case tcell.KeyCtrlO:
397 return keyfn('o')
398 case tcell.KeyCtrlP:
399 return keyfn('p')
400 case tcell.KeyCtrlQ:
401 return keyfn('q')
402 case tcell.KeyCtrlR:
403 return keyfn('r')
404 case tcell.KeyCtrlS:
405 return keyfn('s')
406 case tcell.KeyCtrlT:
407 return keyfn('t')
408 case tcell.KeyCtrlU:
409 return keyfn('u')
410 case tcell.KeyCtrlV:
411 return keyfn('v')
412 case tcell.KeyCtrlW:
413 return keyfn('w')
414 case tcell.KeyCtrlX:
415 return keyfn('x')
416 case tcell.KeyCtrlY:
417 return keyfn('y')
418 case tcell.KeyCtrlZ:
419 return keyfn('z')
420 // section 2: Ctrl+[ \]_]
421 case tcell.KeyCtrlSpace:
422 return Event{CtrlSpace, 0, nil}
423 case tcell.KeyCtrlBackslash:
424 return Event{CtrlBackSlash, 0, nil}
425 case tcell.KeyCtrlRightSq:
426 return Event{CtrlRightBracket, 0, nil}
427 case tcell.KeyCtrlCarat:
428 return Event{CtrlCaret, 0, nil}
429 case tcell.KeyCtrlUnderscore:
430 return Event{CtrlSlash, 0, nil}
431 // section 3: (Alt)+Backspace2
432 case tcell.KeyBackspace2:
433 if ctrl {
434 return Event{CtrlBackspace, 0, nil}
435 }
436 if alt {
437 return Event{AltBackspace, 0, nil}
438 }
439 return Event{Backspace, 0, nil}
440
441 // section 4: (Alt+Shift)+Key(Up|Down|Left|Right)
442 case tcell.KeyUp:
443 if ctrlAltShift {
444 return Event{CtrlAltShiftUp, 0, nil}
445 }
446 if ctrlAlt {
447 return Event{CtrlAltUp, 0, nil}
448 }
449 if ctrlShift {
450 return Event{CtrlShiftUp, 0, nil}
451 }
452 if altShift {
453 return Event{AltShiftUp, 0, nil}
454 }
455 if ctrl {
456 return Event{CtrlUp, 0, nil}
457 }
458 if shift {
459 return Event{ShiftUp, 0, nil}
460 }
461 if alt {
462 return Event{AltUp, 0, nil}
463 }
464 return Event{Up, 0, nil}
465 case tcell.KeyDown:
466 if ctrlAltShift {
467 return Event{CtrlAltShiftDown, 0, nil}
468 }
469 if ctrlAlt {
470 return Event{CtrlAltDown, 0, nil}
471 }
472 if ctrlShift {
473 return Event{CtrlShiftDown, 0, nil}
474 }
475 if altShift {
476 return Event{AltShiftDown, 0, nil}
477 }
478 if ctrl {
479 return Event{CtrlDown, 0, nil}
480 }
481 if shift {
482 return Event{ShiftDown, 0, nil}
483 }
484 if alt {
485 return Event{AltDown, 0, nil}
486 }
487 return Event{Down, 0, nil}
488 case tcell.KeyLeft:
489 if ctrlAltShift {
490 return Event{CtrlAltShiftLeft, 0, nil}
491 }
492 if ctrlAlt {
493 return Event{CtrlAltLeft, 0, nil}
494 }
495 if ctrlShift {
496 return Event{CtrlShiftLeft, 0, nil}
497 }
498 if altShift {
499 return Event{AltShiftLeft, 0, nil}
500 }
501 if ctrl {
502 return Event{CtrlLeft, 0, nil}
503 }
504 if shift {
505 return Event{ShiftLeft, 0, nil}
506 }
507 if alt {
508 return Event{AltLeft, 0, nil}
509 }
510 return Event{Left, 0, nil}
511 case tcell.KeyRight:
512 if ctrlAltShift {
513 return Event{CtrlAltShiftRight, 0, nil}
514 }
515 if ctrlAlt {
516 return Event{CtrlAltRight, 0, nil}
517 }
518 if ctrlShift {
519 return Event{CtrlShiftRight, 0, nil}
520 }
521 if altShift {
522 return Event{AltShiftRight, 0, nil}
523 }
524 if ctrl {
525 return Event{CtrlRight, 0, nil}
526 }
527 if shift {
528 return Event{ShiftRight, 0, nil}
529 }
530 if alt {
531 return Event{AltRight, 0, nil}
532 }
533 return Event{Right, 0, nil}
534
535 // section 5: (Insert|Home|Delete|End|PgUp|PgDn|BackTab|F1-F12)
536 case tcell.KeyInsert:
537 return Event{Insert, 0, nil}
538 case tcell.KeyHome:
539 if ctrlAltShift {
540 return Event{CtrlAltShiftHome, 0, nil}
541 }
542 if ctrlAlt {
543 return Event{CtrlAltHome, 0, nil}
544 }
545 if ctrlShift {
546 return Event{CtrlShiftHome, 0, nil}
547 }
548 if altShift {
549 return Event{AltShiftHome, 0, nil}
550 }
551 if ctrl {
552 return Event{CtrlHome, 0, nil}
553 }
554 if shift {
555 return Event{ShiftHome, 0, nil}
556 }
557 if alt {
558 return Event{AltHome, 0, nil}
559 }
560 return Event{Home, 0, nil}
561 case tcell.KeyDelete:
562 if ctrlAltShift {
563 return Event{CtrlAltShiftDelete, 0, nil}
564 }
565 if ctrlAlt {
566 return Event{CtrlAltDelete, 0, nil}
567 }
568 if ctrlShift {
569 return Event{CtrlShiftDelete, 0, nil}
570 }
571 if altShift {
572 return Event{AltShiftDelete, 0, nil}
573 }
574 if ctrl {
575 return Event{CtrlDelete, 0, nil}
576 }
577 if alt {
578 return Event{AltDelete, 0, nil}
579 }
580 if shift {
581 return Event{ShiftDelete, 0, nil}
582 }
583 return Event{Delete, 0, nil}
584 case tcell.KeyEnd:
585 if ctrlAltShift {
586 return Event{CtrlAltShiftEnd, 0, nil}
587 }
588 if ctrlAlt {
589 return Event{CtrlAltEnd, 0, nil}
590 }
591 if ctrlShift {
592 return Event{CtrlShiftEnd, 0, nil}
593 }
594 if altShift {
595 return Event{AltShiftEnd, 0, nil}
596 }
597 if ctrl {
598 return Event{CtrlEnd, 0, nil}
599 }
600 if shift {
601 return Event{ShiftEnd, 0, nil}
602 }
603 if alt {
604 return Event{AltEnd, 0, nil}
605 }
606 return Event{End, 0, nil}
607 case tcell.KeyPgUp:
608 if ctrlAltShift {
609 return Event{CtrlAltShiftPageUp, 0, nil}
610 }
611 if ctrlAlt {
612 return Event{CtrlAltPageUp, 0, nil}
613 }
614 if ctrlShift {
615 return Event{CtrlShiftPageUp, 0, nil}
616 }
617 if altShift {
618 return Event{AltShiftPageUp, 0, nil}
619 }
620 if ctrl {
621 return Event{CtrlPageUp, 0, nil}
622 }
623 if shift {
624 return Event{ShiftPageUp, 0, nil}
625 }
626 if alt {
627 return Event{AltPageUp, 0, nil}
628 }
629 return Event{PageUp, 0, nil}
630 case tcell.KeyPgDn:
631 if ctrlAltShift {
632 return Event{CtrlAltShiftPageDown, 0, nil}
633 }
634 if ctrlAlt {
635 return Event{CtrlAltPageDown, 0, nil}
636 }
637 if ctrlShift {
638 return Event{CtrlShiftPageDown, 0, nil}
639 }
640 if altShift {
641 return Event{AltShiftPageDown, 0, nil}
642 }
643 if ctrl {
644 return Event{CtrlPageDown, 0, nil}
645 }
646 if shift {
647 return Event{ShiftPageDown, 0, nil}
648 }
649 if alt {
650 return Event{AltPageDown, 0, nil}
651 }
652 return Event{PageDown, 0, nil}
653 case tcell.KeyBacktab:
654 return Event{ShiftTab, 0, nil}
655 case tcell.KeyF1:
656 return Event{F1, 0, nil}
657 case tcell.KeyF2:
658 return Event{F2, 0, nil}
659 case tcell.KeyF3:
660 return Event{F3, 0, nil}
661 case tcell.KeyF4:
662 return Event{F4, 0, nil}
663 case tcell.KeyF5:
664 return Event{F5, 0, nil}
665 case tcell.KeyF6:
666 return Event{F6, 0, nil}
667 case tcell.KeyF7:
668 return Event{F7, 0, nil}
669 case tcell.KeyF8:
670 return Event{F8, 0, nil}
671 case tcell.KeyF9:
672 return Event{F9, 0, nil}
673 case tcell.KeyF10:
674 return Event{F10, 0, nil}
675 case tcell.KeyF11:
676 return Event{F11, 0, nil}
677 case tcell.KeyF12:
678 return Event{F12, 0, nil}
679
680 // section 6: (Ctrl+Alt)+'rune'
681 case tcell.KeyRune:
682 r := ev.Rune()
683
684 switch {
685 // translate native key events to ascii control characters
686 case r == ' ' && ctrl:
687 return Event{CtrlSpace, 0, nil}
688 // handle AltGr characters
689 case ctrlAlt:
690 return Event{Rune, r, nil} // dropping modifiers
691 // simple characters (possibly with modifier)
692 case alt:
693 return AltKey(r)
694 default:
695 return Event{Rune, r, nil}
696 }
697
698 // section 7: Esc
699 case tcell.KeyEsc:
700 return Event{Esc, 0, nil}
701 }
702 }
703
704 // section 8: Invalid
705 return Event{Invalid, 0, nil}
706 }
707
708 func (r *FullscreenRenderer) CancelGetChar() {
709 // TODO
710 }
711
712 func (r *FullscreenRenderer) Pause(clear bool) {
713 if clear {
714 _screen.Suspend()
715 }
716 }
717
718 func (r *FullscreenRenderer) Resume(clear bool, sigcont bool) {
719 if clear {
720 _screen.Resume()
721 }
722 }
723
724 func (r *FullscreenRenderer) Close() {
725 _screen.Fini()
726 _screen = nil
727 }
728
729 func (r *FullscreenRenderer) RefreshWindows(windows []Window) {
730 // TODO
731 for _, w := range windows {
732 w.Refresh()
733 }
734 _screen.Show()
735 }
736
737 func (r *FullscreenRenderer) NewWindow(top int, left int, width int, height int, windowType WindowType, borderStyle BorderStyle, erase bool) Window {
738 width = max(0, width)
739 height = max(0, height)
740 normal := ColBorder
741 switch windowType {
742 case WindowList:
743 normal = ColNormal
744 case WindowHeader:
745 normal = ColHeader
746 case WindowFooter:
747 normal = ColFooter
748 case WindowInput:
749 normal = ColInput
750 case WindowPreview:
751 normal = ColPreview
752 }
753 w := &TcellWindow{
754 color: r.theme.Colored,
755 windowType: windowType,
756 top: top,
757 left: left,
758 width: width,
759 height: height,
760 normal: normal,
761 borderStyle: borderStyle,
762 showCursor: r.showCursor,
763 tabstop: r.tabstop}
764 w.Erase()
765 return w
766 }
767
768 func fill(x, y, w, h int, n ColorPair, r rune) {
769 for ly := 0; ly <= h; ly++ {
770 for lx := 0; lx <= w; lx++ {
771 _screen.SetContent(x+lx, y+ly, r, nil, n.style())
772 }
773 }
774 }
775
776 func (w *TcellWindow) Erase() {
777 fill(w.left, w.top, w.width-1, w.height-1, w.normal, ' ')
778 w.drawBorder(false)
779 }
780
781 func (w *TcellWindow) EraseMaybe() bool {
782 w.Erase()
783 return true
784 }
785
786 func (w *TcellWindow) SetWrapSign(sign string, width int) {
787 w.wrapSign = sign
788 w.wrapSignWidth = width
789 }
790
791 func (w *TcellWindow) EncloseX(x int) bool {
792 return x >= w.left && x < (w.left+w.width)
793 }
794
795 func (w *TcellWindow) EncloseY(y int) bool {
796 return y >= w.top && y < (w.top+w.height)
797 }
798
799 func (w *TcellWindow) Enclose(y int, x int) bool {
800 return w.EncloseX(x) && w.EncloseY(y)
801 }
802
803 func (w *TcellWindow) Move(y int, x int) {
804 w.lastX = x
805 w.lastY = y
806 w.moveCursor = true
807 }
808
809 func (w *TcellWindow) MoveAndClear(y int, x int) {
810 w.Move(y, x)
811 for i := w.lastX; i < w.width; i++ {
812 _screen.SetContent(i+w.left, w.lastY+w.top, rune(' '), nil, w.normal.style())
813 }
814 w.lastX = x
815 }
816
817 func (w *TcellWindow) Print(text string) {
818 w.printString(text, w.normal)
819 }
820
821 func (w *TcellWindow) withUrl(style tcell.Style) tcell.Style {
822 if w.uri != nil {
823 style = style.Url(*w.uri)
824 if md := regexp.MustCompile(`id=([^:]+)`).FindStringSubmatch(*w.params); len(md) > 1 {
825 style = style.UrlId(md[1])
826 }
827 }
828 return style
829 }
830
831 func underlineStyleFromAttr(a Attr) tcell.UnderlineStyle {
832 switch a.UnderlineStyle() {
833 case UlStyleDouble:
834 return tcell.UnderlineStyleDouble
835 case UlStyleCurly:
836 return tcell.UnderlineStyleCurly
837 case UlStyleDotted:
838 return tcell.UnderlineStyleDotted
839 case UlStyleDashed:
840 return tcell.UnderlineStyleDashed
841 default:
842 return tcell.UnderlineStyleSolid
843 }
844 }
845
846 func (w *TcellWindow) printString(text string, pair ColorPair) {
847 lx := 0
848 a := pair.Attr()
849
850 style := pair.style()
851 if a&AttrClear == 0 {
852 style = style.
853 Reverse(a&Attr(tcell.AttrReverse) != 0).
854 StrikeThrough(a&Attr(tcell.AttrStrikeThrough) != 0).
855 Italic(a&Attr(tcell.AttrItalic) != 0).
856 Blink(a&Attr(tcell.AttrBlink) != 0).
857 Dim(a&Attr(tcell.AttrDim) != 0)
858 if a&Attr(tcell.AttrUnderline) != 0 {
859 style = style.Underline(underlineStyleFromAttr(a))
860 if pair.Ul() != colDefault {
861 style = style.Underline(asTcellColor(pair.Ul()))
862 }
863 } else {
864 style = style.Underline(false)
865 }
866 }
867 style = w.withUrl(style)
868
869 gr := uniseg.NewGraphemes(text)
870 for gr.Next() {
871 st := style
872 rs := gr.Runes()
873
874 if len(rs) == 1 {
875 r := rs[0]
876 if r == '\r' {
877 st = style.Dim(true)
878 rs[0] = '␍'
879 } else if r == '\n' {
880 st = style.Dim(true)
881 rs[0] = '␊'
882 } else if r < rune(' ') { // ignore control characters
883 continue
884 }
885 }
886 var xPos = w.left + w.lastX + lx
887 var yPos = w.top + w.lastY
888 if xPos < (w.left+w.width) && yPos < (w.top+w.height) {
889 _screen.SetContent(xPos, yPos, rs[0], rs[1:], st)
890 }
891 lx += util.StringWidth(string(rs))
892 }
893 w.lastX += lx
894 }
895
896 func (w *TcellWindow) CPrint(pair ColorPair, text string) {
897 w.printString(text, pair)
898 }
899
900 func (w *TcellWindow) pairStyle(pair ColorPair) tcell.Style {
901 a := pair.Attr()
902 var style tcell.Style
903 if w.color {
904 style = pair.style()
905 } else {
906 style = w.normal.style()
907 }
908 style = style.
909 Blink(a&Attr(tcell.AttrBlink) != 0).
910 Bold(a&Attr(tcell.AttrBold) != 0 || a&BoldForce != 0).
911 Dim(a&Attr(tcell.AttrDim) != 0).
912 Reverse(a&Attr(tcell.AttrReverse) != 0).
913 StrikeThrough(a&Attr(tcell.AttrStrikeThrough) != 0).
914 Italic(a&Attr(tcell.AttrItalic) != 0)
915 if a&Attr(tcell.AttrUnderline) != 0 {
916 style = style.Underline(underlineStyleFromAttr(a))
917 if pair.Ul() != colDefault {
918 style = style.Underline(asTcellColor(pair.Ul()))
919 }
920 } else {
921 style = style.Underline(false)
922 }
923 return w.withUrl(style)
924 }
925
926 func (w *TcellWindow) renderGraphemes(text string, style tcell.Style) {
927 gr := uniseg.NewGraphemes(text)
928 for gr.Next() {
929 st := style
930 rs := gr.Runes()
931 if len(rs) == 1 && rs[0] == '\r' {
932 st = style.Dim(true)
933 rs[0] = '␍'
934 }
935
936 xPos := w.left + w.lastX
937 yPos := w.top + w.lastY
938 if xPos < (w.left+w.width) && yPos < (w.top+w.height) {
939 _screen.SetContent(xPos, yPos, rs[0], rs[1:], st)
940 }
941 w.lastX += util.StringWidth(string(rs))
942 }
943 }
944
945 func (w *TcellWindow) renderWrapSign(style tcell.Style) {
946 sign := w.wrapSign
947 if w.wrapSignWidth > w.width {
948 runes, _ := util.Truncate(sign, w.width)
949 sign = string(runes)
950 }
951 gr := uniseg.NewGraphemes(sign)
952 for gr.Next() {
953 rs := gr.Runes()
954 _screen.SetContent(w.left+w.lastX, w.top+w.lastY, rs[0], rs[1:], style.Dim(true))
955 w.lastX += uniseg.StringWidth(string(rs))
956 }
957 }
958
959 func (w *TcellWindow) fillString(text string, pair ColorPair) FillReturn {
960 style := w.pairStyle(pair)
961
962 for i, segment := range strings.Split(text, "\n") {
963 for j, wl := range WrapLine(segment, w.lastX, w.width, w.tabstop, w.wrapSignWidth) {
964 if i > 0 || j > 0 {
965 w.lastY++
966 if w.lastY >= w.height {
967 return FillSuspend
968 }
969 w.lastX = 0
970 if j > 0 {
971 w.renderWrapSign(style)
972 }
973 }
974 if w.lastX < w.width {
975 w.renderGraphemes(wl.Text, style)
976 }
977 }
978 }
979 if w.lastX >= w.width {
980 w.lastY++
981 w.lastX = 0
982 return FillNextLine
983 }
984
985 return FillContinue
986 }
987
988 func (w *TcellWindow) Fill(str string) FillReturn {
989 return w.fillString(str, w.normal)
990 }
991
992 func (w *TcellWindow) LinkBegin(uri string, params string) {
993 w.uri = &uri
994 w.params = &params
995 }
996
997 func (w *TcellWindow) LinkEnd() {
998 w.uri = nil
999 w.params = nil
1000 }
1001
1002 func (w *TcellWindow) CFill(fg Color, bg Color, ul Color, a Attr, str string) FillReturn {
1003 if fg == colDefault {
1004 fg = w.normal.Fg()
1005 }
1006 if bg == colDefault {
1007 bg = w.normal.Bg()
1008 }
1009 return w.fillString(str, NewColorPair(fg, bg, a).WithUl(ul))
1010 }
1011
1012 func (w *TcellWindow) DrawBorder() {
1013 w.drawBorder(false)
1014 }
1015
1016 func (w *TcellWindow) DrawHBorder() {
1017 w.drawBorder(true)
1018 }
1019
1020 // borderStyleFor returns the tcell.Style used to draw borders for `wt`, honoring
1021 // whether the window is rendering with colors.
1022 func (w *TcellWindow) borderStyleFor(wt WindowType) tcell.Style {
1023 if !w.color {
1024 return w.normal.style()
1025 }
1026 return BorderColor(wt).style()
1027 }
1028
1029 // drawHLine fills row `y` with `line` between optional left/right caps.
1030 // A zero rune means "no cap"; caps are placed at the very edges of `w`.
1031 // tcell has an issue displaying two overlapping wide runes, so the line
1032 // stops before the cap position rather than overpainting.
1033 func (w *TcellWindow) drawHLine(y int, line, leftCap, rightCap rune, style tcell.Style) {
1034 left := w.left
1035 right := left + w.width
1036 hw := runeWidth(line)
1037 lw := 0
1038 rw := 0
1039 if leftCap != 0 {
1040 lw = runeWidth(leftCap)
1041 }
1042 if rightCap != 0 {
1043 rw = runeWidth(rightCap)
1044 }
1045 for x := left + lw; x <= right-rw-hw; x += hw {
1046 _screen.SetContent(x, y, line, nil, style)
1047 }
1048 if leftCap != 0 {
1049 _screen.SetContent(left, y, leftCap, nil, style)
1050 }
1051 if rightCap != 0 {
1052 _screen.SetContent(right-rw, y, rightCap, nil, style)
1053 }
1054 }
1055
1056 func (w *TcellWindow) DrawHSeparator(row int, windowType WindowType, useBottom bool) {
1057 if w.height == 0 {
1058 return
1059 }
1060 shape := w.borderStyle.shape
1061 if shape == BorderNone {
1062 return
1063 }
1064 style := w.borderStyleFor(windowType)
1065 line := w.borderStyle.top
1066 if useBottom {
1067 line = w.borderStyle.bottom
1068 }
1069 var leftCap, rightCap rune
1070 if shape.HasLeft() {
1071 leftCap = w.borderStyle.leftMid
1072 }
1073 if shape.HasRight() {
1074 rightCap = w.borderStyle.rightMid
1075 }
1076 w.drawHLine(w.top+row, line, leftCap, rightCap, style)
1077 }
1078
1079 func (w *TcellWindow) PaintSectionFrame(topContent, bottomContent int, windowType WindowType, edge SectionEdge) {
1080 if w.height == 0 {
1081 return
1082 }
1083 shape := w.borderStyle.shape
1084 if shape == BorderNone {
1085 return
1086 }
1087 style := w.borderStyleFor(windowType)
1088 left := w.left
1089 right := left + w.width
1090 hasLeft := shape.HasLeft()
1091 hasRight := shape.HasRight()
1092 leftW := runeWidth(w.borderStyle.left)
1093 rightW := runeWidth(w.borderStyle.right)
1094 // Content rows: overpaint the left and right verticals (+ their 1-char margin) in
1095 // the section's color. Inner margin stays at whatever bg the sub-window set.
1096 for row := topContent; row <= bottomContent; row++ {
1097 y := w.top + row
1098 if hasLeft {
1099 _screen.SetContent(left, y, w.borderStyle.left, nil, style)
1100 _screen.SetContent(left+leftW, y, ' ', nil, style)
1101 }
1102 if hasRight {
1103 _screen.SetContent(right-rightW-1, y, ' ', nil, style)
1104 _screen.SetContent(right-rightW, y, w.borderStyle.right, nil, style)
1105 }
1106 }
1107 if edge == SectionEdgeTop && shape.HasTop() {
1108 var leftCap, rightCap rune
1109 if hasLeft {
1110 leftCap = w.borderStyle.topLeft
1111 }
1112 if hasRight {
1113 rightCap = w.borderStyle.topRight
1114 }
1115 w.drawHLine(w.top, w.borderStyle.top, leftCap, rightCap, style)
1116 }
1117 if edge == SectionEdgeBottom && shape.HasBottom() {
1118 var leftCap, rightCap rune
1119 if hasLeft {
1120 leftCap = w.borderStyle.bottomLeft
1121 }
1122 if hasRight {
1123 rightCap = w.borderStyle.bottomRight
1124 }
1125 w.drawHLine(w.top+w.height-1, w.borderStyle.bottom, leftCap, rightCap, style)
1126 }
1127 }
1128
1129 func (w *TcellWindow) drawBorder(onlyHorizontal bool) {
1130 if w.height == 0 {
1131 return
1132 }
1133 shape := w.borderStyle.shape
1134 if shape == BorderNone {
1135 return
1136 }
1137
1138 left := w.left
1139 right := left + w.width
1140 top := w.top
1141 bot := top + w.height
1142
1143 style := w.borderStyleFor(w.windowType)
1144
1145 hasLeft := shape.HasLeft()
1146 hasRight := shape.HasRight()
1147
1148 if shape.HasTop() {
1149 var leftCap, rightCap rune
1150 if hasLeft {
1151 leftCap = w.borderStyle.topLeft
1152 }
1153 if hasRight {
1154 rightCap = w.borderStyle.topRight
1155 }
1156 w.drawHLine(top, w.borderStyle.top, leftCap, rightCap, style)
1157 }
1158 if shape.HasBottom() {
1159 var leftCap, rightCap rune
1160 if hasLeft {
1161 leftCap = w.borderStyle.bottomLeft
1162 }
1163 if hasRight {
1164 rightCap = w.borderStyle.bottomRight
1165 }
1166 w.drawHLine(bot-1, w.borderStyle.bottom, leftCap, rightCap, style)
1167 }
1168 if !onlyHorizontal {
1169 vw := runeWidth(w.borderStyle.right)
1170 for y := top; y < bot; y++ {
1171 // Corner rows are already painted by drawHLine above / below.
1172 if (y == top && shape.HasTop()) || (y == bot-1 && shape.HasBottom()) {
1173 continue
1174 }
1175 if hasLeft {
1176 _screen.SetContent(left, y, w.borderStyle.left, nil, style)
1177 }
1178 if hasRight {
1179 _screen.SetContent(right-vw, y, w.borderStyle.right, nil, style)
1180 }
1181 }
1182 }
1183 }