gomu/start.go

296 lines
6.8 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-08-18 12:26:45 +08:00
"errors"
2020-07-18 15:43:20 +08:00
"flag"
2020-07-21 20:51:10 +08:00
"fmt"
2021-02-14 14:35:36 +08:00
"io/ioutil"
2020-06-25 10:46:45 +08:00
"os"
2020-08-18 12:26:45 +08:00
"os/signal"
"strings"
2020-08-18 12:26:45 +08:00
"syscall"
2020-06-25 10:46:45 +08:00
2021-01-21 01:30:16 +08:00
"github.com/gdamore/tcell/v2"
2020-06-19 16:29:51 +08:00
"github.com/rivo/tview"
2021-02-14 14:35:36 +08:00
"github.com/ztrue/tracerr"
2020-06-19 16:29:51 +08:00
)
2021-02-04 12:23:32 +08:00
// Panel is used to 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
}
2021-02-14 14:35:36 +08:00
// Default values for command line arguments.
2020-08-11 14:36:20 +08:00
const (
2021-02-15 11:56:02 +08:00
configPath = "~/.config/gomu/config"
cacheQueuePath = "~/.local/share/gomu/queue.cache"
musicPath = "~/music"
2020-08-11 14:36:20 +08:00
)
2020-07-23 15:15:39 +08:00
type Args struct {
config *string
2020-08-11 13:39:02 +08:00
empty *bool
2020-07-23 15:15:39 +08:00
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", configPath, "Specify config file"),
2020-08-11 13:39:02 +08:00
empty: flag.Bool("empty", false, "Open gomu with empty queue. Does not override previous queue"),
music: flag.String("music", musicPath, "Specify music directory"),
version: flag.Bool("version", false, "Print gomu version"),
}
2020-07-23 15:15:39 +08:00
flag.Parse()
return ar
}
2021-02-15 20:35:02 +08:00
// built-in functions
func defineBuiltins() {
gomu.anko.Define("debug_popup", debugPopup)
gomu.anko.Define("input_popup", inputPopup)
gomu.anko.Define("show_popup", defaultTimedPopup)
gomu.anko.Define("search_popup", searchPopup)
gomu.anko.Define("shell", shell)
}
2021-02-14 14:35:36 +08:00
// executes user config with default config is executed first in order to apply
// default values
func execConfig(config string) error {
const defaultConfig = `
// confirmation popup to add the whole playlist to the queue
confirm_bulk_add = true
confirm_on_exit = true
queue_loop = false
load_prev_queue = true
popup_timeout = "5s"
// change this to directory that contains mp3 files
music_dir = "~/music"
// url history of downloaded audio will be saved here
history_path = "~/.local/share/gomu/urls"
// some of the terminal supports unicode character
// you can set this to true to enable emojis
use_emoji = true
// initial volume when gomu starts up
volume = 80
// if you experiencing error using this invidious instance, you can change it
// to another instance from this list:
// https://github.com/iv-org/documentation/blob/master/Invidious-Instances.md
invidious_instance = "https://vid.puffyan.us"
// default emoji here is using awesome-terminal-fonts
// you can change these to your liking
emoji_playlist = ""
emoji_file = ""
emoji_loop = "ﯩ"
emoji_noloop = ""
// not all colors can be reproducible in terminal
// changing hex colors may or may not produce expected result
color_accent = "#008B8B"
color_background = "none"
color_foreground = "#FFFFFF"
color_now_playing_title = "#017702"
color_playlist = "#008B8B"
color_popup = "#0A0F14"
// vim: syntax=go
`
cfg := expandTilde(config)
_, err := os.Stat(cfg)
if os.IsNotExist(err) {
err = appendFile(cfg, defaultConfig)
if err != nil {
return tracerr.Wrap(err)
}
}
content, err := ioutil.ReadFile(cfg)
if err != nil {
return tracerr.Wrap(err)
}
// execute default config
_, err = gomu.anko.Execute(defaultConfig)
if err != nil {
return tracerr.Wrap(err)
}
// execute user config
_, err = gomu.anko.Execute(string(content))
if err != nil {
return tracerr.Wrap(err)
}
return nil
}
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-08-22 14:41:03 +08:00
// Assigning to global variable gomu
gomu = newGomu()
gomu.command.defineCommands()
2021-02-15 20:35:02 +08:00
defineBuiltins()
2021-02-13 22:49:26 +08:00
err := execConfig(expandFilePath(*args.config))
if err != nil {
2021-02-15 12:14:03 +08:00
die(err)
}
gomu.args = args
gomu.colors = newColor()
2020-08-22 14:41:03 +08:00
2020-06-19 16:29:51 +08:00
// override default border
// change double line border to one line border when focused
2020-08-22 14:41:03 +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
2020-07-06 18:58:27 +08:00
tview.Borders.BottomRightFocus = tview.Borders.BottomRight
2020-08-22 14:41:03 +08:00
tview.Styles.PrimitiveBackgroundColor = gomu.colors.background
2020-07-28 11:24:58 +08:00
2020-08-11 13:39:02 +08:00
gomu.initPanels(application, args)
2020-06-19 16:29:51 +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
2021-02-10 15:06:08 +08:00
gomu.playingBar.setDefault()
2021-02-14 14:35:36 +08:00
isQueueLoop := gomu.anko.GetBool("queue_loop")
gomu.player.isLoop = isQueueLoop
2021-02-03 10:50:01 +08:00
gomu.queue.isLoop = gomu.player.isLoop
2021-01-31 22:38:49 +08:00
2021-02-14 14:35:36 +08:00
loadQueue := gomu.anko.GetBool("load_prev_queue")
if !*args.empty && loadQueue {
2020-08-18 12:26:45 +08:00
// load saved queue from previous session
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-08-18 12:26:45 +08:00
sigs := make(chan os.Signal, 1)
2021-02-03 10:50:01 +08:00
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
2020-08-18 12:26:45 +08:00
go func() {
sig := <-sigs
errMsg := fmt.Sprintf("Received %s. Exiting program", sig.String())
logError(errors.New(errMsg))
err := gomu.quit(args)
if err != nil {
2021-02-03 10:50:01 +08:00
logError(errors.New("unable to quit program"))
2020-08-18 12:26:45 +08:00
}
}()
// global keybindings are handled here
application.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
2020-06-19 16:29:51 +08:00
if gomu.pages.HasPage("repl-input-popup") {
return e
}
2020-07-24 15:22:50 +08:00
popupName, _ := gomu.pages.GetFrontPage()
// disables keybindings when writing in input fields
if strings.Contains(popupName, "-input-") {
return e
2020-07-24 15:22:50 +08:00
}
switch e.Key() {
2020-06-19 16:29:51 +08:00
// cycle through each section
case tcell.KeyTAB:
2020-07-24 15:22:50 +08:00
if strings.Contains(popupName, "confirmation-") {
return e
}
gomu.cyclePanels2()
2020-06-19 16:29:51 +08:00
}
kb := string(e.Rune())
2021-02-15 14:27:45 +08:00
if gomu.anko.KeybindExists("Global", kb) {
2021-02-15 14:27:45 +08:00
err := gomu.anko.ExecKeybind("Global", kb)
if err != nil {
errorPopup(err)
}
return nil
}
cmds := map[rune]string{
'q': "quit",
' ': "toggle_pause",
'+': "volume_up",
2021-01-27 01:10:48 +08:00
'=': "volume_up",
'-': "volume_down",
2021-01-27 01:10:48 +08:00
'_': "volume_down",
'n': "skip",
':': "command_search",
'?': "toggle_help",
2021-01-27 01:10:48 +08:00
'f': "forward",
'F': "forward_fast",
'b': "rewind",
'B': "rewind_fast",
}
2020-06-21 23:47:12 +08:00
for key, cmd := range cmds {
if e.Rune() != key {
continue
2020-06-26 17:09:15 +08:00
}
fn, err := gomu.command.getFn(cmd)
if err != nil {
logError(err)
return e
2020-06-27 00:09:59 +08:00
}
fn()
2020-06-19 16:29:51 +08:00
}
return e
2020-06-19 16:29:51 +08:00
})
// 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
})
2021-02-03 10:50:01 +08:00
go populateAudioLength(gomu.playlist.GetRoot())
2020-06-19 16:29:51 +08:00
// 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
}
2020-06-19 16:29:51 +08:00
}