-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.go
105 lines (91 loc) · 2.32 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
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)
}
// 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 int
Max int
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
}
}