-
Notifications
You must be signed in to change notification settings - Fork 25
/
views.go
135 lines (113 loc) · 2.92 KB
/
views.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
package common
import (
"fmt"
"strings"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/lipgloss"
)
// State is a general UI state used to help style components.
type State int
// UI states.
const (
StateNormal State = iota
StateSelected
StateActive
StateSpecial
StateDeleting
)
var lineColors = map[State]lipgloss.TerminalColor{
StateNormal: lipgloss.AdaptiveColor{Light: "#BCBCBC", Dark: "#646464"},
StateSelected: lipgloss.Color("#F684FF"),
StateDeleting: lipgloss.AdaptiveColor{Light: "#FF8BA7", Dark: "#893D4E"},
StateSpecial: lipgloss.Color("#04B575"),
}
// VerticalLine return a vertical line colored according to the given state.
func VerticalLine(state State) string {
return lipgloss.NewStyle().
SetString("│").
Foreground(lineColors[state]).
String()
}
var valStyle = lipgloss.NewStyle().Foreground(indigo)
var (
spinnerStyle = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#8E8E8E", Dark: "#747373"})
blurredButtonStyle = lipgloss.NewStyle().
Foreground(cream).
Background(lipgloss.AdaptiveColor{Light: "#BDB0BE", Dark: "#827983"}).
Padding(0, 3)
focusedButtonStyle = blurredButtonStyle.Copy().
Background(fuschia)
)
// KeyValueView renders key-value pairs.
func KeyValueView(stuff ...string) string {
if len(stuff) == 0 {
return ""
}
var (
s string
index int
)
for i := 0; i < len(stuff); i++ {
if i%2 == 0 {
// even: key
s += fmt.Sprintf("%s %s: ", VerticalLine(StateNormal), stuff[i])
continue
}
// odd: value
s += valStyle.Render(stuff[i])
s += "\n"
index++
}
return strings.TrimSpace(s)
}
// NewSpinner returns a spinner model.
func NewSpinner() spinner.Model {
s := spinner.New()
s.Spinner = spinner.Dot
s.Style = spinnerStyle
return s
}
// OKButtonView returns a button reading "OK".
func OKButtonView(focused bool, defaultButton bool) string {
return styledButton("OK", defaultButton, focused)
}
// CancelButtonView returns a button reading "Cancel.".
func CancelButtonView(focused bool, defaultButton bool) string {
return styledButton("Cancel", defaultButton, focused)
}
func styledButton(str string, underlined, focused bool) string {
var st lipgloss.Style
if focused {
st = focusedButtonStyle.Copy()
} else {
st = blurredButtonStyle.Copy()
}
if underlined {
st = st.Underline(true)
}
return st.Render(str)
}
var (
helpDivider = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#DDDADA", Dark: "#3C3C3C"}).
Padding(0, 1).
Render("•")
helpSection = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#9B9B9B", Dark: "#5C5C5C"})
)
// HelpView renders text intended to display at help text, often at the
// bottom of a view.
func HelpView(sections ...string) string {
var s string
if len(sections) == 0 {
return s
}
for i := 0; i < len(sections); i++ {
s += helpSection.Render(sections[i])
if i < len(sections)-1 {
s += helpDivider
}
}
return s
}