-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml_viewer.go
110 lines (93 loc) · 2.15 KB
/
xml_viewer.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
package main
import (
"github.com/charmbracelet/bubbles/textarea"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type ShowXMLRecordMsg string
// component to read xml record
type xmlViewer struct {
textarea *textarea.Model
highlight bool
}
func NewXMLViewer() xmlViewer {
focused, blurred := textarea.DefaultStyles()
focused.Base = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(Theme().primary)
blurred.LineNumber = lipgloss.NewStyle().Faint(true)
blurred.Base = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("240"))
focused.LineNumber = lipgloss.NewStyle().Faint(true)
t := textarea.New()
t.BlurredStyle = blurred
t.FocusedStyle = focused
return xmlViewer{textarea: &t, highlight: highlightXML}
}
func (x xmlViewer) Init() tea.Cmd {
return nil
}
func (x xmlViewer) Update(msg tea.Msg) (xmlViewer, tea.Cmd) {
var cmd tea.Cmd
var t textarea.Model
switch m := msg.(type) {
case ShowXMLRecordMsg:
x.textarea.SetValue(string(m))
if x.highlight {
x.Highlight()
}
}
t, cmd = x.textarea.Update(msg)
x.textarea = &t
return x, cmd
}
func (x xmlViewer) View() string {
return x.textarea.View()
}
func (x xmlViewer) Width() int {
return x.textarea.Width()
}
func (x xmlViewer) SetWidth(w int) {
x.textarea.SetWidth(w)
}
func (x xmlViewer) SetHeight(h int) {
x.textarea.SetHeight(h)
}
func (x xmlViewer) Focus() {
x.textarea.Focus()
x.textarea.Cursor.Focus()
x.CursorTop()
}
func (x xmlViewer) CursorTop() {
n := x.textarea.LineCount()
for i := 0; i < n; i++ {
x.textarea.CursorUp()
}
x.textarea.CursorStart()
}
func (x xmlViewer) Blur() {
x.textarea.Blur()
}
func (x xmlViewer) Focused() bool {
return x.textarea.Focused()
}
func (x xmlViewer) Highlight() {
tag := false
buffer := make([]rune, 0)
out := ""
for _, r := range x.textarea.Value() {
if r == '<' {
tag = true
} else if r == '>' {
tag = false
out += lipgloss.NewStyle().Faint(true).Render("<" + string(buffer) + ">")
buffer = buffer[:0]
} else if tag {
buffer = append(buffer, r)
} else {
out += string(r)
}
}
x.textarea.SetValue(out)
}