-
Notifications
You must be signed in to change notification settings - Fork 3
/
tui_view.go
127 lines (108 loc) · 3.05 KB
/
tui_view.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
package run
import (
"strings"
"github.com/amonks/run/internal/color"
"github.com/charmbracelet/lipgloss"
zone "github.com/lrstanley/bubblezone"
)
type uiZone = string
const (
uiZoneLogs uiZone = "logs"
)
func (m *tuiModel) View() string {
if !m.didInit || !m.gotSize {
return ""
}
if m.focus == focusHelp {
return m.help.View()
}
styles := m.styles(m.width, m.height, m.focus)
var sections []string
if styles.includeHeader {
sections = append(sections, m.renderHeader(styles))
}
sections = append(sections, lipgloss.JoinHorizontal(lipgloss.Top,
m.renderMenu(styles),
zone.Mark(uiZoneLogs, m.renderLog(styles)),
))
if styles.includeFooter {
sections = append(sections, m.renderFooter(styles))
}
return zone.Scan(
lipgloss.JoinVertical(lipgloss.Left, sections...),
)
}
func (m *tuiModel) renderHeader(styles *styles) string {
var out strings.Builder
rightStyle := styles.headerRight.Copy().Foreground(color.Hash(m.activeTaskID()))
out.WriteString(styles.headerLeft.Render("") + rightStyle.Render(m.activeTaskID()))
if styles.headerLine != "" {
out.WriteString("\n" + styles.headerLine)
}
return out.String()
}
func (m *tuiModel) renderMenu(styles *styles) string {
var out strings.Builder
for i, id := range m.ids {
out.WriteString(m.renderMenuItem(styles, i, id) + "\n")
}
return styles.menu.Render(out.String())
}
func (m *tuiModel) renderMenuItem(styles *styles, index int, id string) string {
spinner := m.renderSpinner(id)
marker, isSelected := " ", false
if index == m.selectedTaskIDIndex {
marker, isSelected = ">", true
}
return zone.Mark(id, styles.renderMenuItem(id, spinner, marker, index, isSelected))
}
func (m *tuiModel) renderSpinner(id string) string {
if strings.HasPrefix(id, "@") {
return " "
}
var (
meta = m.tui.run.Tasks().Get(id).Metadata()
status = m.tui.run.TaskStatus(id)
)
switch status {
case TaskStatusNotStarted:
return " "
case TaskStatusRunning:
if meta.Type == "long" {
return m.longSpinner.View()
} else {
return m.shortSpinner.View()
}
case TaskStatusRestarting:
return m.shortSpinner.View()
case TaskStatusFailed:
return "×"
case TaskStatusDone:
return "✓"
default:
return "?"
}
}
func (m *tuiModel) renderLog(styles *styles) string {
activeLogview := m.tasks[m.activeTaskID()]
return activeLogview.Render(styles.log, styles.logWidth, styles.logHeight)
}
func (m *tuiModel) renderFooter(styles *styles) string {
var out strings.Builder
if styles.footerLine != "" {
out.WriteString(styles.footerLine + "\n")
}
var footer strings.Builder
if m.quitKey != "" {
footer.WriteString("press " + m.quitKey + " again to quit")
} else {
footer.WriteString(styles.lineStatus.Render(m.activeTask().RenderLineStatus()))
footer.WriteString("\t")
footer.WriteString(styles.searchStatus.Render(m.activeTask().RenderSearchStatus()))
}
if styles.includeInlineHelp {
footer.WriteString("\n" + helpMenu[0].RenderInline(styles.inlineHelp, styles.inlineHelpWidth, styles.inlineHelpHeight))
}
out.WriteString(styles.footer.Render(footer.String()))
return out.String()
}