-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
130 lines (107 loc) · 2.94 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"flag"
"fmt"
"log"
"log/slog"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"github.com/MatusOllah/slogcolor"
"github.com/gopxl/beep"
"github.com/gopxl/beep/speaker"
)
func getLogLevel(s string) slog.Leveler {
switch strings.ToLower(s) {
case "debug":
return slog.LevelDebug
case "info":
return slog.LevelInfo
case "warn":
return slog.LevelWarn
case "error":
return slog.LevelError
default:
return slog.LevelInfo
}
}
func showVersion() {
a := app.New()
fmt.Printf("%s version v%s\n", a.Metadata().Name, a.Metadata().Version)
fmt.Printf("Go version %s\n", runtime.Version())
}
func main() {
path := flag.String("open-file", "", "Open a file")
logLevel := flag.String("log-level", "info", "Log level (\"debug\", \"info\", \"warn\", \"error\")")
sr := flag.Int("sample-rate", 8000, "Project Sample Rate")
_mode := flag.String("mode", "Bytebeat", "The mode (\"Bytebeat\", \"Floatbeat\")")
version := flag.Bool("version", false, "Show version and exit")
flag.Parse()
slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, &slogcolor.Options{
Level: getLogLevel(*logLevel),
TimeFormat: time.DateTime,
SrcFileMode: slogcolor.ShortFile,
})))
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile)
if *version {
showVersion()
os.Exit(0)
}
slog.Info("Initializing")
beforeInit := time.Now()
a := app.New()
a.Lifecycle().SetOnStarted(func() {
slog.Info("Lifecycle: Started")
})
a.Lifecycle().SetOnStopped(func() {
slog.Info("Lifecycle: Stopped")
})
a.Lifecycle().SetOnEnteredForeground(func() {
slog.Info("Lifecycle: Entered Foreground")
})
a.Lifecycle().SetOnExitedForeground(func() {
slog.Info("Lifecycle: Exited Foreground")
})
slog.Info(fmt.Sprintf("gecfg-editor version %s", a.Metadata().Version))
slog.Info(fmt.Sprintf("Go version %s", runtime.Version()))
slog.Info("initializing speaker")
sampleRate = beep.SampleRate(a.Preferences().IntWithFallback("OutputSampleRate", 48000))
slog.Info("using output sample rate", "sampleRate", sampleRate)
speaker.Init(sampleRate, 1024)
bbSampleRate = beep.SampleRate(*sr)
if *_mode != "Bytebeat" && *_mode != "Floatbeat" {
panic("invalid mode: " + *_mode)
}
mode = *_mode
w := a.NewWindow(openFileName + " - " + a.Metadata().Name)
w.SetMaster()
w.Resize(fyne.NewSize(1280, 720))
w.SetMainMenu(makeMainMenu(a, w))
w.SetContent(makeUI(a, w))
slog.Info(fmt.Sprintf("Initialization took %s", time.Since(beforeInit)))
// open file
if *path != "" {
slog.Info("opening file", "path", *path)
content, err := os.ReadFile(*path)
if err != nil {
slog.Error(err.Error())
panic(err)
}
expr := string(content)
openFileName = filepath.Base(*path)
openFilePath = *path
exprEntry.SetText(expr)
if err := bbgen.SetExpr(expr); err != nil {
slog.Error(err.Error())
panic(err)
}
updateWindowTitle(a, w)
}
w.ShowAndRun()
slog.Info("exiting")
os.Exit(0)
}