-
-
Notifications
You must be signed in to change notification settings - Fork 177
/
tea.go
201 lines (173 loc) · 3.72 KB
/
tea.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
package utils
import (
"bytes"
"context"
"fmt"
"os"
"strings"
"github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/reflow/wrap"
"golang.org/x/term"
)
func NewProgram(model tea.Model, opts ...tea.ProgramOption) Program {
var p Program
if term.IsTerminal(int(os.Stdin.Fd())) {
p = tea.NewProgram(model, opts...)
} else {
p = newFakeProgram(model)
}
return p
}
// An interface describing the parts of BubbleTea's Program that we actually use.
type Program interface {
Start() error
Send(msg tea.Msg)
Quit()
}
func newFakeProgram(model tea.Model) *fakeProgram {
p := &fakeProgram{
model: model,
}
return p
}
// A dumb text implementation of BubbleTea's Program that allows
// for output to be piped to another program.
type fakeProgram struct {
model tea.Model
}
func (p *fakeProgram) Start() error {
initCmd := p.model.Init()
message := initCmd()
if message != nil {
p.model.Update(message)
}
return nil
}
func (p *fakeProgram) Send(msg tea.Msg) {
switch msg := msg.(type) {
case StatusMsg:
fmt.Println(msg)
case PsqlMsg:
if msg != nil {
fmt.Println(*msg)
}
}
_, cmd := p.model.Update(msg)
if cmd != nil {
cmd()
}
}
func (p *fakeProgram) Quit() {
p.Send(tea.Quit())
}
type (
StatusMsg string
ProgressMsg *float64
PsqlMsg *string
)
type StatusWriter struct {
Program
}
func (t StatusWriter) Write(p []byte) (int, error) {
trimmed := bytes.TrimRight(p, "\n")
t.Send(StatusMsg(trimmed))
return len(p), nil
}
func RunProgram(ctx context.Context, f func(p Program, ctx context.Context) error) error {
ctx, cancel := context.WithCancel(ctx)
p := NewProgram(logModel{
cancel: cancel,
spinner: spinner.New(
spinner.WithSpinner(spinner.Dot),
spinner.WithStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("205"))),
),
})
errCh := make(chan error, 1)
go func() {
errCh <- f(p, ctx)
p.Quit()
}()
if err := p.Start(); err != nil {
return err
}
return <-errCh
}
type logModel struct {
cancel context.CancelFunc
spinner spinner.Model
status string
progress *progress.Model
psqlOutputs []string
width int
}
func (m logModel) Init() tea.Cmd {
return m.spinner.Tick
}
func (m logModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlC:
if m.cancel != nil {
m.cancel()
}
return m, tea.Quit
default:
return m, nil
}
case tea.WindowSizeMsg:
m.width = msg.Width
return m, nil
case spinner.TickMsg:
spinnerModel, cmd := m.spinner.Update(msg)
m.spinner = spinnerModel
return m, cmd
case progress.FrameMsg:
if m.progress == nil {
return m, nil
}
tmp, cmd := m.progress.Update(msg)
progressModel := tmp.(progress.Model)
m.progress = &progressModel
return m, cmd
case StatusMsg:
m.status = string(msg)
return m, nil
case ProgressMsg:
if msg == nil {
m.progress = nil
return m, nil
}
if m.progress == nil {
progressModel := progress.New(progress.WithGradient("#1c1c1c", "#34b27b"))
m.progress = &progressModel
}
return m, m.progress.SetPercent(*msg)
case PsqlMsg:
if msg == nil {
m.psqlOutputs = []string{}
return m, nil
}
m.psqlOutputs = append(m.psqlOutputs, *msg)
if len(m.psqlOutputs) > 5 {
m.psqlOutputs = m.psqlOutputs[1:]
}
return m, nil
default:
return m, nil
}
}
func (m logModel) View() string {
var progress string
if m.progress != nil {
progress = "\n\n" + m.progress.View()
}
var psqlOutputs string
if len(m.psqlOutputs) > 0 {
psqlOutputs = "\n\n" + strings.Join(m.psqlOutputs, "\n")
}
return wrap.String(m.spinner.View()+m.status+progress+psqlOutputs, m.width)
}