gomu/playlist.go

781 lines
16 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-07-03 11:17:45 +08:00
"bytes"
2020-06-23 22:27:02 +08:00
"fmt"
2020-06-19 16:22:20 +08:00
"io/ioutil"
2020-08-11 13:39:02 +08:00
"log"
2020-06-23 18:42:27 +08:00
"os"
2020-07-03 11:17:45 +08:00
"os/exec"
2020-06-25 10:46:45 +08:00
"path"
2020-06-19 16:22:20 +08:00
"path/filepath"
2020-07-22 21:01:13 +08:00
"strings"
2020-06-28 13:41:14 +08:00
"time"
2020-06-19 16:22:20 +08:00
2021-01-21 01:30:16 +08:00
"github.com/gdamore/tcell/v2"
2020-06-19 16:22:20 +08:00
"github.com/rivo/tview"
2020-07-24 22:27:08 +08:00
spin "github.com/tj/go-spin"
2020-07-21 12:22:00 +08:00
"github.com/ztrue/tracerr"
2020-06-19 16:22:20 +08:00
)
// AudioFile is representing directories and mp3 files
2020-07-23 15:34:56 +08:00
// if isAudioFile equals to false it is a directory
2020-06-19 16:22:20 +08:00
type AudioFile struct {
2020-07-23 15:15:39 +08:00
name string
path string
isAudioFile bool
length time.Duration
node *tview.TreeNode
parent *tview.TreeNode
2020-06-19 16:22:20 +08:00
}
2020-07-25 22:53:54 +08:00
// Playlist struct represents playlist panel
// that shows the tree of the music directory
2020-06-26 12:54:48 +08:00
type Playlist struct {
*tview.TreeView
2020-07-24 22:27:08 +08:00
prevNode *tview.TreeNode
defaultTitle string
// number of downloads
download int
done chan struct{}
2020-06-26 12:54:48 +08:00
}
2020-06-19 16:22:20 +08:00
var (
2021-02-02 22:52:37 +08:00
yankFile *AudioFile
isYanked bool
)
2020-07-23 15:15:39 +08:00
func (p *Playlist) help() []string {
2020-07-17 15:34:50 +08:00
return []string{
"j down",
"k up",
"h close node",
2020-08-15 11:25:57 +08:00
"a create a playlist",
2020-07-17 15:34:50 +08:00
"l add song to queue",
"L add playlist to queue",
"d delete file from filesystem",
"D delete playlist from filesystem",
2021-02-04 21:27:17 +08:00
"Y download audio from url",
2020-07-17 15:34:50 +08:00
"r refresh",
2020-08-18 16:09:43 +08:00
"R rename",
2021-02-02 22:52:37 +08:00
"y yank file",
"p paste file",
2020-07-24 23:06:34 +08:00
"/ find in playlist",
"s search audio from youtube",
2020-07-17 15:34:50 +08:00
}
}
2020-07-24 22:27:08 +08:00
// newPlaylist returns new instance of playlist and runs populate function
// on root music directory.
2020-08-11 13:39:02 +08:00
func newPlaylist(args Args) *Playlist {
2020-06-24 20:09:47 +08:00
m, err := gomu.env.Get("music_dir")
if err != nil {
log.Fatal(err)
}
rootDir, err := filepath.Abs(expandTilde(m.(string)))
2020-06-19 16:22:20 +08:00
2020-08-11 13:39:02 +08:00
// if not default value was given
if *args.music != "~/music" {
rootDir = expandFilePath(*args.music)
}
2020-06-19 16:22:20 +08:00
if err != nil {
2020-08-11 13:39:02 +08:00
log.Fatalf("Unable to find music directory: %e", err)
2020-06-19 16:22:20 +08:00
}
var rootTextView string
useEmoji, err := gomu.env.Get("use_emoji")
if err != nil {
log.Fatal(err)
}
if useEmoji.(bool) {
emojiPlaylist, err := gomu.env.Get("emoji_playlist")
if err != nil {
log.Fatal(err)
}
rootTextView =
fmt.Sprintf("%s %s", emojiPlaylist.(string), path.Base(rootDir))
} else {
rootTextView = path.Base(rootDir)
}
2020-08-25 22:05:17 +08:00
root := tview.NewTreeNode(rootTextView).
2020-08-22 14:41:03 +08:00
SetColor(gomu.colors.accent)
2020-06-19 16:22:20 +08:00
tree := tview.NewTreeView().SetRoot(root)
2020-06-28 13:41:14 +08:00
2020-07-17 15:34:50 +08:00
playlist := &Playlist{
2020-07-24 22:27:08 +08:00
TreeView: tree,
defaultTitle: "─ Playlist ──┤ 0 downloads ├",
done: make(chan struct{}),
2020-07-17 15:34:50 +08:00
}
2020-06-28 13:41:14 +08:00
rootAudioFile := &AudioFile{
2020-08-25 22:05:17 +08:00
name: path.Base(rootDir),
2020-07-23 15:15:39 +08:00
node: root,
path: rootDir,
2020-06-28 13:41:14 +08:00
}
root.SetReference(rootAudioFile)
2020-06-19 16:22:20 +08:00
2020-07-10 22:05:36 +08:00
playlist.
2020-07-24 22:27:08 +08:00
SetTitle(playlist.defaultTitle).
2020-07-10 22:05:36 +08:00
SetBorder(true).
2020-08-25 21:12:14 +08:00
SetTitleAlign(tview.AlignLeft).
SetBorderPadding(0, 0, 1, 1)
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-23 15:15:39 +08:00
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-23 15:15:39 +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 {
cmds := map[rune]string{
'a': "create_playlist",
'D': "delete_playlist",
'd': "delete_file",
'Y': "download_audio",
's': "youtube_search",
'l': "add_queue",
'L': "bulk_add",
'h': "close_node",
'r': "refresh",
'R': "rename",
2021-02-02 22:52:37 +08:00
'y': "yank",
'p': "paste",
'/': "playlist_search",
}
2020-07-01 17:47:52 +08:00
for key, cmd := range cmds {
if e.Rune() != key {
continue
2020-07-01 17:47:52 +08:00
}
fn, err := gomu.command.getFn(cmd)
2020-07-20 21:48:13 +08:00
if err != nil {
errorPopup(err)
2020-06-28 13:41:14 +08:00
return e
}
fn()
}
2020-06-28 13:41:14 +08:00
// disable default key handler for space
if e.Rune() == ' ' {
return nil
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
}
// Returns the current file highlighted in the playlist
func (p Playlist) getCurrentFile() *AudioFile {
node := p.GetCurrentNode()
if node == nil {
return nil
}
return node.GetReference().(*AudioFile)
}
2020-07-23 15:34:56 +08:00
// Deletes song from filesystem
2020-07-23 15:15:39 +08:00
func (p *Playlist) deleteSong(audioFile *AudioFile) (err error) {
2020-07-20 21:48:13 +08:00
confirmationPopup(
"Are you sure to delete this audio file?", func(_ int, buttonName string) {
2020-07-22 21:01:13 +08:00
if buttonName == "no" || buttonName == "" {
2020-07-20 21:48:13 +08:00
return
}
audioName := getName(audioFile.path)
2020-07-23 15:15:39 +08:00
err := os.Remove(audioFile.path)
2020-07-20 21:48:13 +08:00
if err != nil {
2020-08-25 20:45:16 +08:00
defaultTimedPopup(" Error ", "Unable to delete "+audioFile.name)
2020-07-20 21:48:13 +08:00
2020-07-21 12:22:00 +08:00
err = tracerr.Wrap(err)
2020-07-20 21:48:13 +08:00
} else {
2020-08-25 20:45:16 +08:00
defaultTimedPopup(" Success ",
audioFile.name+"\nhas been deleted successfully")
2020-07-23 15:15:39 +08:00
p.refresh()
//Here we remove the song from queue
songPaths := gomu.queue.getItems()
if audioName == getName(gomu.player.currentSong.name) {
gomu.player.skip()
}
for i, songPath := range songPaths {
if strings.Contains(songPath, audioName) {
gomu.queue.deleteItem(i)
}
}
2020-07-20 21:48:13 +08:00
}
2020-07-21 01:21:59 +08:00
})
2020-07-20 21:48:13 +08:00
return nil
}
// Deletes playlist/dir from filesystem
2020-07-23 15:15:39 +08:00
func (p *Playlist) deletePlaylist(audioFile *AudioFile) (err error) {
2020-07-20 21:48:13 +08:00
var selectedDir *AudioFile
// gets the parent dir if current focused node is not a dir
2020-07-23 15:15:39 +08:00
if audioFile.isAudioFile {
selectedDir = audioFile.parent.GetReference().(*AudioFile)
2020-07-20 21:48:13 +08:00
} else {
selectedDir = audioFile
}
2020-07-21 01:21:59 +08:00
confirmationPopup("Are you sure to delete this directory?",
2020-07-20 21:48:13 +08:00
func(_ int, buttonName string) {
2020-07-22 21:01:13 +08:00
if buttonName == "no" || buttonName == "" {
2020-07-20 21:48:13 +08:00
return
}
2020-07-23 15:15:39 +08:00
err := os.RemoveAll(selectedDir.path)
2020-07-20 21:48:13 +08:00
if err != nil {
2020-08-25 20:45:16 +08:00
defaultTimedPopup(
2020-07-20 21:48:13 +08:00
" Error ",
2020-08-25 20:45:16 +08:00
"Unable to delete dir "+selectedDir.name)
2020-07-20 21:48:13 +08:00
2020-07-21 12:22:00 +08:00
err = tracerr.Wrap(err)
2020-07-20 21:48:13 +08:00
} else {
2020-08-25 20:45:16 +08:00
defaultTimedPopup(
2020-07-20 21:48:13 +08:00
" Success ",
2020-08-25 20:45:16 +08:00
selectedDir.name+"\nhas been deleted successfully")
2020-07-20 21:48:13 +08:00
2020-07-23 15:15:39 +08:00
p.refresh()
2020-07-20 21:48:13 +08:00
}
2020-07-21 01:21:59 +08:00
})
2020-07-20 21:48:13 +08:00
return nil
}
// Bulk add a playlist to queue
2020-07-23 15:15:39 +08:00
func (p *Playlist) addAllToQueue(root *tview.TreeNode) {
2020-06-25 14:12:19 +08:00
var childrens []*tview.TreeNode
childrens = root.GetChildren()
2020-07-01 21:21:09 +08:00
// gets the parent if the highlighted item is a file
2020-07-23 15:15:39 +08:00
if root.GetReference().(*AudioFile).isAudioFile {
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)
2021-02-02 22:52:37 +08:00
if currNode.isAudioFile {
if currNode != gomu.player.currentSong {
gomu.queue.enqueue(currNode)
}
2021-02-02 22:52:37 +08:00
}
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-23 15:34:56 +08:00
// Refreshes the playlist and read the whole root music dir
2020-07-23 15:15:39 +08:00
func (p *Playlist) refresh() {
2020-06-28 13:41:14 +08:00
2020-07-23 15:15:39 +08:00
root := gomu.playlist.GetRoot()
2020-06-28 13:41:14 +08:00
2020-07-23 15:15:39 +08:00
prevFileName := gomu.playlist.GetCurrentNode().GetText()
2020-06-28 13:41:14 +08:00
root.ClearChildren()
2020-07-23 15:15:39 +08:00
populate(root, root.GetReference().(*AudioFile).path)
2020-06-28 13:41:14 +08:00
2021-02-04 12:23:32 +08:00
root.Walk(func(node, _ *tview.TreeNode) bool {
2020-06-28 13:41:14 +08:00
// to preserve previously highlighted node
2020-07-13 14:33:29 +08:00
if node.GetText() == prevFileName {
2020-07-23 15:15:39 +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
2020-07-23 15:15:39 +08:00
func (p *Playlist) addSongToPlaylist(
2020-07-20 21:48:13 +08:00
audioPath string, selPlaylist *tview.TreeNode,
) error {
2020-07-01 17:47:52 +08:00
f, err := os.Open(audioPath)
if err != nil {
2020-07-21 12:22:00 +08:00
return tracerr.Wrap(err)
2020-07-01 17:47:52 +08:00
}
defer f.Close()
2020-08-11 18:31:33 +08:00
songName := getName(audioPath)
node := tview.NewTreeNode(songName)
2020-07-01 17:47:52 +08:00
2020-07-23 15:15:39 +08:00
audioLength, err := getLength(audioPath)
2020-07-01 17:47:52 +08:00
if err != nil {
2020-07-21 12:22:00 +08:00
return tracerr.Wrap(err)
2020-07-01 17:47:52 +08:00
}
audioFile := &AudioFile{
name: songName,
2020-07-23 15:15:39 +08:00
path: audioPath,
isAudioFile: true,
length: audioLength,
node: node,
2020-07-23 15:15:39 +08:00
parent: selPlaylist,
2020-07-01 17:47:52 +08:00
}
displayText := setDisplayText(songName)
2020-08-11 18:31:33 +08:00
2020-07-01 17:47:52 +08:00
node.SetReference(audioFile)
2020-08-11 18:31:33 +08:00
node.SetText(displayText)
2020-07-01 17:47:52 +08:00
selPlaylist.AddChild(node)
return nil
}
2020-07-22 21:00:14 +08:00
// Gets all audio files walks from music root directory
2020-07-23 15:15:39 +08:00
func (p *Playlist) getAudioFiles() []*AudioFile {
2020-07-10 22:05:36 +08:00
2020-07-10 16:52:37 +08:00
root := p.GetRoot()
audioFiles := []*AudioFile{}
root.Walk(func(node, _ *tview.TreeNode) bool {
audioFile := node.GetReference().(*AudioFile)
audioFiles = append(audioFiles, audioFile)
2020-07-10 22:05:36 +08:00
2020-07-10 16:52:37 +08:00
return true
})
return audioFiles
}
// Creates a directory under selected node, returns error if playlist exists
2020-07-23 15:15:39 +08:00
func (p *Playlist) createPlaylist(name string) error {
2020-07-01 17:47:52 +08:00
selectedNode := p.GetCurrentNode()
2020-07-23 15:15:39 +08:00
parentNode := selectedNode.GetReference().(*AudioFile).parent
2020-07-01 17:47:52 +08:00
// if the current node is the root
// sets the parent to itself
if parentNode == nil {
parentNode = selectedNode
}
audioFile := parentNode.GetReference().(*AudioFile)
2020-07-23 15:15:39 +08:00
err := os.Mkdir(path.Join(audioFile.path, name), 0744)
2020-07-01 17:47:52 +08:00
if err != nil {
2020-07-21 12:22:00 +08:00
return tracerr.Wrap(err)
2020-07-01 17:47:52 +08:00
}
2020-07-23 15:15:39 +08:00
p.refresh()
2020-07-01 17:47:52 +08:00
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
2020-07-01 21:21:09 +08:00
// adds color highlight attributes
2020-07-23 15:15:39 +08:00
func (p *Playlist) setHighlight(currNode *tview.TreeNode) {
2020-07-01 21:21:09 +08:00
if p.prevNode != nil {
2021-01-21 10:30:01 +08:00
p.prevNode.SetColor(gomu.colors.background)
2020-07-01 21:21:09 +08:00
}
2020-08-22 14:41:03 +08:00
currNode.SetColor(gomu.colors.accent)
2020-07-01 21:21:09 +08:00
p.SetCurrentNode(currNode)
2020-07-23 15:15:39 +08:00
if currNode.GetReference().(*AudioFile).isAudioFile {
2020-07-01 21:21:09 +08:00
p.prevNode = currNode
}
}
2020-07-03 11:17:45 +08:00
2020-07-06 18:58:27 +08:00
// Traverses the playlist and finds the AudioFile struct
2020-07-10 16:52:37 +08:00
// audioName must be hashed with sha1 first
2020-07-23 15:15:39 +08:00
func (p *Playlist) findAudioFile(audioName string) (*AudioFile, error) {
2020-07-06 18:58:27 +08:00
root := p.GetRoot()
var selNode *AudioFile
root.Walk(func(node, _ *tview.TreeNode) bool {
audioFile := node.GetReference().(*AudioFile)
2020-07-23 15:15:39 +08:00
hashed := sha1Hex(getName(audioFile.name))
2020-07-10 16:52:37 +08:00
if hashed == audioName {
2020-07-06 18:58:27 +08:00
selNode = audioFile
return false
}
return true
})
2020-07-21 12:22:00 +08:00
if selNode == nil {
return nil, tracerr.New("no matching audio name")
}
2020-07-06 18:58:27 +08:00
2020-07-21 12:22:00 +08:00
return selNode, nil
2020-07-06 18:58:27 +08:00
}
2020-07-03 11:17:45 +08:00
2020-08-18 16:09:43 +08:00
func (p *Playlist) rename(newName string) error {
currentNode := p.GetCurrentNode()
audio := currentNode.GetReference().(*AudioFile)
pathToFile, _ := filepath.Split(audio.path)
var newPath string
if audio.isAudioFile {
newPath = pathToFile + newName + ".mp3"
} else {
newPath = pathToFile + newName
}
err := os.Rename(audio.path, newPath)
if err != nil {
return tracerr.Wrap(err)
}
2021-02-02 22:52:37 +08:00
audio.path = newPath
2021-02-03 14:53:46 +08:00
gomu.queue.saveQueue(false)
2021-02-02 22:52:37 +08:00
gomu.queue.clearQueue()
gomu.queue.loadQueue()
2020-08-18 16:09:43 +08:00
return nil
}
2020-07-25 22:53:54 +08:00
// updateTitle creates a spinning motion on the title
// of the playlist panel when downloading.
2020-07-24 22:27:08 +08:00
func (p *Playlist) updateTitle() {
if p.download == 0 {
p.SetTitle(p.defaultTitle)
return
}
// only one call can be made in one time
if p.download > 1 {
return
}
2020-07-24 22:27:08 +08:00
s := spin.New()
Download:
for {
2020-08-14 10:43:55 +08:00
2020-07-24 22:27:08 +08:00
select {
case <-p.done:
p.download -= 1
if p.download == 0 {
p.SetTitle(p.defaultTitle)
break Download
}
case <-time.After(time.Millisecond * 100):
2020-07-25 22:53:25 +08:00
2020-08-22 14:41:03 +08:00
r, g, b := gomu.colors.accent.RGB()
2020-07-25 22:53:25 +08:00
hexColor := padHex(r, g, b)
title := fmt.Sprintf("─ Playlist ──┤ %d downloads [green]%s[#%s] ├",
p.download, s.Next(), hexColor)
2020-07-24 22:27:08 +08:00
p.SetTitle(title)
gomu.app.Draw()
}
}
}
2020-07-23 15:15:39 +08:00
// Download audio from youtube audio and adds the song to the selected playlist
func ytdl(url string, selPlaylist *tview.TreeNode) error {
2020-07-03 11:17:45 +08:00
// lookup if youtube-dl exists
_, err := exec.LookPath("youtube-dl")
if err != nil {
2020-08-25 20:45:16 +08:00
defaultTimedPopup(" Error ", "youtube-dl is not in your $PATH")
2020-07-20 21:48:13 +08:00
2020-07-21 12:22:00 +08:00
return tracerr.Wrap(err)
2020-07-03 11:17:45 +08:00
}
selAudioFile := selPlaylist.GetReference().(*AudioFile)
2021-02-02 22:52:37 +08:00
dir := selAudioFile.path
2020-07-03 11:17:45 +08:00
2020-08-25 20:45:16 +08:00
defaultTimedPopup(" Ytdl ", "Downloading")
2020-07-03 11:17:45 +08:00
// specify the output path for ytdl
outputDir := fmt.Sprintf(
"%s/%%(title)s.%%(ext)s",
2021-02-04 15:35:58 +08:00
dir)
2020-07-03 11:17:45 +08:00
args := []string{
"--extract-audio",
"--audio-format",
"mp3",
"--output",
outputDir,
// "--cookies",
// "~/Downloads/youtube.com_cookies.txt",
2020-07-03 11:17:45 +08:00
url,
}
cmd := exec.Command("youtube-dl", args...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
2020-07-24 22:27:08 +08:00
gomu.playlist.download++
go gomu.playlist.updateTitle()
2020-08-14 10:43:55 +08:00
// blocking
2020-07-21 12:22:00 +08:00
err = cmd.Run()
2020-07-12 17:05:00 +08:00
2020-07-24 22:27:08 +08:00
gomu.playlist.done <- struct{}{}
2020-07-21 12:22:00 +08:00
if err != nil {
2020-08-25 20:45:16 +08:00
defaultTimedPopup(" Error ", "Error running youtube-dl")
2020-07-21 12:22:00 +08:00
return tracerr.Wrap(err)
}
2020-07-12 17:05:00 +08:00
playlistPath := dir
2020-07-21 12:22:00 +08:00
audioPath := extractFilePath(stdout.Bytes(), playlistPath)
2020-07-03 11:17:45 +08:00
historyPath, err := gomu.env.Get("history_path")
if err != nil {
return tracerr.Wrap(err)
}
err = appendFile(expandTilde(historyPath.(string)), url+"\n")
2020-08-18 16:09:43 +08:00
if err != nil {
return tracerr.Wrap(err)
}
2020-07-23 15:15:39 +08:00
err = gomu.playlist.addSongToPlaylist(audioPath, selPlaylist)
2020-07-21 12:22:00 +08:00
if err != nil {
return tracerr.Wrap(err)
}
2020-07-03 11:17:45 +08:00
2021-02-04 15:35:58 +08:00
downloadFinishedMessage := fmt.Sprintf("Finished downloading\n%s", getName(audioPath))
2020-08-25 20:45:16 +08:00
defaultTimedPopup(" Ytdl ", downloadFinishedMessage)
2021-02-04 15:35:58 +08:00
gomu.app.Draw()
2020-07-13 14:33:13 +08:00
2020-07-23 15:15:39 +08:00
return nil
}
// Add songs and their directories in Playlist panel
func populate(root *tview.TreeNode, rootPath string) error {
files, err := ioutil.ReadDir(rootPath)
if err != nil {
return tracerr.Wrap(err)
}
for _, file := range files {
path, err := filepath.EvalSymlinks(filepath.Join(rootPath, file.Name()))
2020-07-23 15:15:39 +08:00
if err != nil {
continue
}
songName := getName(file.Name())
child := tview.NewTreeNode(songName)
if file.Mode().IsRegular() {
2020-07-23 15:15:39 +08:00
2021-02-02 22:52:37 +08:00
f, err := os.Open(path)
if err != nil {
continue
}
defer f.Close()
2020-07-23 15:15:39 +08:00
filetype, err := getFileContentType(f)
if err != nil {
continue
}
// skip if not mp3 file
if filetype != "mpeg" {
continue
}
audioFile := &AudioFile{
name: songName,
path: path,
isAudioFile: true,
node: child,
parent: root,
}
displayText := setDisplayText(songName)
2020-08-11 18:31:33 +08:00
2020-07-23 15:15:39 +08:00
child.SetReference(audioFile)
2020-08-11 18:31:33 +08:00
child.SetText(displayText)
2020-07-23 15:15:39 +08:00
root.AddChild(child)
}
2021-02-04 12:23:32 +08:00
if file.IsDir() || file.Mode()&os.ModeSymlink != 0 {
2020-07-23 15:15:39 +08:00
audioFile := &AudioFile{
2021-02-02 22:52:37 +08:00
name: songName,
path: path,
isAudioFile: false,
2021-02-02 22:52:37 +08:00
node: child,
parent: root,
2020-07-23 15:15:39 +08:00
}
2020-08-11 18:31:33 +08:00
displayText := setDisplayText(songName)
2020-08-11 18:31:33 +08:00
2020-07-23 15:15:39 +08:00
child.SetReference(audioFile)
2020-08-22 14:41:03 +08:00
child.SetColor(gomu.colors.accent)
2020-08-11 18:31:33 +08:00
child.SetText(displayText)
2020-07-23 15:15:39 +08:00
root.AddChild(child)
populate(child, path)
}
}
2020-07-03 11:17:45 +08:00
2020-07-21 12:22:00 +08:00
return nil
2020-07-13 14:33:13 +08:00
}
func (p *Playlist) yank() error {
2021-02-02 22:52:37 +08:00
yankFile = p.getCurrentFile()
if yankFile == nil {
isYanked = false
defaultTimedPopup(" Error! ", "No file has been yanked.")
return nil
}
if yankFile.node == p.GetRoot() {
isYanked = false
defaultTimedPopup(" Error! ", "Please don't yank the root directory.")
return nil
}
2021-02-02 22:52:37 +08:00
isYanked = true
defaultTimedPopup(" Success ", yankFile.name+"\n has been yanked successfully.")
2021-02-02 22:52:37 +08:00
return nil
}
func (p *Playlist) paste() error {
2021-02-02 22:52:37 +08:00
if isYanked {
isYanked = false
oldPathDir, oldPathFileName := filepath.Split(yankFile.path)
pasteFile := p.getCurrentFile()
if pasteFile.isAudioFile {
newPathDir, _ := filepath.Split(pasteFile.path)
if oldPathDir == newPathDir {
return nil
} else {
newPathFull := filepath.Join(newPathDir, oldPathFileName)
err := os.Rename(yankFile.path, newPathFull)
if err != nil {
defaultTimedPopup(" Error ", yankFile.name+"\n has not been pasted.")
return tracerr.Wrap(err)
2021-02-02 22:52:37 +08:00
}
defaultTimedPopup(" Success ", yankFile.name+"\n has been pasted to\n"+pasteFile.name)
2021-02-02 22:52:37 +08:00
}
} else {
newPathDir := pasteFile.path
if oldPathDir == newPathDir {
return nil
} else {
newPathFull := filepath.Join(newPathDir, oldPathFileName)
err := os.Rename(yankFile.path, newPathFull)
if err != nil {
defaultTimedPopup(" Error ", yankFile.name+"\n has not been pasted.")
return tracerr.Wrap(err)
2021-02-02 22:52:37 +08:00
}
defaultTimedPopup(" Success ", yankFile.name+"\n has been pasted to\n"+pasteFile.name)
2021-02-02 22:52:37 +08:00
}
}
p.refresh()
gomu.queue.updateQueueNames()
}
return nil
}
func setDisplayText(songName string) string {
useEmoji, err := getBool(gomu.env, "use_emoji")
if err != nil {
log.Fatal(err)
}
if !useEmoji {
return songName
}
emojiFile, err := getString(gomu.env, "emoji_file")
if err != nil {
log.Fatal(err)
}
return fmt.Sprintf(" %s %s", emojiFile, songName)
}
2021-02-03 10:50:01 +08:00
//populateAudioLength is the most time consuming part of startup,
//so here we initialize it separately
func populateAudioLength(root *tview.TreeNode) error {
2021-02-02 22:52:37 +08:00
root.Walk(func(node *tview.TreeNode, _ *tview.TreeNode) bool {
audioFile := node.GetReference().(*AudioFile)
if audioFile.isAudioFile {
audioLength, err := getLength(audioFile.path)
if err != nil {
logError(err)
return false
}
audioFile.length = audioLength
}
return true
})
2021-02-02 22:52:37 +08:00
gomu.queue.updateTitle()
return nil
}