2020-06-22 00:05:56 +08:00
|
|
|
// Copyright (C) 2020 Raziman
|
|
|
|
|
2020-06-19 16:29:51 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-06-25 10:46:45 +08:00
|
|
|
"os"
|
|
|
|
|
2020-06-19 16:29:51 +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:29:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func start(app *tview.Application) {
|
|
|
|
// override default border
|
|
|
|
// change double line border to one line border when focused
|
|
|
|
tview.Borders.HorizontalFocus = tview.Borders.Horizontal
|
|
|
|
tview.Borders.VerticalFocus = tview.Borders.Vertical
|
|
|
|
tview.Borders.TopLeftFocus = tview.Borders.TopLeft
|
|
|
|
tview.Borders.TopRightFocus = tview.Borders.TopRight
|
|
|
|
tview.Borders.BottomLeftFocus = tview.Borders.BottomLeft
|
|
|
|
tview.Borders.BottomRightFocus = tview.Borders.BottomRight
|
|
|
|
tview.Styles.PrimitiveBackgroundColor = tcell.ColorDefault
|
2020-06-24 20:09:47 +08:00
|
|
|
tview.Styles.BorderColor = tcell.ColorWhite
|
2020-06-19 16:29:51 +08:00
|
|
|
|
2020-06-21 00:00:17 +08:00
|
|
|
player := &Player{}
|
|
|
|
|
2020-06-19 16:29:51 +08:00
|
|
|
|
2020-06-25 14:12:19 +08:00
|
|
|
playingBar := PlayingBar(app, player)
|
|
|
|
queue := Queue(player)
|
|
|
|
playlist := Playlist(player)
|
|
|
|
|
|
|
|
player.tree = playlist
|
|
|
|
player.list = queue
|
|
|
|
player.playingBar = playingBar
|
2020-06-21 23:47:12 +08:00
|
|
|
player.app = app
|
|
|
|
|
|
|
|
flex := Layout(app, player)
|
2020-06-19 16:29:51 +08:00
|
|
|
pages := tview.NewPages().AddPage("main", flex, true, true)
|
|
|
|
|
2020-06-25 14:12:19 +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() {
|
|
|
|
case 'l':
|
|
|
|
|
|
|
|
log("test")
|
|
|
|
addToQueue(audioFile, player, queue)
|
|
|
|
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
|
|
|
|
|
|
|
|
currNode.SetColor(textColor)
|
|
|
|
parent.SetExpanded(false)
|
|
|
|
parent.SetColor(accentColor)
|
|
|
|
//prevNode = parent
|
|
|
|
playlist.SetCurrentNode(parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
currNode.Collapse()
|
|
|
|
|
|
|
|
case 'L':
|
|
|
|
|
|
|
|
confirmationPopup(
|
|
|
|
app,
|
|
|
|
pages,
|
|
|
|
"Are you sure to add this whole directory into queue?",
|
|
|
|
func (_ int, label string) {
|
|
|
|
|
|
|
|
if label == "yes" {
|
|
|
|
addAllToQueue(playlist.GetCurrentNode(), player, queue)
|
|
|
|
}
|
|
|
|
|
|
|
|
pages.RemovePage("confirmation-popup")
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return e
|
|
|
|
})
|
|
|
|
|
|
|
|
childrens := []Children{playlist, queue, playingBar.frame}
|
2020-06-19 16:29:51 +08:00
|
|
|
|
|
|
|
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
|
|
|
|
switch event.Key() {
|
|
|
|
// cycle through each section
|
|
|
|
case tcell.KeyTAB:
|
|
|
|
cycleChildren(app, childrens)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
switch event.Rune() {
|
|
|
|
case 'q':
|
2020-06-19 16:42:30 +08:00
|
|
|
|
2020-06-25 10:46:45 +08:00
|
|
|
if !viper.GetBool("confirm_on_exit") {
|
2020-06-24 20:09:47 +08:00
|
|
|
app.Stop()
|
|
|
|
}
|
|
|
|
|
2020-06-25 10:46:45 +08:00
|
|
|
|
2020-06-19 16:42:30 +08:00
|
|
|
confirmationPopup(app, pages, "Are you sure to exit?", func(_ int, label string) {
|
|
|
|
|
2020-06-19 16:29:51 +08:00
|
|
|
if label == "yes" {
|
|
|
|
app.Stop()
|
|
|
|
} else {
|
|
|
|
pages.RemovePage("confirmation-popup")
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2020-06-21 00:00:17 +08:00
|
|
|
case ' ':
|
2020-06-23 20:15:29 +08:00
|
|
|
player.TogglePause()
|
2020-06-21 23:47:12 +08:00
|
|
|
|
|
|
|
case '+':
|
|
|
|
player.Volume(0.5)
|
|
|
|
|
|
|
|
case '-':
|
|
|
|
player.Volume(-0.5)
|
2020-06-21 00:00:17 +08:00
|
|
|
|
2020-06-23 18:42:18 +08:00
|
|
|
case 'n':
|
|
|
|
player.Skip()
|
|
|
|
|
2020-06-19 16:29:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return event
|
|
|
|
})
|
|
|
|
|
|
|
|
// fix transparent background issue
|
|
|
|
app.SetBeforeDrawFunc(func(screen tcell.Screen) bool {
|
|
|
|
screen.Clear()
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
// main loop
|
|
|
|
if err := app.SetRoot(pages, true).SetFocus(flex).Run(); err != nil {
|
2020-06-21 23:47:12 +08:00
|
|
|
log(err.Error())
|
2020-06-19 16:29:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// created so we can keep track of childrens in slices
|
|
|
|
type Children interface {
|
|
|
|
HasFocus() bool
|
|
|
|
SetBorderColor(color tcell.Color) *tview.Box
|
|
|
|
SetTitleColor(color tcell.Color) *tview.Box
|
|
|
|
SetTitle(s string) *tview.Box
|
|
|
|
GetTitle() string
|
|
|
|
}
|
|
|
|
|
|
|
|
func cycleChildren(app *tview.Application, childrens []Children) {
|
|
|
|
|
|
|
|
focusedColor := tcell.ColorDarkCyan
|
|
|
|
unfocusedColor := tcell.ColorAntiqueWhite
|
|
|
|
anyChildHasFocus := false
|
|
|
|
|
|
|
|
for i, child := range childrens {
|
|
|
|
|
|
|
|
if child.HasFocus() {
|
|
|
|
|
|
|
|
anyChildHasFocus = true
|
|
|
|
|
|
|
|
var nextChild Children
|
|
|
|
|
|
|
|
// if its the last element set the child back to one
|
2020-06-19 16:42:30 +08:00
|
|
|
if i == len(childrens)-1 {
|
2020-06-19 16:29:51 +08:00
|
|
|
nextChild = childrens[0]
|
|
|
|
} else {
|
2020-06-19 16:42:30 +08:00
|
|
|
nextChild = childrens[i+1]
|
2020-06-19 16:29:51 +08:00
|
|
|
}
|
|
|
|
|
2020-06-19 16:42:30 +08:00
|
|
|
child.SetBorderColor(unfocusedColor)
|
2020-06-19 16:29:51 +08:00
|
|
|
child.SetTitleColor(unfocusedColor)
|
|
|
|
|
|
|
|
app.SetFocus(nextChild.(tview.Primitive))
|
|
|
|
nextChild.SetBorderColor(focusedColor)
|
|
|
|
nextChild.SetTitleColor(focusedColor)
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if anyChildHasFocus == false {
|
2020-06-19 16:42:30 +08:00
|
|
|
|
2020-06-19 16:29:51 +08:00
|
|
|
app.SetFocus(childrens[0].(tview.Primitive))
|
|
|
|
childrens[0].SetBorderColor(focusedColor)
|
|
|
|
childrens[0].SetTitleColor(focusedColor)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-06-24 20:09:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
func readConfig() {
|
|
|
|
|
2020-06-25 10:46:45 +08:00
|
|
|
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
configPath := home + "/.config/gomu/config"
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
viper.SetConfigName("config")
|
|
|
|
viper.SetConfigType("yaml")
|
2020-06-24 20:09:47 +08:00
|
|
|
viper.AddConfigPath("/etc/gomu")
|
2020-06-25 10:46:45 +08:00
|
|
|
viper.AddConfigPath(home + "/.gomu")
|
2020-06-24 20:09:47 +08:00
|
|
|
viper.AddConfigPath("$HOME/.config/gomu")
|
|
|
|
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
2020-06-25 10:46:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
viper.SetDefault("music_dir", "~/music")
|
|
|
|
viper.SetDefault("confirm_on_exit", true)
|
|
|
|
|
|
|
|
// creates gomu config dir if does not exist
|
|
|
|
if _, err := os.Stat(configPath); err != nil {
|
|
|
|
if err := os.MkdirAll(home + "/.config/gomu", 0755); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if config file was not found
|
|
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
|
|
|
if err := viper.SafeWriteConfigAs(configPath); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-24 20:09:47 +08:00
|
|
|
}
|
|
|
|
|
2020-06-25 10:46:45 +08:00
|
|
|
|
2020-06-24 20:09:47 +08:00
|
|
|
}
|