From d6c5530b35743d1a2f153f942f508ca0768870af Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Thu, 24 Oct 2024 12:02:39 -0400 Subject: more than one slider WIP --- internal/picker/picker.go | 89 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 internal/picker/picker.go (limited to 'internal/picker/picker.go') diff --git a/internal/picker/picker.go b/internal/picker/picker.go new file mode 100644 index 0000000..290e9f9 --- /dev/null +++ b/internal/picker/picker.go @@ -0,0 +1,89 @@ +package picker + +import ( + "github.com/ChausseBenjamin/golorpicker/internal/slider" + "github.com/charmbracelet/bubbles/list" + tea "github.com/charmbracelet/bubbletea" +) + +const ( + rgbTitle string = "RGB" +) + +// A picker is a list of sliders allowing the user to select a color +// It must therefore be capable of exporting a HEX value for previewing +// as well as a + +type RGBPicker struct { + sliders list.Model + r, g, b slider.Slider + loaded bool +} + +func NewRGB() *RGBPicker { + r := slider.New("R", 0, 255) + g := slider.New("G", 0, 255) + b := slider.New("B", 0, 255) + + sliders := list.New( + []list.Item{*r, *g, *b}, + list.NewDefaultDelegate(), + 80, 6, + ) + sliders.Title = rgbTitle + sliders.SetShowHelp(false) + sliders.SetShowFilter(false) + sliders.DisableQuitKeybindings() + + return &RGBPicker{ + sliders: sliders, + r: *r, + g: *g, + b: *b, + loaded: false, + } +} + +func (p RGBPicker) toHex() string { + const txt = "0123456789ABCDEF" + rgb := string([]byte{ + '#', + txt[(p.r.Val()>>4)&0x0F], + txt[p.r.Val()&0x0F], + txt[(p.g.Val()>>4)&0x0F], + txt[p.g.Val()&0x0F], + txt[(p.b.Val()>>4)&0x0F], + txt[p.b.Val()&0x0F], + }) + return rgb +} + +func (p RGBPicker) Init() tea.Cmd { + return nil +} + +func (p RGBPicker) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case tea.WindowSizeMsg: + if p.sliders.Width() != msg.Width && !p.loaded { + p.sliders.SetWidth(msg.Width - 6) + p.loaded = true + } + case tea.KeyMsg: + key := msg.String() + for _, k := range slider.Keystrokes() { + if key == k { + m := p.sliders.Items()[p.sliders.Index()].(slider.Slider) + return m.Update(msg) + } + } + } + + var cmd tea.Cmd + p.sliders, cmd = p.sliders.Update(msg) + return p, cmd +} + +func (p RGBPicker) View() string { + return p.sliders.View() +} -- cgit v1.2.3