From e23b37a0b0f7027677b323599dc7fdee6949fb47 Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Thu, 17 Oct 2024 22:38:01 -0400 Subject: Batman --- .gitignore | 34 ++++++++++++++++++++++ main.go | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 .gitignore create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5d3bf26 --- /dev/null +++ b/.gitignore @@ -0,0 +1,34 @@ +# Swap +[._]*.s[a-v][a-z] +!*.svg # comment out if you don't need vector files +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim +Sessionx.vim + +# Temporary +.netrwhist +*~ +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ diff --git a/main.go b/main.go new file mode 100644 index 0000000..e6068b2 --- /dev/null +++ b/main.go @@ -0,0 +1,95 @@ +package main + +import ( + "fmt" + "os" + + "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() + if _, err := tea.NewProgram(m).Run(); err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } +} -- cgit v1.2.3