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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
package switcher
import (
"fmt"
"log/slog"
"strings"
"github.com/ChausseBenjamin/termpicker/internal/colors"
"github.com/ChausseBenjamin/termpicker/internal/notices"
"github.com/ChausseBenjamin/termpicker/internal/picker"
"github.com/ChausseBenjamin/termpicker/internal/preview"
"github.com/ChausseBenjamin/termpicker/internal/quit"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
const (
IndexRgb int = iota
IndexCmyk
IndexHsl
)
type Model struct {
active int
pickers []picker.Model
preview preview.Model
help help.Model
input textinput.Model
notices notices.Model
fullHelp bool // When false, only show help for the switcher (not children)
}
func New() Model {
pickers := []picker.Model{ // Order MUST match the Index* constants
*picker.RGB(),
*picker.CMYK(),
*picker.HSL(),
}
return Model{
active: 0,
pickers: pickers,
preview: *preview.New(colors.Hex(pickers[0].GetColor())),
help: help.New(),
input: textinput.New(),
notices: notices.New(),
fullHelp: false,
}
}
func (m Model) fixSel(val int) int {
size := len(m.pickers)
return (val%size + size) % size
}
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) SetActive(i int) {
m.active = m.fixSel(i)
}
func (m *Model) UpdatePicker(i int, c colors.ColorSpace) {
m.pickers[i].SetColor(c)
}
func (m *Model) NewNotice(msg string) tea.Cmd {
return m.notices.New(msg)
}
func (m Model) Init() tea.Cmd {
cmds := []tea.Cmd{}
// The NoticeExpiryMsg is never sent to bubbletea by a tea.Cmd for the initial notices
// That's why we need to manually reset them here. Otherwise, they would never expire.
for k := range m.notices.Notices {
cmds = append(cmds, m.notices.Reset(k))
}
for _, p := range m.pickers {
cmds = append(cmds, p.Init())
}
return tea.Batch(cmds...)
}
func (m Model) View() string {
norm := lipgloss.NewStyle().Faint(true)
bright := lipgloss.NewStyle().Faint(false)
delims := [3]string{"[ ", " | ", "]"}
for i, d := range delims {
delims[i] = bright.Render(d)
}
var sections []string
for i, p := range m.pickers {
if i == m.active {
sections = append(
sections,
bright.
Underline(true).
Bold(true).
Render(p.Title()),
)
} else {
sections = append(sections, norm.Render(p.Title()))
}
}
tabs := "[ " + strings.Join(sections, " | ") + " ]"
pickerView := m.pickers[m.active].View()
boxStyle := lipgloss.NewStyle().Border(lipgloss.RoundedBorder(), true, true, false, true)
w := lipgloss.Width(pickerView)
pickerView = boxStyle.Render(pickerView)
m.preview.SetWidth(w)
boxStyle = boxStyle.Border(lipgloss.RoundedBorder(), false, true, false, true)
previewStr := boxStyle.Render(m.preview.View())
m.help.Styles.ShortKey.Width(w)
boxStyle = boxStyle.Border(lipgloss.RoundedBorder(), false, true, true, true).Width(w)
var helpstr string
if m.fullHelp {
helpstr = m.help.FullHelpView(m.AllKeys())
} else {
// This is a hack since the current view has too many keys
// and the horizontal "ShortHelpView" gets too wide.
// "FullHelpView" seperates keys by columns (and we only show the first).
// helpstr = m.help.FullHelpView([][]key.Binding{m.AllKeys()[0]})
helpstr = m.help.FullHelpView(shortKeys())
}
helpstr = boxStyle.Render(helpstr)
inputStr := ""
if m.input.Focused() {
boxStyle = boxStyle.Border(lipgloss.RoundedBorder(), true, true, true, true).Width(w)
inputStr = boxStyle.Render(m.input.View())
}
return fmt.Sprintf("%s\n%s\n%s\n%v\n%v\n%v",
tabs,
pickerView,
previewStr,
helpstr,
inputStr,
m.notices.View(),
)
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
keys := newKeybinds()
cmds := []tea.Cmd{}
slog.Info("Received tea.Msg", "tea_msg", msg, "type", fmt.Sprintf("%T", msg))
switch msg := msg.(type) {
case notices.NoticeExpiryMsg:
newNotices, cmd := m.notices.Update(msg)
m.notices = newNotices.(notices.Model)
cmds = append(cmds, cmd)
case tea.KeyMsg:
if m.input.Focused() {
keys.esc.SetEnabled(true)
keys.confirm.SetEnabled(true)
if key.Matches(msg, keys.esc) {
m.input.Blur()
} else if key.Matches(msg, keys.confirm) {
m.input.Blur()
cmds = append(
cmds,
m.NewNotice(m.SetColorFromText(m.input.Value())),
m.Init(), // Will force a slider update/animation
)
}
newInput, cmd := m.input.Update(msg)
m.input = newInput
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
switch {
case key.Matches(msg, keys.next):
cs := m.pickers[m.active].GetColor()
m.Next()
m.pickers[m.active].SetColor(cs)
case key.Matches(msg, keys.prev):
cs := m.pickers[m.active].GetColor()
m.Prev()
m.pickers[m.active].SetColor(cs)
case key.Matches(msg, keys.copy):
cmd := m.notices.New(m.copyColor(msg.String()))
cmds = append(cmds, cmd)
case key.Matches(msg, keys.help):
m.fullHelp = !m.fullHelp
case key.Matches(msg, keys.insert):
cmd := m.input.Focus()
cmds = append(cmds, cmd)
case key.Matches(msg, keys.quit):
return quit.Model{}, tea.Quit
default:
// Update the picker
newActive, cmd := m.pickers[m.active].Update(msg)
m.pickers[m.active] = newActive.(picker.Model)
cmds = append(cmds, cmd)
// Update the preview
newPreview := preview.New(colors.Hex(m.pickers[m.active].GetColor()))
m.preview = *newPreview
newNotices, cmd := m.notices.Update(msg)
m.notices = newNotices.(notices.Model)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
default:
}
for i, p := range m.pickers {
newActive, cmd := p.Update(msg)
m.pickers[i] = newActive.(picker.Model)
cmds = append(cmds, cmd)
}
// Update the preview
newPreview := preview.New(colors.Hex(m.pickers[m.active].GetColor()))
m.preview = *newPreview
return m, tea.Batch(cmds...)
}
|