-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
211 lines (184 loc) · 4.39 KB
/
main.go
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"errors"
"flag"
"log"
"os"
"path/filepath"
"sync"
"github.com/KasimKaizer/SweetClover/internal/music"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/progress"
tea "github.com/charmbracelet/bubbletea"
)
/* List model */
type tuiMusic struct {
*music.Music
width *int
}
func (m *tuiMusic) FilterValue() string {
return m.Name
}
func (m *tuiMusic) Title() string {
return truncate(m.Name, fineTuneSize(*m.width, 0.3)) //nolint:gomnd // fineTuning
}
func (m *tuiMusic) Description() string {
return truncate(m.Artist, fineTuneSize(*m.width, 0.3)) //nolint:gomnd // fineTuning
}
/* End List Model */
/* Main Model */
type Model struct {
selectedIdx int
list list.Model
progress progress.Model
controller *music.Controller
done chan struct{}
currentPlaying string
displayedImg string
width int
height int
}
func newModel(path string) (*Model, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return nil, err
}
var collection []list.Item
model := &Model{ //nolint:exhaustruct // its fine to have default values.
progress: progress.New(
progress.WithDefaultScaledGradient(),
progress.WithoutPercentage(),
),
controller: music.NewController(),
}
if fileInfo.IsDir() {
collection, err = generateMusicCollection(path, &model.width)
if err != nil {
return nil, err
}
} else {
m, mErr := music.NewMusic(path)
if mErr != nil {
return nil, mErr
}
newMusic := &tuiMusic{
Music: m,
width: &model.width,
}
collection = append(collection, newMusic)
}
if len(collection) == 0 {
return nil, errors.New("no music files in the provided path")
}
delegate := list.NewDefaultDelegate()
keys := newExtraKeyMap()
delegate.FullHelpFunc = func() [][]key.Binding {
return [][]key.Binding{{keys.TogglePlay, keys.TogglePausePlay, keys.SeekForward, keys.SeekBackward}}
}
model.list = list.New([]list.Item{}, delegate, 0, 0)
model.list.Title = filepath.Base(path)
model.list.SetItems(collection)
return model, nil
}
/* Implement Bubbletea model */
func (m *Model) Init() tea.Cmd {
return m.progressCmd()
}
func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m.updateHomeScreen(msg)
}
func (m *Model) View() string {
if m.height == 0 {
return "Loading..."
}
return m.homePageView()
}
func generateMusicCollection(path string, textWidth *int) ([]list.Item, error) {
var collection []list.Item
var wg sync.WaitGroup
mChan := make(chan *tuiMusic)
var onceErr error
var once sync.Once
setErr := func(err error) {
if err != nil {
once.Do(func() { onceErr = err })
}
}
walk := func(path string, f os.FileInfo, err error) error {
wg.Add(1)
go func() {
defer wg.Done()
if f.IsDir() || !music.IsMusic(path) {
return
}
m, musicErr := music.NewMusic(path)
if err != nil {
setErr(musicErr)
}
newMusic := &tuiMusic{
Music: m,
width: textWidth,
}
mChan <- newMusic
}()
return nil
}
_ = filepath.Walk(path, walk) // error ignored as its always nil.
go func() {
wg.Wait()
close(mChan)
}()
for item := range mChan {
if onceErr != nil {
return nil, onceErr
}
collection = append(collection, item)
}
// TODO: For some reason sorting doesn't work, check out how list components is
// created, and if its possible to sort
// slices.SortStableFunc(collection, func(a, b list.Item) int {
// return cmp.Compare(a.(*tuiMusic).Name, b.(*tuiMusic).Name)
// })
return collection, nil
}
type extraKeyMap struct {
SeekBackward key.Binding
TogglePausePlay key.Binding
SeekForward key.Binding
TogglePlay key.Binding
}
func newExtraKeyMap() *extraKeyMap {
return &extraKeyMap{
SeekBackward: key.NewBinding(
key.WithKeys("i"),
key.WithHelp("i", "seek backwards"),
),
TogglePausePlay: key.NewBinding(
key.WithKeys("o", " "),
key.WithHelp("s/leader", "pause/resume"),
),
SeekForward: key.NewBinding(
key.WithKeys("p"),
key.WithHelp("p", "seek forward"),
),
TogglePlay: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "play music"),
),
}
}
func main() {
flag.Parse()
path := flag.Arg(0)
// path = ""
model, err := newModel(path)
if err != nil {
log.Fatal(err)
}
p := tea.NewProgram(model, tea.WithAltScreen())
p.SetWindowTitle("Sweet Clover")
if _, err = p.Run(); err != nil {
log.Fatal(err)
}
}