/
common.go
167 lines (145 loc) · 3.52 KB
/
common.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
package ui
import (
"fmt"
"github.com/fatih/color"
"github.com/muesli/termenv"
)
// General stuff for styling the view
var (
Term = termenv.EnvColorProfile()
Subtle = MakeFgStyle("241")
Red = MakeFgStyle("196")
Green = MakeFgStyle("46")
Yellow = MakeFgStyle("226")
)
type (
// ErrMsg is an error message.
ErrMsg error
)
// MakeFgStyle returns a function that will colorize the foreground of a given.
func MakeFgStyle(color string) func(string) string {
return termenv.Style{}.Foreground(Term.Color(color)).Styled
}
// ColorFg a string's foreground with the given value.
func ColorFg(val, color string) string {
return termenv.String(val).Foreground(Term.Color(color)).String()
}
// Checkbox represent [ ] and [x] items in the view.
func Checkbox(label string, checked bool) string {
if checked {
return ColorFg("[x] "+label, "212")
}
return fmt.Sprintf("[ ] %s", label)
}
// ToggleWidget represents a toggle.
func ToggleWidget(label string, now, enabled bool) string {
if now {
if enabled {
return ColorFg("▶ [x] "+label, "212")
}
return ColorFg("▶ [ ] "+label, "212")
}
if enabled {
return ColorFg(" [x] "+label, "212")
}
return fmt.Sprintf(" [ ] %s", label)
}
// Split splits a string into multiple lines.
// Each line has a maximum length of 80 characters.
func Split(s string) []string {
var result []string
for i := 0; i < len(s); i += 80 {
end := i + 80
if end > len(s) {
end = len(s)
}
result = append(result, s[i:end])
}
return result
}
// GoodByeMessage returns a goodbye message.
func GoodByeMessage() string {
s := fmt.Sprintf("\n See you later 🌈\n %s\n %s\n\n",
"Following URL for bug reports and encouragement (e.g. GitHub Star ⭐️ )",
color.GreenString("https://github.com/nao1215/rainbow"))
return s
}
// ErrorMessage returns an error message.
func ErrorMessage(err error) string {
message := fmt.Sprintf("%s\n", Red("[Error]"))
for _, line := range Split(err.Error()) {
message += fmt.Sprintf(" %s\n", Red(line))
}
return message
}
// Choice represents a choice.
type Choice struct {
// Choice is the currently selected menu item.
Choice int
// Max is the maximum choice number.
Max int
// Min is the minimum choice number.
Min int
}
// NewChoice returns a new choice.
func NewChoice(min, max int) *Choice {
return &Choice{
Choice: min,
Max: max,
Min: min,
}
}
// Increment increments the choice.
// If the choice is greater than the maximum, the choice is set to the minimum.
func (c *Choice) Increment() {
c.Choice++
if c.Choice > c.Max {
c.Choice = c.Min
}
}
// Decrement decrements the choice.
// If the choice is less than the minimum, the choice is set to the maximum.
func (c *Choice) Decrement() {
c.Choice--
if c.Choice < c.Min {
c.Choice = c.Max
}
}
// Toggle represents a toggle.
type Toggle struct {
Enabled bool
}
// NewToggle returns a new toggle.
func NewToggle() *Toggle {
return &Toggle{
Enabled: false,
}
}
// Toggle toggles the toggle.
func (t *Toggle) Toggle() {
t.Enabled = !t.Enabled
}
// ToggleSets represents a set of toggles.
type ToggleSets []*Toggle
// NewToggleSets returns a new toggle sets.
func NewToggleSets(n int) ToggleSets {
ts := make([]*Toggle, 0, n)
for i := 0; i < n; i++ {
ts = append(ts, NewToggle())
}
return ts
}
// Window represents the window size.
type Window struct {
// Width is the window width.
Width int
// Height is the window height.
Height int
}
// NewWindow returns a new window.
func NewWindow(width, height int) *Window {
return &Window{
Width: width,
Height: height,
}
}