diff --git a/v3/events.go b/v3/events.go index 7ff6cf4e..390a0889 100644 --- a/v3/events.go +++ b/v3/events.go @@ -5,6 +5,7 @@ package termui import ( + "context" "fmt" tb "github.com/nsf/termbox-go" @@ -77,6 +78,23 @@ func PollEvents() <-chan Event { return ch } +// PollEventsWithContext same as PollEvents() prevents goroutine leaking using context. +func PollEventsWithContext(ctx context.Context) <-chan Event { + ch := make(chan Event) + go func() { + defer close(ch) + for { + select { + case <-ctx.Done(): + return + default: + ch <- convertTermboxEvent(tb.PollEvent()) + } + } + }() + return ch +} + var keyboardMap = map[tb.Key]string{ tb.KeyF1: "", tb.KeyF2: "",