From 81300eeac7af56439cedba2b461cacc57944175c Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Thu, 17 Oct 2024 23:12:11 -0400 Subject: A single slider --- internal/slider/slider.go | 105 ++++++++++++++++++++++++++++++++++++++++++++++ main.go | 81 +---------------------------------- 2 files changed, 107 insertions(+), 79 deletions(-) create mode 100644 internal/slider/slider.go diff --git a/internal/slider/slider.go b/internal/slider/slider.go new file mode 100644 index 0000000..0c0a89e --- /dev/null +++ b/internal/slider/slider.go @@ -0,0 +1,105 @@ +package slider + +import ( + "fmt" + + "github.com/charmbracelet/bubbles/progress" + tea "github.com/charmbracelet/bubbletea" +) + +const ( + // Global Keystrokes for sliders + setMinKey = "i" // i -> Initialise + setMaxKey = "I" + bigDecKey = "h" // vim left + smallDecKey = "H" + bigIncKey = "l" // vim right + smallIncKey = "L" +) + +func Keystrokes() []string { + return []string{ + setMinKey, setMaxKey, bigIncKey, bigDecKey, smallIncKey, smallDecKey, + } +} + +type Slider struct { + name string + val int + max int + bar progress.Model + loaded bool + options []progress.Option +} + +func (s Slider) Set(val int) (tea.Model, tea.Cmd) { + s.val = val + if s.val > s.max { + s.val = s.max + } + if s.val < 0 { + s.val = 0 + } + return s, s.bar.SetPercent(float64(s.val) / float64(s.max)) +} + +func (s Slider) Inc(val int) (tea.Model, tea.Cmd) { + return s.Set(s.val + val) +} + +func (s Slider) Dec(val int) (tea.Model, tea.Cmd) { + return s.Set(s.val - val) +} + +func (s Slider) View() string { + return s.bar.View() + fmt.Sprintf(" (%d)", s.val) +} + +func (s Slider) Animate(msg tea.Msg) (tea.Model, tea.Cmd) { + progressModel, cmd := s.bar.Update(msg) + s.bar = progressModel.(progress.Model) + return s, cmd +} + +// Implement tea.Model interface {{{ +func (s Slider) Init() tea.Cmd { + return s.bar.Init() +} + +func New(name string, v int, maxV int, opt ...progress.Option) *Slider { + opt = append(opt, progress.WithoutPercentage()) + return &Slider{name: name, max: maxV, val: v, options: opt} +} + +func (s Slider) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case tea.KeyMsg: + switch msg.String() { + case bigIncKey: + return s.Inc(25) + case smallIncKey: + return s.Inc(1) + case bigDecKey: + return s.Dec(25) + case smallDecKey: + return s.Dec(1) + case setMinKey: + return s.Set(0) + case setMaxKey: + return s.Set(s.max) + default: + return s, tea.Quit + } + case tea.WindowSizeMsg: + if s.bar.Width != msg.Width && !s.loaded { + s.loaded = true + s.bar = progress.New(s.options...) + return s.Set(s.val) + } + case progress.FrameMsg: + return s.Animate(msg) + } + return s, nil +} + +// }}} diff --git a/main.go b/main.go index e6068b2..4124db8 100644 --- a/main.go +++ b/main.go @@ -4,90 +4,13 @@ import ( "fmt" "os" + "github.com/ChausseBenjamin/golorpicker/internal/slider" "github.com/charmbracelet/bubbles/progress" tea "github.com/charmbracelet/bubbletea" ) -type Slider struct { - name string - val int - max int - bar progress.Model - loaded bool -} - -func (m Slider) Init() tea.Cmd { - return m.bar.Init() -} - -func (m Slider) View() string { - return m.name + "\n" + m.bar.View() -} - -func (m Slider) Set(val int) (tea.Model, tea.Cmd) { - m.val = val - if m.val > m.max { - m.val = m.max - } - if m.val < 0 { - m.val = 0 - } - return m, m.bar.SetPercent(float64(m.val) / float64(m.max)) -} - -func (m Slider) Inc(val int) (tea.Model, tea.Cmd) { - return m.Set(m.val + val) -} - -func (m Slider) Dec(val int) (tea.Model, tea.Cmd) { - return m.Set(m.val - val) -} - -func (m Slider) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - switch msg := msg.(type) { - case tea.KeyMsg: - switch msg.String() { - case "l": - // cmd := m.bar.IncrPercent(.1) - // return m, cmd - return m.Inc(25) - case "L": - // cmd := m.bar.IncrPercent(.01) - // return m, cmd - return m.Inc(1) - case "h": - return m.Dec(25) - // cmd := m.bar.DecrPercent(.1) - // return m, cmd - case "H": - return m.Dec(1) - // cmd := m.bar.DecrPercent(.01) - // return m, cmd - case "r": - return m.Set(128) - default: - return m, tea.Quit - } - case tea.WindowSizeMsg: - if m.bar.Width != msg.Width && !m.loaded { - m.loaded = true - m.bar = progress.New(progress.WithSolidFill("#FF0000")) - return m.Set(m.val) - } - case progress.FrameMsg: - progressModel, cmd := m.bar.Update(msg) - m.bar = progressModel.(progress.Model) - return m, cmd - } - return m, nil -} - -func New() *Slider { - return &Slider{name: "Red", max: 255, val: 128} -} - func main() { - m := New() + m := slider.New("Red", 128, 255, progress.WithSolidFill("#ff0000")) if _, err := tea.NewProgram(m).Run(); err != nil { fmt.Println(err.Error()) os.Exit(1) -- cgit v1.2.3