Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature]<history>: del music item #13

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cmd/terminal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions internal/storage/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
62 changes: 53 additions & 9 deletions internal/ui/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,35 @@ package ui

import (
"container/list"
"fmt"
"math"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"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

page int
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,
}
}

Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading