gizak / termui

Golang terminal dashboard
MIT License
13.06k stars 783 forks source link

input stops working when rendering too often #258

Open bboozzoo opened 4 years ago

bboozzoo commented 4 years ago

Looks like a bug or a race, but I can't pinpoint the exact reason. Consider the following snippet:

package main

import (
    "flag"
    "fmt"
    "log"

    ui "github.com/gizak/termui/v3"
    "github.com/gizak/termui/v3/widgets"
)

type state struct {
    pressed uint
    draw    uint
}

func prompt(st *state) {
    st.draw++

    ui.Clear()
    p := widgets.NewParagraph()
    p.Text = fmt.Sprintf("[%v] pressed %v times", st.draw, st.pressed)
    p.SetRect(0, 0, 25, 5)

    ui.Render(p)
}

func main() {
    flag.Parse()

    if err := ui.Init(); err != nil {
        log.Fatalf("failed to initialize termui: %v", err)
    }
    defer ui.Close()

    st := &state{}

    prompt(st)
    events := ui.PollEvents()
    for {
        e := <-events
        st.pressed++
        if e.Type == ui.KeyboardEvent && e.ID == "<Escape>" {
            break
        }
        prompt(st)
    }
}

I'm running this on Linux, At a point where I reach ~80 key presses, the input stops working. I've written a small snippet that uses only termbox-go to read input and print the state, and the input does not block. Using ui.PollEvents() and rendering test through termbox-go works well too. However, combining ui.Render() and ui.PollEvents(), blocks after registering some number of input events.