@@ -3,43 +3,45 @@ package ui
3
3
import (
4
4
"fmt"
5
5
6
+ "github.com/fatih/color"
6
7
"github.com/muesli/termenv"
7
8
)
8
9
9
10
// General stuff for styling the view
10
11
var (
11
- term = termenv .EnvColorProfile ()
12
- subtle = makeFgStyle ("241" )
13
- red = makeFgStyle ("196" )
14
- green = makeFgStyle ("46" )
15
- yellow = makeFgStyle ("226" )
12
+ Term = termenv .EnvColorProfile ()
13
+ Subtle = MakeFgStyle ("241" )
14
+ Red = MakeFgStyle ("196" )
15
+ Green = MakeFgStyle ("46" )
16
+ Yellow = MakeFgStyle ("226" )
16
17
)
17
18
18
19
type (
19
- errMsg error
20
+ // ErrMsg is an error message.
21
+ ErrMsg error
20
22
)
21
23
22
- // makeFgStyle returns a function that will colorize the foreground of a given.
23
- func makeFgStyle (color string ) func (string ) string {
24
- return termenv.Style {}.Foreground (term .Color (color )).Styled
24
+ // MakeFgStyle returns a function that will colorize the foreground of a given.
25
+ func MakeFgStyle (color string ) func (string ) string {
26
+ return termenv.Style {}.Foreground (Term .Color (color )).Styled
25
27
}
26
28
27
- // Color a string's foreground with the given value.
28
- func colorFg (val , color string ) string {
29
- return termenv .String (val ).Foreground (term .Color (color )).String ()
29
+ // ColorFg a string's foreground with the given value.
30
+ func ColorFg (val , color string ) string {
31
+ return termenv .String (val ).Foreground (Term .Color (color )).String ()
30
32
}
31
33
32
- // checkbox represent [ ] and [x] items in the view.
33
- func checkbox (label string , checked bool ) string {
34
+ // Checkbox represent [ ] and [x] items in the view.
35
+ func Checkbox (label string , checked bool ) string {
34
36
if checked {
35
- return colorFg ("[x] " + label , "212" )
37
+ return ColorFg ("[x] " + label , "212" )
36
38
}
37
39
return fmt .Sprintf ("[ ] %s" , label )
38
40
}
39
41
40
- // split splits a string into multiple lines.
42
+ // Split splits a string into multiple lines.
41
43
// Each line has a maximum length of 80 characters.
42
- func split (s string ) []string {
44
+ func Split (s string ) []string {
43
45
var result []string
44
46
for i := 0 ; i < len (s ); i += 80 {
45
47
end := i + 80
@@ -50,3 +52,54 @@ func split(s string) []string {
50
52
}
51
53
return result
52
54
}
55
+
56
+ // GoodByeMessage returns a goodbye message.
57
+ func GoodByeMessage () string {
58
+ s := fmt .Sprintf ("\n See you later 🌈\n %s\n %s\n \n " ,
59
+ "Following URL for bug reports and encouragement (e.g. GitHub Star ⭐️ )" ,
60
+ color .GreenString ("https://github.com/nao1215/rainbow" ))
61
+ return s
62
+ }
63
+
64
+ // ErrorMessage returns an error message.
65
+ func ErrorMessage (err error ) string {
66
+ message := fmt .Sprintf ("%s\n " , Red ("[Error]" ))
67
+ for _ , line := range Split (err .Error ()) {
68
+ message += fmt .Sprintf (" %s\n " , Red (line ))
69
+ }
70
+ return message
71
+ }
72
+
73
+ // Choice represents a choice.
74
+ type Choice struct {
75
+ Choice int
76
+ Max int
77
+ Min int
78
+ }
79
+
80
+ // NewChoice returns a new choice.
81
+ func NewChoice (min , max int ) * Choice {
82
+ return & Choice {
83
+ Choice : min ,
84
+ Max : max ,
85
+ Min : min ,
86
+ }
87
+ }
88
+
89
+ // Increment increments the choice.
90
+ // If the choice is greater than the maximum, the choice is set to the minimum.
91
+ func (c * Choice ) Increment () {
92
+ c .Choice ++
93
+ if c .Choice > c .Max {
94
+ c .Choice = c .Min
95
+ }
96
+ }
97
+
98
+ // Decrement decrements the choice.
99
+ // If the choice is less than the minimum, the choice is set to the maximum.
100
+ func (c * Choice ) Decrement () {
101
+ c .Choice --
102
+ if c .Choice < c .Min {
103
+ c .Choice = c .Max
104
+ }
105
+ }
0 commit comments