-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (91 loc) · 2.91 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"os"
"sync"
"github.com/manish-mehra/govibes/lib"
"github.com/manish-mehra/govibes/ui"
)
const baseAudioFilesPath string = "./audio"
func main() {
paths := lib.GetAudioFilesPath(baseAudioFilesPath)
var wg sync.WaitGroup
// get args
args := os.Args[1:]
var ctx context.Context
var cancel context.CancelFunc // holds the cancel function of the previous sound
// if no args, start application in interactive mode
if len(args) == 0 {
ui.Ui_Main()
} else {
arg := args[0]
switch arg {
case "help":
fmt.Printf("%s \n\n", ui.TitleStyle("Help"))
fmt.Printf("%s \n", lib.PrintHelp())
// govibes list
case "sounds":
fmt.Printf("%s \n\n", ui.TitleStyle("Available Sounds"))
audio := lib.GetAudioFilesPath("./audio")
for key := range audio {
fmt.Printf("> %s \n", key)
}
fmt.Printf("\n\n")
case "default":
fmt.Printf("%s \n\n", ui.AsciiTitle)
loadedPreferences, err := ui.LoadPreferences()
if err != nil {
log.Fatal(err)
}
if loadedPreferences.LastKeyboardDev != "" || loadedPreferences.LastKeyboardDevPath != "" || loadedPreferences.LastKeyboardSound != "" {
// get config json & sound file path based on selected sound
configPaths, err := lib.GetConfigPaths(paths[loadedPreferences.LastKeyboardSound])
if err != nil {
panic(err)
}
// Cancel previous sound if it's playing
if cancel != nil {
cancel()
}
ctx, cancel = context.WithCancel(context.Background())
wg.Add(1)
go lib.ListenKeyboardInput(ctx, configPaths.ConfigJson, configPaths.SoundFilePath, loadedPreferences.LastKeyboardDevPath)
fmt.Printf("%s %s \n\n", ui.InputDeviceStyle(loadedPreferences.LastKeyboardDev), ui.SoundStyle(loadedPreferences.LastKeyboardSound))
} else {
fmt.Printf("%s \n\n", ui.AlertStyle("No default configrations are found!"))
}
case lib.GetKeyAsString(paths, arg):
/*
* Pass sound as args
* Use keyboard device from preference.json as default input channel
*/
fmt.Printf("%s \n\n", ui.AsciiTitle)
loadedPreferences, err := ui.LoadPreferences()
if err != nil {
log.Fatal(err)
}
if loadedPreferences.LastKeyboardDev != "" && loadedPreferences.LastKeyboardDevPath != "" {
// get config json & sound file path based on selected sound
configPaths, err := lib.GetConfigPaths(paths[arg])
if err != nil {
panic(err)
}
// Cancel previous sound if it's playing
if cancel != nil {
cancel()
}
ctx, cancel = context.WithCancel(context.Background())
wg.Add(1)
go lib.ListenKeyboardInput(ctx, configPaths.ConfigJson, configPaths.SoundFilePath, loadedPreferences.LastKeyboardDevPath)
fmt.Printf("%s %s \n\n", ui.InputDeviceStyle(loadedPreferences.LastKeyboardDev), ui.SoundStyle(arg))
} else {
fmt.Printf("%s \n\n", ui.AlertStyle("No default input channel found!"))
}
default:
fmt.Println("unknown args")
}
}
wg.Wait()
}