This commit is contained in:
raziman 2020-07-18 15:43:20 +08:00
parent 26ff9a8be9
commit 5d88bb69ae
3 changed files with 49 additions and 11 deletions

View File

@ -14,11 +14,13 @@ func main() {
os.Setenv("TEST", "false")
readConfig()
args := getArgs()
readConfig(args)
app := tview.NewApplication()
start(app)
start(app, args)
}
@ -38,3 +40,5 @@ func init() {
log.SetOutput(file)
log.SetFlags(log.Ldate | log.Ltime | log.Llongfile)
}

View File

@ -3,6 +3,7 @@
package main
import (
"flag"
"log"
"os"
"strings"
@ -123,7 +124,8 @@ func (g *Gomu) SetUnfocusPanel(panel Panel) {
// one single instance of global variable
var gomu *Gomu
func start(application *tview.Application) {
func start(application *tview.Application, args Args) {
// override default border
// change double line border to one line border when focused
tview.Borders.HorizontalFocus = tview.Borders.Horizontal
@ -143,12 +145,16 @@ func start(application *tview.Application) {
flex := Layout(gomu)
gomu.Pages.AddPage("main", flex, true, true)
// sets the first focused panel
gomu.Playlist.SetBorderColor(gomu.AccentColor)
gomu.Playlist.SetTitleColor(gomu.AccentColor)
gomu.PrevPanel = gomu.Playlist
if err := gomu.Queue.LoadQueue(); err != nil {
log.Println(err)
if *args.load {
// load saved queue from previous
if err := gomu.Queue.LoadQueue(); err != nil {
log.Println(err)
}
}
application.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
@ -240,10 +246,18 @@ func start(application *tview.Application) {
}
}
func readConfig() {
func readConfig(args Args) {
configPath := *args.config
musicDir := *args.music
home, err := os.UserHomeDir()
configPath := home + "/.config/gomu/config"
if err != nil {
log.Println(err)
}
defaultPath := home + "/.config/gomu/config"
if err != nil {
log.Println(err)
@ -251,20 +265,22 @@ func readConfig() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(expandTilde(configPath))
viper.AddConfigPath("/etc/gomu")
viper.AddConfigPath("$HOME/.gomu")
viper.AddConfigPath("$HOME/.config/gomu")
if err := viper.ReadInConfig(); err != nil {
viper.SetDefault("music_dir", "~/music")
viper.SetDefault("music_dir", musicDir)
viper.SetDefault("confirm_on_exit", true)
viper.SetDefault("confirm_bulk_add", true)
viper.SetDefault("popup_timeout", "5s")
viper.SetDefault("volume", "50")
// creates gomu config dir if does not exist
if _, err := os.Stat(configPath); err != nil {
if _, err := os.Stat(defaultPath); err != nil {
if err := os.MkdirAll(home+"/.config/gomu", 0755); err != nil {
log.Println(err)
}
@ -272,7 +288,7 @@ func readConfig() {
// if config file was not found
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
if err := viper.SafeWriteConfigAs(configPath); err != nil {
if err := viper.SafeWriteConfigAs(defaultPath); err != nil {
log.Println(err)
}
}
@ -281,6 +297,25 @@ func readConfig() {
}
type Args struct {
config *string
load *bool
music *string
}
func getArgs() Args {
ar := Args{
config: flag.String("config", "~/.config/gomu/config", "specify config file"),
load: flag.Bool("load", true, "load previous queue"),
music: flag.String("music", "~/music", "specify music directory"),
}
flag.Parse()
return ar
}
// layout is used to organize the panels
func Layout(gomu *Gomu) *tview.Flex {

View File

@ -13,7 +13,6 @@ import (
"time"
)
// formats duration to my desired output mm:ss
func fmtDuration(input time.Duration) string {