diff --git a/command.go b/command.go index 51f999b..7ed9735 100644 --- a/command.go +++ b/command.go @@ -316,7 +316,6 @@ func (c Command) defineCommands() { err := gomu.player.Seek(position) if err != nil { errorPopup(err) - gomu.app.Draw() } gomu.playingBar.setProgress(position) } @@ -330,14 +329,12 @@ func (c Command) defineCommands() { err := gomu.player.Seek(position) if err != nil { errorPopup(err) - gomu.app.Draw() } gomu.playingBar.setProgress(position) } else { err := gomu.player.Seek(0) if err != nil { errorPopup(err) - gomu.app.Draw() } gomu.playingBar.setProgress(0) } @@ -351,7 +348,6 @@ func (c Command) defineCommands() { err := gomu.player.Seek(position) if err != nil { errorPopup(err) - gomu.app.Draw() } gomu.playingBar.setProgress(position) } @@ -365,14 +361,12 @@ func (c Command) defineCommands() { err := gomu.player.Seek(position) if err != nil { errorPopup(err) - gomu.app.Draw() } gomu.playingBar.setProgress(position) } else { err := gomu.player.Seek(0) if err != nil { errorPopup(err) - gomu.app.Draw() } gomu.playingBar.setProgress(0) } @@ -383,7 +377,6 @@ func (c Command) defineCommands() { err := gomu.playlist.yank() if err != nil { errorPopup(err) - gomu.app.Draw() } }) @@ -391,7 +384,6 @@ func (c Command) defineCommands() { err := gomu.playlist.paste() if err != nil { errorPopup(err) - gomu.app.Draw() } }) @@ -422,7 +414,6 @@ func (c Command) defineCommands() { err := lyricPopup(lang, audioFile, &wg) if err != nil { errorPopup(err) - gomu.app.Draw() } }() } @@ -439,7 +430,6 @@ func (c Command) defineCommands() { err := lyricPopup(lang, audioFile, &wg) if err != nil { errorPopup(err) - gomu.app.Draw() } }() } @@ -449,7 +439,6 @@ func (c Command) defineCommands() { err := gomu.playingBar.delayLyric(500) if err != nil { errorPopup(err) - gomu.app.Draw() } }) @@ -457,7 +446,6 @@ func (c Command) defineCommands() { err := gomu.playingBar.delayLyric(-500) if err != nil { errorPopup(err) - gomu.app.Draw() } }) diff --git a/playlist.go b/playlist.go index 7c84444..3ac00a2 100644 --- a/playlist.go +++ b/playlist.go @@ -4,6 +4,7 @@ package main import ( "bytes" + "errors" "fmt" "io/ioutil" "os" @@ -82,12 +83,8 @@ type Playlist struct { // number of downloads download int done chan struct{} -} - -var ( yankFile *AudioFile - isYanked bool -) +} func (p *Playlist) help() []string { @@ -488,6 +485,7 @@ 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 { @@ -500,11 +498,6 @@ func (p *Playlist) rename(newName string) error { return tracerr.Wrap(err) } - audio.path = newPath - gomu.queue.saveQueue(false) - gomu.queue.clearQueue() - gomu.queue.loadQueue() - return nil } @@ -778,60 +771,51 @@ func populate(root *tview.TreeNode, rootPath string, sortMtime bool) error { } func (p *Playlist) yank() error { - yankFile = p.getCurrentFile() - if yankFile == nil { - isYanked = false - defaultTimedPopup(" Error! ", "No file has been yanked.") - return nil + p.yankFile = p.getCurrentFile() + if p.yankFile == nil { + return errors.New("no file has been yanked") } - if yankFile.node == p.GetRoot() { - isYanked = false - defaultTimedPopup(" Error! ", "Please don't yank the root directory.") - return nil + if p.yankFile.node == p.GetRoot() { + return errors.New("please don't yank the root directory") } - isYanked = true - defaultTimedPopup(" Success ", yankFile.name+"\n has been yanked successfully.") + defaultTimedPopup(" Success ", p.yankFile.name+"\n has been yanked successfully.") return nil } func (p *Playlist) paste() error { - 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 - } - 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) - } - defaultTimedPopup(" Success ", yankFile.name+"\n has been pasted to\n"+pasteFile.name) - - } else { - newPathDir := pasteFile.path - if oldPathDir == newPathDir { - return nil - } - 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) - } - defaultTimedPopup(" Success ", yankFile.name+"\n has been pasted to\n"+pasteFile.name) - - } - - p.refresh() - gomu.queue.updateQueueNames() + if p.yankFile == nil { + return errors.New("no file has been yanked") } + oldPathDir, oldPathFileName := filepath.Split(p.yankFile.path) + pasteFile := p.getCurrentFile() + var newPathDir string + if pasteFile.isAudioFile { + newPathDir, _ = filepath.Split(pasteFile.path) + } else { + newPathDir = pasteFile.path + } + + if oldPathDir == newPathDir { + return nil + } + + newPathFull := filepath.Join(newPathDir, oldPathFileName) + err := os.Rename(p.yankFile.path, newPathFull) + if err != nil { + return tracerr.Wrap(err) + } + + defaultTimedPopup(" Success ", p.yankFile.name+"\n has been pasted to\n"+newPathDir) + p.yankFile = nil + + // keep queue references updated + p.refresh() + gomu.queue.saveQueue(false) + gomu.queue.clearQueue() + gomu.queue.loadQueue() + return nil } diff --git a/popup.go b/popup.go index bdafc72..f72e846 100644 --- a/popup.go +++ b/popup.go @@ -555,14 +555,12 @@ func renamePopup(node *AudioFile) { } err := gomu.playlist.rename(newName) if err != nil { - defaultTimedPopup(" Error ", err.Error()) - logError(err) + errorPopup(err) } gomu.pages.RemovePage(popupID) gomu.popups.pop() gomu.playlist.refresh() - gomu.queue.updateQueueNames() gomu.setFocusPanel(gomu.playlist) gomu.prevPanel = gomu.playlist @@ -573,6 +571,18 @@ func renamePopup(node *AudioFile) { } return true }) + // update queue + if node.isAudioFile { + newNode := gomu.playlist.getCurrentFile() + err = gomu.queue.rename(node, newNode) + if err != nil { + errorPopup(err) + } + } else { + gomu.queue.saveQueue(false) + gomu.queue.clearQueue() + gomu.queue.loadQueue() + } case tcell.KeyEsc: gomu.pages.RemovePage(popupID) diff --git a/queue.go b/queue.go index 682a7d1..89c503f 100644 --- a/queue.go +++ b/queue.go @@ -412,10 +412,23 @@ func sha1Hex(input string) string { } // Modify the title of songs in queue -func (q *Queue) updateQueueNames() error { - q.saveQueue(false) - q.clearQueue() - q.loadQueue() +func (q *Queue) rename(oldAudio *AudioFile, newAudio *AudioFile) error { + for i, v := range q.items { + if v.name != oldAudio.name { + continue + } + + q.items[i] = newAudio + songLength, err := getTagLength(newAudio.path) + if err != nil { + return tracerr.Wrap(err) + } + queueItemView := fmt.Sprintf( + "[ %s ] %s", fmtDuration(songLength), getName(newAudio.name), + ) + q.SetItemText(i, queueItemView, "") + + } return nil }