mirror of
https://github.com/issadarkthing/gomu.git
synced 2025-04-26 13:49:21 +08:00
refactor yank paste and rename
This commit is contained in:
parent
fda946ae92
commit
90cc3c4970
12
command.go
12
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()
|
||||
}
|
||||
})
|
||||
|
||||
|
70
playlist.go
70
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,59 +771,50 @@ 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)
|
||||
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)
|
||||
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)
|
||||
|
||||
newPathDir, _ = filepath.Split(pasteFile.path)
|
||||
} else {
|
||||
newPathDir := pasteFile.path
|
||||
newPathDir = pasteFile.path
|
||||
}
|
||||
|
||||
if oldPathDir == newPathDir {
|
||||
return nil
|
||||
}
|
||||
|
||||
newPathFull := filepath.Join(newPathDir, oldPathFileName)
|
||||
err := os.Rename(yankFile.path, newPathFull)
|
||||
err := os.Rename(p.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)
|
||||
|
||||
}
|
||||
defaultTimedPopup(" Success ", p.yankFile.name+"\n has been pasted to\n"+newPathDir)
|
||||
p.yankFile = nil
|
||||
|
||||
// keep queue references updated
|
||||
p.refresh()
|
||||
gomu.queue.updateQueueNames()
|
||||
}
|
||||
gomu.queue.saveQueue(false)
|
||||
gomu.queue.clearQueue()
|
||||
gomu.queue.loadQueue()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
16
popup.go
16
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)
|
||||
|
21
queue.go
21
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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user