forked from issadarkthing/gomu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.go
275 lines (216 loc) · 5.81 KB
/
start.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright (C) 2020 Raziman
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"github.com/spf13/viper"
)
// Created so we can keep track of childrens in slices
type Panel interface {
HasFocus() bool
SetBorderColor(color tcell.Color) *tview.Box
SetTitleColor(color tcell.Color) *tview.Box
SetTitle(s string) *tview.Box
GetTitle() string
help() []string
}
const (
CONFIG_PATH = ".config/gomu/config"
MUSIC_PATH = "~/music"
)
// Reads config file and sets the options
func readConfig(args Args) {
// config path passed by flag
configPath := *args.config
home, err := os.UserHomeDir()
if err != nil {
logError(err)
}
defaultPath := path.Join(home, CONFIG_PATH)
if err != nil {
logError(err)
}
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(strings.TrimSuffix(expandFilePath(configPath), "/config"))
viper.AddConfigPath("$HOME/.config/gomu")
colors := map[string]string{
"color.foreground": "#FFFFFF",
"color.background": "none",
"color.accent": "#008B8B",
"color.popup": "#0A0F14",
"color.now_playing_title": "#017702",
"color.playlist": "#008B8B",
}
if err := viper.ReadInConfig(); err != nil {
// General config
viper.SetDefault("general.music_dir", MUSIC_PATH)
viper.SetDefault("general.confirm_on_exit", true)
viper.SetDefault("general.confirm_bulk_add", true)
viper.SetDefault("general.popup_timeout", "5s")
viper.SetDefault("general.volume", 100)
viper.SetDefault("general.load_prev_queue", true)
viper.SetDefault("general.use_emoji", true)
// Colors
for k, v := range colors {
viper.SetDefault(k, v)
}
// 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)
}
}
// if config file was not found
// copy default config to default config path
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
input, err := ioutil.ReadFile("config")
if err != nil {
logError(err)
}
err = ioutil.WriteFile(defaultPath, input, 0644)
if err != nil {
logError(err)
}
}
} else {
// Validate hex color
for k, v := range colors {
cfgColor := viper.GetString(k)
if validateHexColor(cfgColor) {
continue
}
// use default value if invalid hex color was given
viper.Set(k, v)
}
}
}
type Args struct {
config *string
empty *bool
music *string
version *bool
}
func getArgs() Args {
ar := Args{
config: flag.String("config", CONFIG_PATH, "Specify config file"),
empty: flag.Bool("empty", false, "Open gomu with empty queue. Does not override previous queue"),
music: flag.String("music", MUSIC_PATH, "Specify music directory"),
version: flag.Bool("version", false, "Print gomu version"),
}
flag.Parse()
return ar
}
// 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)
return flex
}
// Initialize
func start(application *tview.Application, args Args) {
// Print version and exit
if *args.version {
fmt.Printf("Gomu %s\n", VERSION)
return
}
// 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
var bgColor tcell.Color
bg := viper.GetString("color.background")
if bg == "none" {
bgColor = tcell.ColorDefault
} else {
bgColor = tcell.GetColor(bg)
}
tview.Styles.PrimitiveBackgroundColor = bgColor
// Assigning to global variable gomu
gomu = newGomu()
gomu.initPanels(application, args)
debugLog("App start")
flex := layout(gomu)
gomu.pages.AddPage("main", flex, true, true)
// sets the first focused panel
gomu.setFocusPanel(gomu.playlist)
gomu.prevPanel = gomu.playlist
if !*args.empty {
// load saved queue from previous
if err := gomu.queue.loadQueue(); err != nil {
logError(err)
}
}
application.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
popupName, _ := gomu.pages.GetFrontPage()
// disables keybindings when writing in input fields
if strings.Contains(popupName, "-input-") {
return event
}
switch event.Key() {
// cycle through each section
case tcell.KeyTAB:
if strings.Contains(popupName, "confirmation-") {
return event
}
gomu.cyclePanels()
}
switch event.Rune() {
case 'q':
if !viper.GetBool("general.confirm_on_exit") {
err := gomu.quit(args)
if err != nil {
logError(err)
}
}
exitConfirmation(args)
case ' ':
gomu.player.togglePause()
case '+':
v := int(gomu.player.volume*10) + 100
if v < 100 {
vol := gomu.player.setVolume(0.5)
volumePopup(vol)
}
case '-':
v := int(gomu.player.volume*10) + 100
if v > 0 {
vol := gomu.player.setVolume(-0.5)
volumePopup(vol)
}
case 'n':
gomu.player.skip()
case '?':
name, _ := gomu.pages.GetFrontPage()
if name == "help-page" {
gomu.pages.RemovePage(name)
gomu.app.SetFocus(gomu.prevPanel.(tview.Primitive))
} else {
helpPopup(gomu.prevPanel)
}
}
return event
})
// fix transparent background issue
application.SetBeforeDrawFunc(func(screen tcell.Screen) bool {
screen.Clear()
return false
})
// main loop
if err := application.SetRoot(gomu.pages, true).SetFocus(gomu.playlist).Run(); err != nil {
logError(err)
}
}