gomu/playlist.go

463 lines
8.5 KiB
Go
Raw Normal View History

2020-06-22 00:05:56 +08:00
// Copyright (C) 2020 Raziman
2020-06-19 16:22:20 +08:00
package main
import (
2020-06-23 22:27:02 +08:00
"fmt"
2020-06-19 16:22:20 +08:00
"io/ioutil"
2020-06-23 18:42:27 +08:00
"os"
2020-06-25 10:46:45 +08:00
"path"
2020-06-19 16:22:20 +08:00
"path/filepath"
2020-06-28 13:41:14 +08:00
"time"
2020-06-19 16:22:20 +08:00
"github.com/gdamore/tcell"
"github.com/rivo/tview"
2020-06-24 20:09:47 +08:00
"github.com/spf13/viper"
2020-06-19 16:22:20 +08:00
)
type AudioFile struct {
2020-06-19 16:42:30 +08:00
Name string
Path string
2020-06-19 16:22:20 +08:00
IsAudioFile bool
2020-07-01 17:47:52 +08:00
Length time.Duration
2020-06-19 16:42:30 +08:00
Parent *tview.TreeNode
2020-06-19 16:22:20 +08:00
}
2020-06-26 12:54:48 +08:00
type Playlist struct {
*tview.TreeView
2020-06-28 13:41:14 +08:00
prevNode *tview.TreeNode
2020-06-26 12:54:48 +08:00
}
2020-06-19 16:22:20 +08:00
2020-06-26 12:54:48 +08:00
func InitPlaylist() *Playlist {
2020-06-24 20:09:47 +08:00
2020-06-25 10:46:45 +08:00
rootDir, err := filepath.Abs(expandTilde(viper.GetString("music_dir")))
2020-06-19 16:22:20 +08:00
if err != nil {
2020-06-21 23:47:12 +08:00
log(err.Error())
2020-06-19 16:22:20 +08:00
}
2020-07-01 21:21:09 +08:00
root := tview.NewTreeNode(path.Base(rootDir)).
SetColor(accentColor)
2020-06-19 16:22:20 +08:00
tree := tview.NewTreeView().SetRoot(root)
2020-06-28 13:41:14 +08:00
2020-07-01 17:48:33 +08:00
playlist := &Playlist{tree, nil}
2020-06-28 13:41:14 +08:00
rootAudioFile := &AudioFile{
2020-07-01 17:48:33 +08:00
Name: root.GetText(),
Path: rootDir,
IsAudioFile: false,
Parent: nil,
2020-06-28 13:41:14 +08:00
}
root.SetReference(rootAudioFile)
2020-06-19 16:22:20 +08:00
2020-06-26 12:54:48 +08:00
playlist.SetTitle(" Playlist ").SetBorder(true)
2020-06-19 16:22:20 +08:00
2020-06-25 14:12:19 +08:00
populate(root, rootDir)
2020-06-19 16:22:20 +08:00
2020-06-25 14:12:19 +08:00
var firstChild *tview.TreeNode
2020-06-23 18:42:27 +08:00
2020-06-25 14:12:19 +08:00
if len(root.GetChildren()) == 0 {
firstChild = root
} else {
firstChild = root.GetChildren()[0]
}
2020-06-19 16:22:20 +08:00
2020-07-01 21:21:09 +08:00
// firstChild.SetColor(textColor)
// playlist.SetCurrentNode(firstChild)
2020-06-25 14:12:19 +08:00
// keep track of prev node so we can remove the color of highlight
2020-07-01 21:21:09 +08:00
// playlist.prevNode = firstChild.SetColor(accentColor)
playlist.SetHighlight(firstChild)
2020-06-19 16:22:20 +08:00
2020-06-26 12:54:48 +08:00
playlist.SetChangedFunc(func(node *tview.TreeNode) {
2020-07-01 21:21:09 +08:00
playlist.SetHighlight(node)
2020-06-25 14:12:19 +08:00
})
2020-06-23 18:42:27 +08:00
2020-06-26 12:54:48 +08:00
playlist.SetSelectedFunc(func(node *tview.TreeNode) {
node.SetExpanded(!node.IsExpanded())
})
2020-06-19 16:22:20 +08:00
2020-06-26 12:54:48 +08:00
playlist.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
currNode := playlist.GetCurrentNode()
if currNode == playlist.GetRoot() {
return e
}
audioFile := currNode.GetReference().(*AudioFile)
switch e.Rune() {
2020-06-28 13:41:14 +08:00
2020-07-01 17:47:52 +08:00
case 'a':
name, _ := pages.GetFrontPage()
if name != "mkdir-popup" {
CreatePlaylistPopup()
}
2020-06-28 13:41:14 +08:00
case 'D':
var selectedDir *AudioFile
// gets the parent dir if current focused node is not a dir
if audioFile.IsAudioFile {
selectedDir = audioFile.Parent.GetReference().(*AudioFile)
} else {
selectedDir = audioFile
}
confirmationPopup(
"Are you sure to delete this directory?", func(_ int, buttonName string) {
if buttonName == "no" {
2020-07-01 17:47:52 +08:00
pages.RemovePage("confirmation-popup")
app.SetFocus(prevPanel.(tview.Primitive))
2020-06-28 13:41:14 +08:00
return
}
err := os.RemoveAll(selectedDir.Path)
if err != nil {
2020-06-28 14:03:30 +08:00
timedPopup(
2020-06-28 13:41:14 +08:00
" Error ",
"Unable to delete dir "+selectedDir.Name, time.Second*5)
} else {
2020-06-28 14:03:30 +08:00
timedPopup(
2020-06-28 13:41:14 +08:00
" Success ",
selectedDir.Name+"\nhas been deleted successfully", time.Second*5)
playlist.Refresh()
}
pages.RemovePage("confirmation-popup")
app.SetFocus(prevPanel.(tview.Primitive))
})
case 'd':
// prevent from deleting a directory
if !audioFile.IsAudioFile {
return e
}
confirmationPopup(
"Are you sure to delete this audio file?", func(_ int, buttonName string) {
if buttonName == "no" {
2020-07-01 17:47:52 +08:00
pages.RemovePage("confirmation-popup")
app.SetFocus(prevPanel.(tview.Primitive))
2020-06-28 13:41:14 +08:00
return
}
err := os.Remove(audioFile.Path)
if err != nil {
2020-06-28 14:03:30 +08:00
timedPopup(
2020-06-28 13:41:14 +08:00
" Error ", "Unable to delete "+audioFile.Name, time.Second*5)
} else {
2020-06-28 14:03:30 +08:00
timedPopup(
2020-06-28 13:41:14 +08:00
" Success ",
audioFile.Name+"\nhas been deleted successfully", time.Second*5)
playlist.Refresh()
}
pages.RemovePage("confirmation-popup")
app.SetFocus(prevPanel.(tview.Primitive))
})
2020-06-27 17:16:49 +08:00
case 'Y':
if pages.HasPage("download-popup") {
pages.RemovePage("download-popup")
2020-06-28 13:41:14 +08:00
return e
}
2020-06-27 17:16:49 +08:00
2020-06-28 13:41:14 +08:00
// this ensures it downloads to
// the correct dir
if audioFile.IsAudioFile {
downloadMusic(audioFile.Parent)
} else {
downloadMusic(currNode)
2020-06-27 17:16:49 +08:00
}
2020-06-26 12:54:48 +08:00
case 'l':
playlist.addToQueue(audioFile)
currNode.SetExpanded(true)
case 'h':
// if closing node with no children
// close the node's parent
// remove the color of the node
if audioFile.IsAudioFile {
parent := audioFile.Parent
2020-07-01 21:21:09 +08:00
playlist.SetHighlight(parent)
2020-06-26 12:54:48 +08:00
parent.SetExpanded(false)
}
currNode.Collapse()
case 'L':
2020-06-26 17:09:15 +08:00
if !viper.GetBool("confirm_bulk_add") {
playlist.addAllToQueue(playlist.GetCurrentNode())
return e
}
2020-06-26 12:54:48 +08:00
confirmationPopup(
"Are you sure to add this whole directory into queue?",
func(_ int, label string) {
if label == "yes" {
playlist.addAllToQueue(playlist.GetCurrentNode())
}
pages.RemovePage("confirmation-popup")
app.SetFocus(playlist)
})
2020-07-01 17:47:52 +08:00
2020-06-26 12:54:48 +08:00
}
return e
2020-06-19 16:22:20 +08:00
})
2020-06-26 12:54:48 +08:00
return playlist
2020-06-19 16:22:20 +08:00
}
2020-07-01 21:21:09 +08:00
// add songs and their directories in Playlist panel
2020-06-19 16:22:20 +08:00
func populate(root *tview.TreeNode, rootPath string) {
files, err := ioutil.ReadDir(rootPath)
if err != nil {
2020-06-21 23:47:12 +08:00
log(err.Error())
2020-06-19 16:22:20 +08:00
}
for _, file := range files {
path := filepath.Join(rootPath, file.Name())
2020-06-23 18:42:27 +08:00
f, err := os.Open(path)
if err != nil {
log(err.Error())
}
defer f.Close()
2020-07-01 17:47:52 +08:00
child := tview.NewTreeNode(file.Name())
root.AddChild(child)
2020-06-23 18:42:27 +08:00
if !file.IsDir() {
filetype, err := GetFileContentType(f)
if err != nil {
log(err.Error())
}
// skip if not mp3 file
if filetype != "mpeg" {
continue
}
2020-07-01 17:47:52 +08:00
audioLength, err := GetLength(path)
2020-06-23 18:42:27 +08:00
2020-07-01 17:47:52 +08:00
if err != nil {
log(err.Error())
}
2020-06-19 16:22:20 +08:00
2020-07-01 17:47:52 +08:00
audioFile := &AudioFile{
Name: file.Name(),
Path: path,
IsAudioFile: true,
Length: audioLength,
Parent: root,
}
2020-06-19 16:22:20 +08:00
2020-07-01 17:47:52 +08:00
child.SetReference(audioFile)
}
2020-06-19 16:22:20 +08:00
if file.IsDir() {
2020-07-01 17:47:52 +08:00
audioFile := &AudioFile{
Name: file.Name(),
Path: path,
IsAudioFile: false,
Length: 0,
Parent: root,
}
child.SetReference(audioFile)
2020-07-01 21:21:09 +08:00
child.SetColor(accentColor)
2020-06-19 16:22:20 +08:00
populate(child, path)
2020-07-01 17:47:52 +08:00
2020-06-19 16:22:20 +08:00
}
}
}
2020-06-25 10:46:45 +08:00
2020-06-26 12:54:48 +08:00
// add to queue and update queue panel
2020-07-01 17:47:52 +08:00
func (p *Playlist) addToQueue(audioFile *AudioFile) {
2020-06-25 10:46:45 +08:00
2020-06-25 14:12:19 +08:00
if audioFile.IsAudioFile {
2020-06-25 10:46:45 +08:00
2020-06-25 14:12:19 +08:00
if !player.IsRunning {
player.IsRunning = true
2020-06-26 12:54:48 +08:00
go func() {
queue.AddItem("", audioFile.Path, 0, nil)
2020-06-25 14:12:19 +08:00
player.Run()
2020-06-26 12:54:48 +08:00
}()
2020-06-25 14:12:19 +08:00
2020-07-01 21:21:09 +08:00
return
2020-06-25 14:12:19 +08:00
2020-07-01 21:21:09 +08:00
}
2020-06-25 14:12:19 +08:00
2020-07-01 21:21:09 +08:00
songLength, err := GetLength(audioFile.Path)
2020-06-26 12:54:48 +08:00
2020-07-01 21:21:09 +08:00
if err != nil {
log(err.Error())
2020-06-26 12:54:48 +08:00
}
2020-07-01 21:21:09 +08:00
queueItemView := fmt.Sprintf("[ %s ] %s", fmtDuration(songLength), audioFile.Name)
queue.AddItem(queueItemView, audioFile.Path, 0, nil)
2020-06-25 10:46:45 +08:00
}
2020-06-25 14:12:19 +08:00
}
2020-06-26 12:54:48 +08:00
// bulk add a playlist to queue
2020-07-01 17:47:52 +08:00
func (p *Playlist) addAllToQueue(root *tview.TreeNode) {
2020-06-25 14:12:19 +08:00
var childrens []*tview.TreeNode
2020-06-25 10:46:45 +08:00
2020-06-25 14:12:19 +08:00
childrens = root.GetChildren()
2020-07-01 21:21:09 +08:00
// gets the parent if the highlighted item is a file
2020-06-25 14:12:19 +08:00
if len(childrens) == 0 {
childrens = root.GetReference().(*AudioFile).Parent.GetChildren()
2020-06-26 12:54:48 +08:00
}
2020-06-25 14:12:19 +08:00
for _, v := range childrens {
currNode := v.GetReference().(*AudioFile)
2020-06-27 17:16:49 +08:00
go playlist.addToQueue(currNode)
2020-06-25 14:12:19 +08:00
}
2020-06-25 10:46:45 +08:00
}
2020-06-28 13:41:14 +08:00
2020-07-01 17:47:52 +08:00
// refresh the playlist and read the whole root music dir
func (p *Playlist) Refresh() {
2020-06-28 13:41:14 +08:00
root := playlist.GetRoot()
prevFileName := playlist.GetCurrentNode().GetText()
root.ClearChildren()
populate(root, root.GetReference().(*AudioFile).Path)
root.Walk(func(node, parent *tview.TreeNode) bool {
// to preserve previously highlighted node
if node.GetReference().(*AudioFile).Name == prevFileName {
2020-07-01 21:21:09 +08:00
p.SetHighlight(node)
2020-06-28 13:41:14 +08:00
return false
}
return true
})
2020-07-01 17:47:52 +08:00
}
// adds child while setting reference to audio file
func (p *Playlist) AddSongToPlaylist(audioPath string, selPlaylist *tview.TreeNode) error {
f, err := os.Open(audioPath)
if err != nil {
return err
}
defer f.Close()
node := tview.NewTreeNode(path.Base(audioPath))
audioLength, err := GetLength(audioPath)
if err != nil {
return err
}
audioFile := &AudioFile{
Name: path.Base(audioPath),
Path: audioPath,
IsAudioFile: true,
Length: audioLength,
Parent: selPlaylist,
}
node.SetReference(audioFile)
selPlaylist.AddChild(node)
app.Draw()
return nil
}
// creates a directory under selected node, returns error if playlist exists
func (p *Playlist) CreatePlaylist(name string) error {
selectedNode := p.GetCurrentNode()
parentNode := selectedNode.GetReference().(*AudioFile).Parent
// if the current node is the root
// sets the parent to itself
if parentNode == nil {
parentNode = selectedNode
}
audioFile := parentNode.GetReference().(*AudioFile)
err := os.Mkdir(path.Join(audioFile.Path, name), 555)
if err != nil {
return err
}
p.Refresh()
return nil
2020-06-28 13:41:14 +08:00
}
2020-07-01 21:21:09 +08:00
// this is used to replace default behaviour of SetCurrentNode which
// adds color highlight attributes
func (p *Playlist) SetHighlight(currNode *tview.TreeNode) {
if p.prevNode != nil {
p.prevNode.SetColor(textColor)
}
currNode.SetColor(accentColor)
p.SetCurrentNode(currNode)
if currNode.GetReference().(*AudioFile).IsAudioFile {
p.prevNode = currNode
}
}