-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbar.go
More file actions
141 lines (120 loc) · 2.72 KB
/
bar.go
File metadata and controls
141 lines (120 loc) · 2.72 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package tui
import (
"bytes"
"fmt"
"github.com/charmbracelet/bubbles/progress"
tea "github.com/charmbracelet/bubbletea"
"os"
"strings"
"time"
)
const (
padding = 2
maxWidth = 60
)
func NewBar() *BarModel {
bar := &BarModel{
Model: progress.New(progress.WithDefaultGradient()),
}
return bar
}
type progressMsg float64
type progressErrMsg struct{ err error }
type ViewMsg struct {
}
func finalPause() tea.Cmd {
return tea.Tick(time.Millisecond*750, func(_ time.Time) tea.Msg {
return nil
})
}
type BarModel struct {
Model progress.Model
progressPercent float64
*bytes.Buffer
err error
}
func (m *BarModel) Init() tea.Cmd {
return setPercentMsg(m.progressPercent)
}
func (m *BarModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
return m, tea.Quit
case tea.WindowSizeMsg:
m.Model.Width = msg.Width - padding*2 - 4
if m.Model.Width > maxWidth {
m.Model.Width = maxWidth
}
return m, nil
case progressErrMsg:
m.err = msg.err
return m, tea.Quit
case progressMsg:
//var cmds []tea.Cmd
//
//if msg >= 1.0 {
// cmds = append(cmds, tea.Sequence(finalPause(), tea.Quit))
//}
//
//cmds = append(cmds, m.Model.SetPercent(float64(msg)))
//return m, tea.Batch(cmds...)
if m.Model.Percent() == 1.0 {
return m, tea.Quit
}
m.progressPercent = float64(msg)
cmd := m.Model.SetPercent(m.progressPercent)
return m, tea.Batch(setPercentMsg(m.progressPercent), cmd)
// FrameMsg is sent when the Model bar wants to animate itself
case progress.FrameMsg:
progressModel, cmd := m.Model.Update(msg)
m.Model = progressModel.(progress.Model)
return m, cmd
case ViewMsg:
m.View()
return m, nil
default:
return m, nil
}
}
func (m *BarModel) View() string {
if m.err != nil {
return m.err.Error()
}
var s strings.Builder
pad := strings.Repeat(" ", padding)
s.WriteString("\n" +
pad + m.Model.ViewAs(m.progressPercent) + "\n\n" +
pad + HelpStyle("Press any key to quit"))
s.WriteString(m.Buffer.String())
return s.String()
}
func setPercentMsg(percent float64) tea.Cmd {
return func() tea.Msg {
return progressMsg(percent)
}
}
func (m *BarModel) SetProgressPercent(percent float64) {
m.progressPercent = percent
}
func (m *BarModel) Run() error {
p := tea.NewProgram(m)
_, err := p.Run()
if err != nil {
return err
}
fmt.Printf(HelpStyle("<Press enter to exit>\n"))
os.Stdin.Write([]byte("\n"))
ClearLines(1)
return nil
}
//func (m *BarModel) SetOnProgress(p *tea.Program) {
// m.pw.onProgress = func(f float64) {
// p.Send(progressMsg(f))
// }
//}
//func (m *BarModel) Incr() {
// m.pw.completed++
// if m.pw.total > 0 && m.pw.onProgress != nil {
// m.pw.onProgress(float64(m.pw.completed) / float64(m.pw.total))
// }
//}