-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (72 loc) · 2.04 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
package main
import (
"fmt"
"log"
"log/slog"
"os"
"github.com/jba/slog/handlers/loghandler"
"github.com/thiagokokada/twenty-twenty-twenty/internal/core"
"github.com/thiagokokada/twenty-twenty-twenty/internal/ctxlog"
"github.com/thiagokokada/twenty-twenty-twenty/internal/notification"
"github.com/thiagokokada/twenty-twenty-twenty/internal/sound"
)
var (
version = "development"
twenty *core.TwentyTwentyTwenty
)
func main() {
lvl := new(slog.LevelVar)
lvl.Set(slog.LevelInfo)
handler := &ctxlog.ContextHandler{
Handler: loghandler.New(
os.Stdout,
&slog.HandlerOptions{Level: lvl},
),
}
logger := slog.New(handler)
slog.SetDefault(logger)
features := core.Features{Sound: sound.Enabled, Systray: systrayEnabled}
settings := core.ParseFlags(os.Args[0], os.Args[1:], version, features)
if settings.Verbose {
lvl.Set(slog.LevelDebug)
}
if features.Sound {
err := sound.Init(!settings.Sound)
if err != nil {
log.Printf("Error while initialising sound: %v\n", err)
log.Println("Disabling sound")
features.Sound = false
settings.Sound = false
}
}
sentNotification, err := notification.Send(
&settings.Sound,
"Starting 20-20-20",
fmt.Sprintf("You will see a notification every %.f minutes(s)", settings.Frequency.Minutes()),
)
if err != nil {
log.Fatalf("Test notification failed: %v. Exiting...", err)
}
twenty = core.New(features, settings)
// we need to start notification cancellation in a goroutine to show the
// systray as soon as possible (since it depends on the loop() call), but we
// also need to give it access to the core.Ctx to cancel it if necessary
twenty.Start()
go func() {
if features.Sound {
// wait the 1.5x of duration so we have some time for the sounds to
// finish playing
go sound.SuspendAfter(min(settings.Duration*3/2, settings.Frequency))
}
err := notification.CancelAfter(
twenty.Ctx(),
sentNotification,
&twenty.Settings.Duration,
&twenty.Settings.Sound,
)
if err != nil {
log.Printf("Test notification cancel failed: %v\n", err)
}
}()
loop()
}