-
Notifications
You must be signed in to change notification settings - Fork 2
/
termui.go
100 lines (83 loc) · 2.45 KB
/
termui.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package components
import (
"image"
"github.com/gdamore/tcell/v2"
"github.com/gizak/termui/v3"
"github.com/rivo/tview"
)
// TermUIWrapper is a custom tview component that wraps a legacy termui component and draws it.
type TermUIWrapper struct {
*tview.Box
termUIDrawable termui.Drawable
}
// NewTermUIWrapper initializes a new TermUIWrapper.
func NewTermUIWrapper(drawable termui.Drawable) *TermUIWrapper {
return &TermUIWrapper{
Box: tview.NewBox(),
termUIDrawable: drawable,
}
}
// Draw implements the tview.Primitive interface.
func (w *TermUIWrapper) Draw(screen tcell.Screen) {
w.Box.DrawForSubclass(screen, w)
x, y, width, height := w.GetInnerRect()
if width == 0 || height == 0 {
return
}
w.termUIDrawable.SetRect(0, 0, width, height)
buf := termui.NewBuffer(w.termUIDrawable.GetRect())
w.termUIDrawable.Draw(buf)
for i := range width {
for j := range height {
cell := buf.GetCell(image.Point{X: i, Y: j})
style := w.convertStyle(cell.Style)
screen.SetContent(i+x, j+y, cell.Rune, nil, style)
}
}
}
// convertStyle converts termui style to tcell (tview) style.
func (w *TermUIWrapper) convertStyle(style termui.Style) tcell.Style {
fgColor := w.convertColor(style.Fg)
bgColor := w.convertColor(style.Bg)
bold := false
if style.Modifier&termui.ModifierBold != 0 {
bold = true
}
underline := false
if style.Modifier&termui.ModifierUnderline != 0 {
underline = true
}
reverse := false
if style.Modifier&termui.ModifierReverse != 0 {
reverse = true
}
return tcell.StyleDefault.Foreground(fgColor).Background(bgColor).Bold(bold).Underline(underline).Reverse(reverse)
}
// convertColor converts termui color to tcell (tview) color.
func (w *TermUIWrapper) convertColor(color termui.Color) tcell.Color {
switch color {
case termui.ColorClear:
return tcell.ColorDefault
case termui.ColorBlack:
return tcell.ColorBlack
case termui.ColorRed:
return tcell.ColorRed
case termui.ColorGreen:
return tcell.ColorGreen
case termui.ColorYellow:
return tcell.ColorYellow
case termui.ColorBlue:
return tcell.ColorBlue
case termui.ColorMagenta:
return tcell.ColorPurple
case termui.ColorCyan:
return tcell.ColorTeal
case termui.ColorWhite:
return tcell.ColorWhite
default:
return tcell.ColorDefault
}
}