-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathtty.go
More file actions
230 lines (197 loc) · 6.46 KB
/
tty.go
File metadata and controls
230 lines (197 loc) · 6.46 KB
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
package preflight
import (
"context"
"fmt"
"strings"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/ansi"
)
var (
ttyDimStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("240"))
ttyStatusStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FFBA03")).Bold(true)
ttyBorderStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("238"))
ttyFailureStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("9"))
ttySoftFailureStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("11"))
)
type ttyModel struct {
spinner spinner.Model
latest Event
summary *Event
cancelFunc context.CancelFunc
width int
}
func newTTYModel() ttyModel {
s := spinner.New()
s.Spinner = spinner.Dot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("#DE8F0C"))
return ttyModel{spinner: s}
}
func (m ttyModel) Init() tea.Cmd {
return m.spinner.Tick
}
func (m ttyModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "ctrl+c":
if m.cancelFunc != nil {
m.cancelFunc()
}
return m, nil
}
case Event:
switch msg.Type {
case EventOperation:
m.latest = msg
timestamp := ttyDimStyle.Render(msg.Time.Format("15:04:05"))
prefix := timestamp + " "
line := prefix + msg.Title
if msg.Detail != "" {
detail := indentAllLines(msg.Detail, len("15:04:05 "))
line += ":\n" + detail
}
return m, tea.Printf("%s", m.hardwrapLine(line))
case EventBuildStatus:
m.latest = msg
return m, nil
case EventJobFailure:
if msg.Job != nil {
presenter := jobPresenter{pipeline: msg.Pipeline, buildNumber: msg.BuildNumber}
line := timestampPrefix(msg.Time) + presenter.ColoredLine(*msg.Job)
return m, tea.Printf("%s", m.hardwrapLine(line))
}
case EventJobRetryPassed:
if msg.Job != nil {
presenter := jobPresenter{pipeline: msg.Pipeline, buildNumber: msg.BuildNumber}
line := timestampPrefix(msg.Time) + presenter.ColoredRetryPassedLine(*msg.Job)
return m, tea.Printf("%s", m.hardwrapLine(line))
}
case EventBuildSummary:
// Print the summary via Printf (which scrolls it above the
// view) instead of rendering it through View(). Inline-image
// escape sequences from emoji.Render confuse Bubbletea's
// cursor tracking, causing lines to vanish on re-render.
m.summary = &msg
return m, tea.Sequence(
tea.Printf("%s", buildSummaryView(msg)),
tea.Quit,
)
case EventTestFailure:
if len(msg.TestFailures) > 0 {
presenter := testPresenter{}
var cmds []tea.Cmd
for _, t := range msg.TestFailures {
line := formatTimestampedBlock(presenter.ColoredLine(t), msg.Time)
cmds = append(cmds, tea.Printf("%s", m.hardwrapLine(line)))
}
return m, tea.Batch(cmds...)
}
}
case tea.WindowSizeMsg:
m.width = msg.Width
return m, nil
case spinner.TickMsg:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
return m, nil
}
func (m ttyModel) statusText() string {
switch {
case m.latest.Title != "":
return m.latest.Title
case m.latest.BuildState != "":
link := fmt.Sprintf("\033]8;;%s\033\\build #%d\033]8;;\033\\", m.latest.BuildURL, m.latest.BuildNumber)
return fmt.Sprintf("Watching %s (%s)", link, m.latest.BuildState)
default:
return "Starting..."
}
}
// hardwrapLine pre-wraps text with explicit newlines at the terminal width so that
// Bubbletea's line counting matches the physical rows the terminal will use.
// This prevents cursor positioning errors that leave View() artifacts in the scrollback.
func (m ttyModel) hardwrapLine(s string) string {
if m.width <= 0 {
return s
}
return ansi.Hardwrap(s, m.width, false)
}
func (m ttyModel) render() string {
separator := ttyBorderStyle.Render("─────────────────────────────────────────────")
statusLine := fmt.Sprintf(" %s %s", m.spinner.View(), ttyStatusStyle.Render(m.statusText()))
if m.latest.Jobs == nil {
return separator + "\n" + statusLine
}
parts := make([]string, 0, 6)
appendPart := func(count int, text string) {
if count > 0 {
parts = append(parts, text)
}
}
appendPart(m.latest.Jobs.Passed, fmt.Sprintf("%d passed", m.latest.Jobs.Passed))
appendPart(m.latest.Jobs.Failed, ttyFailureStyle.Render(fmt.Sprintf("%d failed", m.latest.Jobs.Failed)))
appendPart(m.latest.Jobs.SoftFailed, ttySoftFailureStyle.Render(fmt.Sprintf("%d soft failed", m.latest.Jobs.SoftFailed)))
appendPart(m.latest.Jobs.Running, fmt.Sprintf("%d running", m.latest.Jobs.Running))
appendPart(m.latest.Jobs.Scheduled, fmt.Sprintf("%d scheduled", m.latest.Jobs.Scheduled))
appendPart(m.latest.Jobs.Waiting, fmt.Sprintf("%d waiting", m.latest.Jobs.Waiting))
if len(parts) == 0 {
return separator + "\n" + statusLine
}
summaryLine := fmt.Sprintf(" %s", ttyDimStyle.Render(strings.Join(parts, ", ")))
return separator + "\n" + statusLine + "\n" + summaryLine
}
func (m ttyModel) View() string {
if m.summary != nil {
// Summary was already printed via tea.Printf; return empty
// so Bubbletea clears the spinner area on exit.
return ""
}
return m.hardwrapLine(m.render())
}
// buildSummaryView renders the final build summary as a string for use in View().
func buildSummaryView(e Event) string {
style := ttyFailureStyle
if e.BuildState == "passed" {
style = ttyStatusStyle
}
separator := ttyBorderStyle.Render("─────────────────────────────────────────────")
out := separator + "\n" + style.Render(summaryHeader(e))
presenter := jobPresenter{pipeline: e.Pipeline, buildNumber: e.BuildNumber}
for _, j := range e.PassedJobs {
out += "\n " + presenter.ColoredPassedLine(j, ttyDimStyle)
}
for _, j := range e.FailedJobs {
out += "\n " + presenter.ColoredLine(j)
}
return out
}
type ttyRenderer struct {
program *tea.Program
done chan struct{}
err error
}
func newTTYRenderer(cancel context.CancelFunc) *ttyRenderer {
model := newTTYModel()
model.cancelFunc = cancel
p := tea.NewProgram(model)
r := &ttyRenderer{program: p, done: make(chan struct{})}
go func() {
if _, err := p.Run(); err != nil {
r.err = err
}
close(r.done)
}()
return r
}
func (r *ttyRenderer) Render(e Event) error {
r.program.Send(e)
return nil
}
func (r *ttyRenderer) Close() error {
r.program.Quit()
<-r.done
return r.err
}