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
168
169
170
171
172
173
174
175
176
177
178
|
package picker
import (
"fmt"
"strings"
"github.com/ChausseBenjamin/termpicker/internal/colors"
"github.com/ChausseBenjamin/termpicker/internal/slider"
"github.com/ChausseBenjamin/termpicker/internal/ui"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
)
type Model struct {
title string
active int
sliders []slider.Model
}
func (m *Model) Next() int {
m.active = m.fixSel(m.active + 1)
return m.active
}
func (m *Model) Prev() int {
m.active = m.fixSel(m.active - 1)
return m.active
}
func (m *Model) Sel(i int) int {
m.active = m.fixSel(i)
return m.active
}
func (m Model) fixSel(val int) int {
size := len(m.sliders)
return (val%size + size) % size
}
func New(sliders []slider.Model, title string) *Model {
return &Model{
title: title,
active: 0,
sliders: sliders,
}
}
func (m Model) Title() string {
return m.title
}
func (m Model) GetColor() colors.ColorSpace {
switch m.title {
case "RGB":
return colors.RGB{
R: m.sliders[0].Val(),
G: m.sliders[1].Val(),
B: m.sliders[2].Val(),
}
case "CMYK":
return colors.CMYK{
C: m.sliders[0].Val(),
M: m.sliders[1].Val(),
Y: m.sliders[2].Val(),
K: m.sliders[3].Val(),
}
case "HSL":
return colors.HSL{
H: m.sliders[0].Val(),
S: m.sliders[1].Val(),
L: m.sliders[2].Val(),
}
default: // Default to white if we don't know the color space
return colors.RGB{
R: 255,
G: 255,
B: 255,
}
}
}
func (m Model) SetColor(c colors.ColorSpace) {
p := c.ToPrecise()
switch m.title {
case "RGB":
rgb := colors.RGB{}.FromPrecise(p).(colors.RGB)
m.sliders[0].Set(rgb.R)
m.sliders[1].Set(rgb.G)
m.sliders[2].Set(rgb.B)
case "CMYK":
cmyk := colors.CMYK{}.FromPrecise(p).(colors.CMYK)
m.sliders[0].Set(cmyk.C)
m.sliders[1].Set(cmyk.M)
m.sliders[2].Set(cmyk.Y)
m.sliders[3].Set(cmyk.K)
case "HSL":
hsl := colors.HSL{}.FromPrecise(p).(colors.HSL)
m.sliders[0].Set(hsl.H)
m.sliders[1].Set(hsl.S)
m.sliders[2].Set(hsl.L)
}
}
func (m Model) Init() tea.Cmd {
cmds := []tea.Cmd{}
for _, s := range m.sliders {
cmds = append(cmds, s.Init())
}
return tea.Batch(cmds...)
}
func ViewSlider(active bool, s slider.Model) string {
if active {
return fmt.Sprintf("%s %s",
ui.Style().PickerCursor.Render(ui.PickerSelRune),
s.View(),
)
}
return fmt.Sprintf(" %s",
s.View(),
)
}
func (m Model) View() string {
sliderList := make([]string, len(m.sliders))
for i, slider := range m.sliders {
sliderList[i] = ViewSlider(i == m.active, slider)
}
return strings.Join(sliderList, "\n")
}
func (m Model) FixWidth(windowW int) (tea.Model, tea.Cmd) {
cmds := []tea.Cmd{}
currentW := ui.SliderMaxWidth
if (currentW + ui.BoxOffsetW) > windowW {
currentW -= (currentW + ui.BoxOffsetW) - windowW
}
if currentW < ui.SliderMinWidth {
currentW = ui.SliderMinWidth
}
for i, s := range m.sliders {
model, cmd := s.SetWidth(currentW)
cmds = append(cmds, cmd)
m.sliders[i] = model.(slider.Model)
}
return m, tea.Batch(cmds...)
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
keys := newKeybinds()
cmds := []tea.Cmd{}
switch msg := msg.(type) {
case tea.WindowSizeMsg:
newModel, cmd := m.FixWidth(msg.Width)
cmds = append(cmds, cmd)
return newModel, tea.Batch(cmds...)
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.next):
m.Next()
case key.Matches(msg, keys.prev):
m.Prev()
default:
newActive, cmd := m.sliders[m.active].Update(msg)
m.sliders[m.active] = newActive.(slider.Model)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
}
// Keys are only sent to the active sliders
// However, other messages (ex: tick, resize) must be sent to all
for i, s := range m.sliders {
newSlider, cmd := s.Update(msg)
m.sliders[i] = newSlider.(slider.Model)
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
}
|