summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorBenjamin Chausse <benjamin@chausse.xyz>2024-10-17 23:12:11 -0400
committerBenjamin Chausse <benjamin@chausse.xyz>2024-10-17 23:12:11 -0400
commit81300eeac7af56439cedba2b461cacc57944175c (patch)
treeec8810e34f59b0593fa69a382d717d7d7895ce69 /main.go
parent0ab03ce4c13e63b2403eb1039b7d0185d3d30d9d (diff)
A single slider
Diffstat (limited to 'main.go')
-rw-r--r--main.go81
1 files changed, 2 insertions, 79 deletions
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)