-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaylist.go
More file actions
386 lines (343 loc) · 9.7 KB
/
Copy pathplaylist.go
File metadata and controls
386 lines (343 loc) · 9.7 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package main
import (
"fmt"
"math"
"os"
"path/filepath"
"sort"
"syscall"
"time"
"bm/search"
"github.com/gopxl/beep/v2/speaker"
"github.com/mattn/go-runewidth"
"golang.org/x/term"
)
// PlayList displays the list of songs to be played.
//
// PlayList 显示要播放的歌曲列表。
type PlayList struct {
app *App
cursor int // The UI cursor on the viewPlaylist. / UI在viewPlaylist上的光标。
offset int
isSearching bool
searchQuery string
viewPlaylist []string // The filtered playlist to be displayed. / 要显示的已过滤播放列表。
originalIndices []int // Map from viewPlaylist index to app.Playlist index. / 从viewPlaylist索引到app.Playlist索引的映射。
searchEngine *search.Engine
// Debounce mechanism to prevent accidental rapid removal of the current song.
// 防抖机制,防止快速连续移除当前播放歌曲。
lastRemoveTime time.Time
}
// NewPlayList creates a new instance of PlayList.
//
// NewPlayList 创建一个新的 PlayList 实例。
func NewPlayList(app *App) *PlayList {
return &PlayList{
app: app,
isSearching: false,
searchQuery: "",
viewPlaylist: make([]string, 0),
originalIndices: make([]int, 0),
searchEngine: search.New(),
}
}
// Init prepares the initial view for the playlist.
//
// Init 准备播放列表的初始视图。
func (p *PlayList) Init() {
p.filterPlaylist()
}
// filterPlaylist updates the viewPlaylist based on the searchQuery.
//
// filterPlaylist 根据 searchQuery 更新 viewPlaylist。
func (p *PlayList) filterPlaylist() {
p.viewPlaylist = make([]string, 0)
p.originalIndices = make([]int, 0)
if p.searchQuery == "" {
p.viewPlaylist = p.app.Playlist
for i := range p.app.Playlist {
p.originalIndices = append(p.originalIndices, i)
}
} else {
type scoredSong struct {
path string
score float64
index int
}
var scoredSongs []scoredSong
p.searchEngine.BuildFromPaths(p.app.Playlist)
for i, songPath := range p.app.Playlist {
songName := filepath.Base(songPath)
score := p.searchEngine.Match(p.searchQuery, songName)
if score > 0 {
scoredSongs = append(scoredSongs, scoredSong{
path: songPath,
score: score,
index: i,
})
}
}
sort.Slice(scoredSongs, func(i, j int) bool {
if math.Abs(scoredSongs[i].score-scoredSongs[j].score) > 0.0001 {
return scoredSongs[i].score > scoredSongs[j].score
}
return scoredSongs[i].index < scoredSongs[j].index
})
for _, scored := range scoredSongs {
p.viewPlaylist = append(p.viewPlaylist, scored.path)
p.originalIndices = append(p.originalIndices, scored.index)
}
}
p.cursor = 0
p.offset = 0
}
// HandleKey handles user input for the playlist.
//
// HandleKey 处理播放列表的用户输入。
func (p *PlayList) HandleKey(key rune) (Page, bool, error) {
if p.isSearching {
if IsKey(key, GlobalConfig.Keymap.Playlist.SearchMode.ConfirmSearch) {
p.isSearching = false
} else if IsKey(key, GlobalConfig.Keymap.Playlist.SearchMode.EscapeSearch) {
p.isSearching = false
p.searchQuery = ""
p.filterPlaylist()
} else if IsKey(key, GlobalConfig.Keymap.Playlist.SearchMode.SearchBackspace) {
if len(p.searchQuery) > 0 {
runes := []rune(p.searchQuery)
p.searchQuery = string(runes[:len(runes)-1])
p.filterPlaylist()
}
} else if key == KeyArrowUp || key == KeyArrowDown || key == KeyArrowLeft || key == KeyArrowRight {
p.isSearching = false
} else {
if key >= 32 {
p.searchQuery += string(key)
p.filterPlaylist()
}
}
p.View()
return nil, false, nil
}
needRedraw := true
if IsKey(key, GlobalConfig.Keymap.Playlist.SearchMode.EscapeSearch) {
if p.searchQuery != "" {
p.searchQuery = ""
p.filterPlaylist()
}
} else if IsKey(key, GlobalConfig.Keymap.Playlist.Search) {
p.isSearching = true
} else if IsKey(key, GlobalConfig.Keymap.Playlist.PlaySong) {
if len(p.viewPlaylist) > 0 && p.cursor >= 0 && p.cursor < len(p.viewPlaylist) {
songPath := p.viewPlaylist[p.cursor]
if err := p.app.PlaySong(songPath); err != nil {
// Handle error
}
}
needRedraw = false
} else if IsKey(key, GlobalConfig.Keymap.Playlist.NavUp) {
if len(p.viewPlaylist) > 0 {
p.cursor = (p.cursor - 1 + len(p.viewPlaylist)) % len(p.viewPlaylist)
}
} else if IsKey(key, GlobalConfig.Keymap.Playlist.NavDown) {
if len(p.viewPlaylist) > 0 {
p.cursor = (p.cursor + 1) % len(p.viewPlaylist)
}
} else if IsKey(key, GlobalConfig.Keymap.Playlist.RemoveSong) {
oldCursor := p.cursor
p.removeCurrentSong()
if oldCursor < len(p.viewPlaylist) {
p.cursor = oldCursor
} else if len(p.viewPlaylist) > 0 {
p.cursor = len(p.viewPlaylist) - 1
}
} else {
needRedraw = false
}
if needRedraw {
p.View()
}
return nil, false, nil
}
// removeCurrentSong removes the song at the current cursor position.
//
// removeCurrentSong 删除当前光标位置的歌曲。
func (p *PlayList) removeCurrentSong() {
if p.cursor < 0 || p.cursor >= len(p.viewPlaylist) {
return
}
originalIndex := p.originalIndices[p.cursor]
songPath := p.app.Playlist[originalIndex]
wasPlayingSong := (p.app.currentSongPath == songPath)
if wasPlayingSong {
currentTime := time.Now()
debounceMs := GlobalConfig.App.SwitchDebounceMs
if debounceMs == 0 {
debounceMs = 200
}
if currentTime.Sub(p.lastRemoveTime) < time.Duration(debounceMs)*time.Millisecond {
return
}
p.lastRemoveTime = currentTime
}
p.app.Playlist = append(p.app.Playlist[:originalIndex], p.app.Playlist[originalIndex+1:]...)
if err := SavePlaylist(p.app.Playlist, p.app.LibraryPath); err != nil {
l.Warnf("failed to save playlist: %v\n\n警告: 保存播放列表失败: %v", err, err)
}
p.app.removeFromPlayHistory(songPath)
for _, page := range p.app.pages {
if libPage, ok := page.(*Library); ok {
delete(libPage.selected, songPath)
if libPage.searchQuery != "" {
libPage.filterSongs()
}
break
}
}
p.filterPlaylist()
if len(p.app.Playlist) == 0 {
p.stopPlaybackAndShowEmptyState()
} else if wasPlayingSong {
nextIndex := originalIndex
if nextIndex >= len(p.app.Playlist) {
nextIndex = len(p.app.Playlist) - 1
}
p.app.PlaySongWithSwitchAndRender(p.app.Playlist[nextIndex], false, false)
}
}
// stopPlaybackAndShowEmptyState stops playback and shows an empty state for the player page.
//
// stopPlaybackAndShowEmptyState 停止播放并显示播放器页面的空状态。
func (p *PlayList) stopPlaybackAndShowEmptyState() {
if p.app.player != nil {
speaker.Lock()
p.app.player.ctrl.Paused = true
speaker.Unlock()
}
p.app.player = nil
p.app.setCurrentSong("")
if p.app.mprisServer != nil {
p.app.mprisServer.StopService()
p.app.mprisServer = nil
}
if playerPage, ok := p.app.pages[0].(*PlayerPage); ok {
playerPage.UpdateSong("")
}
}
// HandleSignal redraws the view on resize.
//
// HandleSignal 在调整大小时重绘视图。
func (p *PlayList) HandleSignal(sig os.Signal) error {
if sig == syscall.SIGWINCH {
p.View()
}
return nil
}
// View renders the playlist.
//
// View 渲染播放列表。
func (p *PlayList) View() {
w, h, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
w, h = 80, 24
}
fmt.Print("\x1b[2J\x1b[3J\x1b[H")
title := "PlayList"
titleX := (w - len(title)) / 2
fmt.Printf("\x1b[1;%dH\x1b[1m%s\x1b[0m", titleX, title)
listHeight := h - 4
var footer string
if p.isSearching || p.searchQuery != "" {
footer = fmt.Sprintf("Search: %s", p.searchQuery)
} else {
footer = ""
}
if len(footer) > w {
footer = "..." + footer[len(footer)-w+3:]
}
footerX := (w - len(footer)) / 2
if footerX < 1 {
footerX = 1
}
fmt.Printf("\x1b[%d;%dH\x1b[90m%s\x1b[0m", h, footerX, footer)
if p.isSearching {
cursorX := footerX + len("Search: ") + len(p.searchQuery)
if cursorX <= w {
fmt.Printf("\x1b[%d;%dH█", h, cursorX)
}
}
if len(p.viewPlaylist) == 0 {
msg := "PlayList is empty"
if p.searchQuery != "" {
msg = "No songs match your search"
}
msg2 := "Add songs from the Library tab"
msgX := (w - len(msg)) / 2
msg2X := (w - len(msg2)) / 2
centerRow := h / 2
fmt.Printf("\x1b[%d;%dH\x1b[90m%s\x1b[0m", centerRow-1, msgX, msg)
if p.searchQuery == "" {
fmt.Printf("\x1b[%d;%dH\x1b[90m%s\x1b[0m", centerRow+1, msg2X, msg2)
}
return
}
if p.cursor < p.offset {
p.offset = p.cursor
}
if p.cursor >= p.offset+listHeight {
p.offset = p.cursor - listHeight + 1
}
for i := range listHeight {
trackIndex := p.offset + i
if trackIndex >= len(p.viewPlaylist) {
break
}
trackPath := p.viewPlaylist[trackIndex]
trackName := filepath.Base(trackPath)
style := "\x1b[32m"
if trackPath == p.app.currentSongPath {
style = "\x1b[31m"
}
if p.app.IsFileCorrupted(trackPath) {
style = "\x1b[33m"
}
if trackIndex == p.cursor {
style += "\x1b[7m"
}
prefix := "✓"
if p.app.IsFileCorrupted(trackPath) {
prefix = "⚠"
}
line := fmt.Sprintf("%s %s", prefix, trackName)
if runewidth.StringWidth(line) > w-1 {
for runewidth.StringWidth(line) > w-1 && len(line) > 0 {
line = line[:len(line)-1]
}
}
fmt.Printf("\x1b[%d;1H\x1b[K%s%s\x1b[0m", i+3, style, line)
}
totalItems := len(p.viewPlaylist)
if totalItems > listHeight {
thumbSize := listHeight * listHeight / totalItems
if thumbSize < 1 {
thumbSize = 1
}
scrollRange := totalItems - listHeight
thumbRange := listHeight - thumbSize
thumbStart := 0
if scrollRange > 0 {
thumbStart = p.offset * thumbRange / scrollRange
}
for i := range listHeight {
if i >= thumbStart && i < thumbStart+thumbSize {
fmt.Printf("\x1b[%d;%dH┃", i+3, w)
} else {
fmt.Printf("\x1b[%d;%dH│", i+3, w)
}
}
}
}
// Tick for PlayList does nothing, as it's event-driven.
//
// PlayList的Tick方法不执行任何操作,因为它是事件驱动的。
func (p *PlayList) Tick() {}