gomu/start.go

233 lines
4.9 KiB
Go
Raw Normal View History

2020-06-22 00:05:56 +08:00
// Copyright (C) 2020 Raziman
2020-06-19 16:29:51 +08:00
package main
import (
2020-07-18 15:43:20 +08:00
"flag"
2020-07-21 20:51:10 +08:00
"fmt"
2020-06-25 10:46:45 +08:00
"os"
"strings"
2020-06-25 10:46:45 +08:00
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
)
2020-07-23 15:34:56 +08:00
// Created so we can keep track of childrens in slices
2020-07-06 17:02:59 +08:00
type Panel interface {
HasFocus() bool
SetBorderColor(color tcell.Color) *tview.Box
SetTitleColor(color tcell.Color) *tview.Box
SetTitle(s string) *tview.Box
GetTitle() string
2020-07-23 15:15:39 +08:00
help() []string
2020-07-06 17:02:59 +08:00
}
2020-07-23 15:15:39 +08:00
func readConfig(args Args) {
2020-07-23 15:15:39 +08:00
configPath := *args.config
musicDir := *args.music
2020-07-23 15:15:39 +08:00
home, err := os.UserHomeDir()
2020-07-06 17:02:59 +08:00
2020-07-23 15:15:39 +08:00
if err != nil {
logError(err)
}
2020-07-06 17:02:59 +08:00
2020-07-23 15:15:39 +08:00
defaultPath := home + "/.config/gomu/config"
2020-07-06 17:02:59 +08:00
2020-07-23 15:15:39 +08:00
if err != nil {
logError(err)
}
2020-07-06 17:02:59 +08:00
2020-07-23 15:15:39 +08:00
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(expandTilde(configPath))
viper.AddConfigPath("/etc/gomu")
viper.AddConfigPath("$HOME/.gomu")
viper.AddConfigPath("$HOME/.config/gomu")
2020-07-06 17:02:59 +08:00
2020-07-23 15:15:39 +08:00
if err := viper.ReadInConfig(); err != nil {
2020-07-06 17:02:59 +08:00
2020-07-23 15:15:39 +08:00
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")
2020-07-06 17:02:59 +08:00
2020-07-23 15:15:39 +08:00
// creates gomu config dir if does not exist
if _, err := os.Stat(defaultPath); err != nil {
if err := os.MkdirAll(home+"/.config/gomu", 0755); err != nil {
logError(err)
2020-07-06 17:02:59 +08:00
}
}
2020-07-23 15:15:39 +08:00
// if config file was not found
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
if err := viper.SafeWriteConfigAs(defaultPath); err != nil {
logError(err)
}
}
2020-07-06 17:02:59 +08:00
}
}
2020-07-23 15:15:39 +08:00
type Args struct {
config *string
load *bool
music *string
version *bool
2020-07-06 17:02:59 +08:00
}
2020-07-23 15:15:39 +08:00
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"),
version: flag.Bool("version", false, "print gomu version"),
}
2020-07-23 15:15:39 +08:00
flag.Parse()
return ar
}
2020-07-23 15:15:39 +08:00
// Sets the layout of the application
func layout(gomu *Gomu) *tview.Flex {
flex := tview.NewFlex().
AddItem(gomu.playlist, 0, 1, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(gomu.queue, 0, 5, false).
AddItem(gomu.playingBar, 0, 1, false), 0, 3, false)
2020-07-23 15:15:39 +08:00
return flex
}
2020-07-23 15:15:39 +08:00
// Initialize
2020-07-18 15:43:20 +08:00
func start(application *tview.Application, args Args) {
2020-07-23 15:15:39 +08:00
// Print version and exit
2020-07-21 20:51:10 +08:00
if *args.version {
fmt.Printf("Gomu %s\n", VERSION)
return
}
2020-06-19 16:29:51 +08:00
// override default border
// change double line border to one line border when focused
2020-07-06 18:58:27 +08:00
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
2020-06-19 16:29:51 +08:00
tview.Styles.PrimitiveBackgroundColor = tcell.ColorDefault
2020-07-06 18:58:27 +08:00
tview.Styles.BorderColor = tcell.ColorWhite
2020-07-23 15:15:39 +08:00
// Assigning to global variable gomu
gomu = newGomu()
gomu.initPanels(application)
2020-06-19 16:29:51 +08:00
2020-07-23 15:15:39 +08:00
logError(fmt.Errorf("App start"))
2020-07-02 16:11:10 +08:00
2020-07-23 15:15:39 +08:00
flex := layout(gomu)
gomu.pages.AddPage("main", flex, true, true)
2020-06-25 14:12:19 +08:00
2020-07-18 15:43:20 +08:00
// sets the first focused panel
2020-07-23 15:15:39 +08:00
gomu.setFocusPanel(gomu.playlist)
gomu.prevPanel = gomu.playlist
2020-06-25 14:12:19 +08:00
2020-07-18 15:43:20 +08:00
if *args.load {
// load saved queue from previous
2020-07-23 15:15:39 +08:00
if err := gomu.queue.loadQueue(); err != nil {
logError(err)
2020-07-18 15:43:20 +08:00
}
2020-07-10 16:52:37 +08:00
}
2020-06-26 12:54:48 +08:00
application.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
2020-06-19 16:29:51 +08:00
switch event.Key() {
// cycle through each section
case tcell.KeyTAB:
2020-07-23 15:15:39 +08:00
gomu.cyclePanels()
2020-07-06 17:02:59 +08:00
2020-06-19 16:29:51 +08:00
}
2020-07-23 15:15:39 +08:00
name, _ := gomu.pages.GetFrontPage()
// disables keybindings when writing in input fields
if strings.Contains(name, "-input-") {
return event
}
2020-06-19 16:29:51 +08:00
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-26 12:54:48 +08:00
application.Stop()
2020-06-24 20:09:47 +08:00
}
2020-06-26 12:54:48 +08:00
confirmationPopup("Are you sure to exit?", func(_ int, label string) {
2020-06-19 16:42:30 +08:00
if label == "no" || label == "" {
2020-07-23 15:15:39 +08:00
gomu.pages.RemovePage("confirmation-popup")
2020-07-10 16:52:37 +08:00
return
2020-07-10 22:05:36 +08:00
}
2020-07-23 15:15:39 +08:00
if err := gomu.queue.saveQueue(); err != nil {
logError(err)
2020-06-19 16:29:51 +08:00
}
2020-07-12 09:48:48 +08:00
if err := viper.WriteConfig(); err != nil {
2020-07-23 15:15:39 +08:00
logError(err)
2020-07-12 09:48:48 +08:00
}
2020-07-10 16:52:37 +08:00
application.Stop()
2020-06-19 16:29:51 +08:00
})
2020-06-21 00:00:17 +08:00
case ' ':
2020-07-23 15:15:39 +08:00
gomu.player.togglePause()
2020-06-21 23:47:12 +08:00
case '+':
2020-07-23 15:15:39 +08:00
v := int(gomu.player.volume*10) + 50
2020-06-26 17:09:15 +08:00
if v < 50 {
2020-07-23 15:15:39 +08:00
vol := gomu.player.setVolume(0.5)
2020-06-26 17:09:15 +08:00
volumePopup(vol)
}
2020-06-21 23:47:12 +08:00
case '-':
2020-07-23 15:15:39 +08:00
v := int(gomu.player.volume*10) + 50
2020-06-26 17:09:15 +08:00
if v > 0 {
2020-07-23 15:15:39 +08:00
vol := gomu.player.setVolume(-0.5)
2020-06-26 17:09:15 +08:00
volumePopup(vol)
}
2020-06-21 00:00:17 +08:00
2020-06-23 18:42:18 +08:00
case 'n':
2020-07-23 15:15:39 +08:00
gomu.player.skip()
2020-06-23 18:42:18 +08:00
2020-06-27 00:09:59 +08:00
case '?':
2020-07-23 15:15:39 +08:00
name, _ := gomu.pages.GetFrontPage()
2020-06-27 00:09:59 +08:00
if name == "help-page" {
2020-07-23 15:15:39 +08:00
gomu.pages.RemovePage(name)
gomu.app.SetFocus(gomu.prevPanel.(tview.Primitive))
2020-06-27 00:09:59 +08:00
} else {
2020-07-23 15:15:39 +08:00
helpPopup(gomu.prevPanel)
2020-06-27 00:09:59 +08:00
}
2020-06-19 16:29:51 +08:00
}
return event
})
// fix transparent background issue
2020-06-26 12:54:48 +08:00
application.SetBeforeDrawFunc(func(screen tcell.Screen) bool {
2020-06-19 16:29:51 +08:00
screen.Clear()
return false
})
// main loop
2020-07-23 15:15:39 +08:00
if err := application.SetRoot(gomu.pages, true).SetFocus(gomu.playlist).Run(); err != nil {
logError(err)
2020-06-19 16:29:51 +08:00
}
}