From 4b0ea68d7af9a6031a7ffda7ad66e0cb83315750 Mon Sep 17 00:00:00 2001 From: Kujtim Hoxha Date: Fri, 21 Mar 2025 18:20:28 +0100 Subject: initial --- cmd/termai/main.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 cmd/termai/main.go (limited to 'cmd') diff --git a/cmd/termai/main.go b/cmd/termai/main.go new file mode 100644 index 000000000..5b71728d8 --- /dev/null +++ b/cmd/termai/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "context" + "sync" + + tea "github.com/charmbracelet/bubbletea" + "github.com/kujtimiihoxha/termai/internal/logging" + "github.com/kujtimiihoxha/termai/internal/tui" +) + +var log = logging.Get() + +func main() { + log.Info("Starting termai...") + ctx := context.Background() + + app := tea.NewProgram( + tui.New(), + tea.WithAltScreen(), + ) + log.Info("Setting up subscriptions...") + ch, unsub := setupSubscriptions(ctx) + defer unsub() + + go func() { + for msg := range ch { + app.Send(msg) + } + }() + if _, err := app.Run(); err != nil { + panic(err) + } +} + +func setupSubscriptions(ctx context.Context) (chan tea.Msg, func()) { + ch := make(chan tea.Msg) + wg := sync.WaitGroup{} + ctx, cancel := context.WithCancel(ctx) + + { + sub := log.Subscribe(ctx) + wg.Add(1) + go func() { + for ev := range sub { + ch <- ev + } + wg.Done() + }() + } + // cleanup function to be invoked when program is terminated. + return ch, func() { + cancel() + // Wait for relays to finish before closing channel, to avoid sends + // to a closed channel, which would result in a panic. + wg.Wait() + close(ch) + } +} -- cgit v1.2.3