-
Notifications
You must be signed in to change notification settings - Fork 6
/
view.go
104 lines (82 loc) · 2.02 KB
/
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
package ui
import (
"fmt"
"strings"
"github.com/charmbracelet/lipgloss"
"github.com/koki-develop/clive/internal/styles"
"github.com/koki-develop/clive/internal/util"
)
func (m *Model) View() string {
s := ""
if m.err != nil {
s += m.errView() + "\n\n"
}
if m.config == nil {
return m.loadingConfigView()
}
if m.page == nil {
return m.openingView()
}
s += m.actionsView()
if m.quitting {
s += "\n\n" + m.quittingView()
}
return s
}
func (m *Model) errView() string {
if !m.running() {
return ""
}
return styles.StyleErrorHeader.Render("Error") + "\n" + m.err.Error()
}
func (m *Model) loadingConfigView() string {
return fmt.Sprintf("%s Loading config", m.spinner.View())
}
func (m *Model) openingView() string {
return fmt.Sprintf("%s Opening", m.spinner.View())
}
func (m *Model) actionsView() string {
from := util.Max(0, m.currentActionIndex-3)
show := 20
rows := []string{}
for i, action := range m.config.Actions {
if i < from && len(m.config.Actions)-i > show {
continue
}
if i-from >= show {
rows = append(rows, fmt.Sprintf("... %d more actions", len(m.config.Actions)-i))
break
}
var style lipgloss.Style
if m.currentActionIndex > i {
style = styles.StyleDone
} else if m.currentActionIndex == i {
style = styles.StyleActive
}
s, trunc := util.TruncateString(action.String(), 40)
if trunc {
s += styles.StyleTruncated.Render("...")
}
digits := util.Digits(len(m.config.Actions))
num := util.PaddingRight(fmt.Sprintf("#%d", i+1), digits+1)
cursor := m.cursorView(i)
rows = append(rows, fmt.Sprintf("%s %s%s", style.Render(num), cursor, style.Render(s)))
}
return styles.StyleActionHeader.Render("Actions") + "\n" + strings.Join(rows, "\n")
}
func (m *Model) cursorView(idx int) string {
cursor := " "
if m.currentActionIndex != idx {
return cursor
}
if m.quitting {
return cursor
}
if m.pausing {
return "> "
}
return m.spinner.View()
}
func (m *Model) quittingView() string {
return styles.StyleActive.Render("Press enter to quit")
}