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
|
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)
}
}
|