1package main
2
3import (
4 "context"
5 "sync"
6
7 tea "github.com/charmbracelet/bubbletea"
8 "github.com/kujtimiihoxha/termai/internal/logging"
9 "github.com/kujtimiihoxha/termai/internal/tui"
10)
11
12var log = logging.Get()
13
14func main() {
15 log.Info("Starting termai...")
16 ctx := context.Background()
17
18 app := tea.NewProgram(
19 tui.New(),
20 tea.WithAltScreen(),
21 )
22 log.Info("Setting up subscriptions...")
23 ch, unsub := setupSubscriptions(ctx)
24 defer unsub()
25
26 go func() {
27 for msg := range ch {
28 app.Send(msg)
29 }
30 }()
31 if _, err := app.Run(); err != nil {
32 panic(err)
33 }
34}
35
36func setupSubscriptions(ctx context.Context) (chan tea.Msg, func()) {
37 ch := make(chan tea.Msg)
38 wg := sync.WaitGroup{}
39 ctx, cancel := context.WithCancel(ctx)
40
41 {
42 sub := log.Subscribe(ctx)
43 wg.Add(1)
44 go func() {
45 for ev := range sub {
46 ch <- ev
47 }
48 wg.Done()
49 }()
50 }
51 // cleanup function to be invoked when program is terminated.
52 return ch, func() {
53 cancel()
54 // Wait for relays to finish before closing channel, to avoid sends
55 // to a closed channel, which would result in a panic.
56 wg.Wait()
57 close(ch)
58 }
59}