From 14309a484a598a346d4a5b54c052fe009a20fb64 Mon Sep 17 00:00:00 2001 From: o98k-ok Date: Tue, 12 Mar 2024 17:18:10 +0800 Subject: [PATCH] [feature]: del music item Signed-off-by: o98k-ok --- cmd/terminal/main.go | 8 ++--- internal/storage/local.go | 13 ++++++++ internal/ui/history.go | 62 +++++++++++++++++++++++++++++++++------ internal/ui/process.go | 2 +- 4 files changed, 71 insertions(+), 14 deletions(-) diff --git a/cmd/terminal/main.go b/cmd/terminal/main.go index 7d21b73..98393bc 100644 --- a/cmd/terminal/main.go +++ b/cmd/terminal/main.go @@ -24,13 +24,13 @@ func main() { []int{6, 30, 6, 12, 0}, ) processBar := ui.NewProcessLineElem(player) - historyList := ui.NewHistoryList(player, - []string{"标题", "描述", "时长", "BVID"}, - []int{40, 60, 12, 0}, + historyList := ui.NewHistoryList(player, localIndex, + []string{"标题", "描述", "时长", "BVID", "path"}, + []int{40, 60, 12, 0, 0}, ) elems := []ui.Element{processBar, inputElem, historyList} - menu := ui.NewMenuElem([]string{"🤓 当前", "😂 搜索", "😳 列表"}, elems) + menu := ui.NewMenuElem([]string{"⏯ 当前", "🔎 搜索", "📜 列表"}, elems) framework := ui.NewFramework(menu, elems) program := tea.NewProgram(framework) diff --git a/internal/storage/local.go b/internal/storage/local.go index 9126556..79ee523 100644 --- a/internal/storage/local.go +++ b/internal/storage/local.go @@ -14,6 +14,7 @@ type Storage interface { SaveMusic(music music.MusicKey) error HistoryMusics() chan music.MusicKey GetRootPath() string + DelMusic(music music.MusicKey) error } type LocalFileStorage struct { @@ -39,6 +40,18 @@ func (ls *LocalFileStorage) SaveMusic(music music.MusicKey) error { return os.WriteFile(fmt.Sprintf("%s/%x.raw", ls.Root, md5.Sum(key)), key, 0644) } +func (ls *LocalFileStorage) DelMusic(music music.MusicKey) error { + if len(ls.Root) == 0 { + return nil + } + + key, _ := json.Marshal(music) + path := fmt.Sprintf("%s/%x.raw", ls.Root, md5.Sum(key)) + os.Remove(path) + os.Remove(music.LocalPath) + return nil +} + func (ls *LocalFileStorage) HistoryMusics() chan music.MusicKey { chans := make(chan music.MusicKey, 4) fdir, err := os.ReadDir(ls.Root) diff --git a/internal/ui/history.go b/internal/ui/history.go index 0a4f171..91b195c 100644 --- a/internal/ui/history.go +++ b/internal/ui/history.go @@ -2,6 +2,7 @@ package ui import ( "container/list" + "fmt" "math" tea "github.com/charmbracelet/bubbletea" @@ -9,11 +10,13 @@ import ( "github.com/o98k-ok/voice/internal/music" "github.com/o98k-ok/voice/internal/pkg" "github.com/o98k-ok/voice/internal/player" + "github.com/o98k-ok/voice/internal/storage" ) type HistoryList struct { list *ListElem player *player.VoicePlayer + storage storage.Storage active bool current int @@ -21,12 +24,13 @@ type HistoryList struct { limit int } -func NewHistoryList(player *player.VoicePlayer, headers []string, widths []int) *HistoryList { +func NewHistoryList(player *player.VoicePlayer, index storage.Storage, headers []string, widths []int) *HistoryList { return &HistoryList{ - list: NewListElem(headers, widths, nil), - player: player, - page: 1, - limit: 10, + list: NewListElem(headers, widths, nil), + player: player, + page: 1, + limit: 10, + storage: index, } } @@ -35,8 +39,13 @@ func (hl *HistoryList) Init() tea.Cmd { } func (hl *HistoryList) View() string { - help := "enter play • ↓/j move down • ↑/k move up\n ← page left • → page right • tab next menu" - return lipgloss.JoinVertical(lipgloss.Center, hl.list.View(), "\n", help) + help := "enter play • ↓/j move down • ↑/k move up\n backspace del music • ← page left • → page right" + + size := math.Ceil(float64(hl.player.PlayList.Len()) / float64(hl.limit)) + pageInfo := fmt.Sprintf("%d • %d/%d页", hl.list.table.Cursor()+1, hl.page, int(size)) + + v := lipgloss.JoinVertical(lipgloss.Right, hl.list.View(), " ", pageInfo) + return lipgloss.JoinVertical(lipgloss.Center, v, "\n", help) } func (hl *HistoryList) fechByBvID(bvID string, limit int) [][]string { @@ -57,7 +66,7 @@ func (hl *HistoryList) fechByBvID(bvID string, limit int) [][]string { hl.current = i hl.page = page } - values = append(values, []string{m.Name, m.Desc, m.Duration, m.BvID}) + values = append(values, []string{m.Name, m.Desc, m.Duration, m.BvID, m.LocalPath}) p = p.Next() } if got || p == nil { @@ -80,7 +89,7 @@ func (hl *HistoryList) fechList(off, limit int) [][]string { break } m := p.Value.(*music.Music) - values = append(values, []string{m.Name, m.Desc, m.Duration, m.BvID}) + values = append(values, []string{m.Name, m.Desc, m.Duration, m.BvID, m.LocalPath}) p = p.Next() } if page >= off || p == nil { @@ -105,6 +114,41 @@ func (hl *HistoryList) MsgKeyBindings() map[string]map[string]func(interface{}) hl.list.table.SetCursor(hl.current) return nil }, + "backspace": func(i interface{}) tea.Cmd { + if !hl.active { + return nil + } + + m := hl.list.table.SelectedRow() + hl.storage.DelMusic(music.MusicKey{ + Name: m[0], + Desc: m[1], + BVID: m[3], + Duration: m[2], + LocalPath: m[4], + }) + + p := hl.player.PlayList.Front() + for ; p != nil; p = p.Next() { + if p.Value.(*music.Music).BvID == m[3] { + break + } + } + hl.player.PlayList.Remove(p) + + // 刷新列表数据 + size := math.Ceil(float64(hl.player.PlayList.Len()) / float64(hl.limit)) + if hl.page > int(size) { + hl.page -= 1 + } + hl.list.ResetList(hl.fechList(hl.page, hl.limit)) + + // 如果正在播放,就下一首 + if p == hl.player.CurrentElem { + hl.player.Next() + } + return nil + }, "enter": func(i interface{}) tea.Cmd { if !hl.active || len(hl.list.table.Rows()) == 0 { return nil diff --git a/internal/ui/process.go b/internal/ui/process.go index c7e2697..e8f5511 100644 --- a/internal/ui/process.go +++ b/internal/ui/process.go @@ -44,7 +44,7 @@ func (pe *ProceeLineElem) View() string { pkg.RenderWithWidth(music.Name, MaxWindowSize*0.6), "\n", pkg.RenderWithWidth(music.Desc, MaxWindowSize*0.6), "\n", pe.progress.ViewAs(pe.progress.Percent())+" "+music.DurationRate(), "\n", - lipgloss.NewStyle().Bold(true).Render("p prev • space pause/play • n next")) + lipgloss.NewStyle().Bold(true).Render("tab next menu • p prev • space pause/play • n next")) } func (pe *ProceeLineElem) MsgKeyBindings() map[string]map[string]func(interface{}) tea.Cmd {