-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_picker.go
115 lines (102 loc) · 2.72 KB
/
color_picker.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
package whys
import (
_ "embed"
"regexp"
"strings"
"github.com/benhsm/goalie/internal/ui/common"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var (
//go:embed hex_colors.txt
color_data string
frame = lipgloss.NewStyle().Padding(1, 1)
inputStyle = lipgloss.NewStyle().
Border(lipgloss.RoundedBorder(), false, false, true, false)
colorRegex = regexp.MustCompile("^(#[A-Z0-9]{6})|([0-9]{1,3})$")
)
// colorPickerModel represents a UI for choosing a hex color
type colorPickerModel struct {
// Colors is a slice a list of preset colors
Colors []lipgloss.Color
// Choice is a color chosen by the user
Choice lipgloss.Color
selection textinput.Model
info string
done bool
common.Common
}
// New returns a new color picker model
func newColorPicker() colorPickerModel {
colorStrings := strings.Split(color_data, "\n")
var colorList []lipgloss.Color
for _, color := range colorStrings {
if color == "" {
break
}
colorList = append(colorList, lipgloss.Color(color))
}
selection := textinput.New()
selection.Placeholder = "choose a color"
selection.CharLimit = 7
selection.Width = 13
selection.Focus()
return colorPickerModel{
Colors: colorList,
selection: selection,
info: "Input either a hex code or an ANSI color code.",
}
}
func (m colorPickerModel) Init() tea.Cmd {
return textinput.Blink
}
// Update is a Bubble Tea
func (m colorPickerModel) Update(msg tea.Msg) (colorPickerModel, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.SetSize(msg.Height, msg.Width)
case tea.KeyMsg:
switch {
case msg.Type == tea.KeyCtrlC:
return m, tea.Quit
case msg.Type == tea.KeyEnter:
if !validColor(m.selection.Value()) {
m.info = "Sorry, that's not a valid color value."
} else {
m.Choice = lipgloss.Color(m.selection.Value())
m.selection.SetValue("")
}
}
}
m.selection, cmd = m.selection.Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m colorPickerModel) View() string {
var b strings.Builder
b.WriteString(inputStyle.Render(m.selection.View()))
b.WriteString("\n")
b.WriteString(m.info)
for i, color := range m.Colors {
if i%7 == 0 {
b.WriteString("\n\n")
}
b.WriteString(string(color))
b.WriteString(" ")
b.WriteString(renderColor(color))
b.WriteString(" ")
}
return lipgloss.Place(m.Width, m.Height, lipgloss.Center, lipgloss.Center, b.String())
}
func renderColor(color lipgloss.Color) string {
return lipgloss.NewStyle().Background(color).Render(" ")
}
func validColor(s string) bool {
if colorRegex.MatchString(s) && colorRegex.FindString(s) == s {
return true
}
return false
}