Queue UI output and status updates

This commit is contained in:
Brandon McGinty (chatgpt)
2026-08-09 19:37:18 -04:00
committed by Brandon McGinty
parent b10e08477c
commit 12761f29ca
3 changed files with 51 additions and 4 deletions
+20
View File
@@ -23,6 +23,7 @@ type Ui struct {
close chan struct{}
closeOnce sync.Once
events chan func()
manager UiManager
drawCount int32
@@ -42,6 +43,7 @@ type uiElement struct {
func New(manager UiManager) *Ui {
ui := &Ui{
close: make(chan struct{}),
events: make(chan func(), 256),
elements: make(map[string]*uiElement),
manager: manager,
keyListeners: make(map[Key][]KeyListener),
@@ -55,6 +57,22 @@ func (ui *Ui) Close() {
ui.closeOnce.Do(func() { close(ui.close) })
}
// Post schedules UI work on Run's owning goroutine. It is deliberately
// bounded: network callbacks must not block behind slow terminal rendering.
func (ui *Ui) Post(fn func()) bool {
if fn == nil {
return true
}
select {
case <-ui.close:
return false
case ui.events <- fn:
return true
default:
return false
}
}
func (ui *Ui) Refresh() {
if termbox.IsInit {
ui.beginDraw()
@@ -137,6 +155,8 @@ func (ui *Ui) Run(cmds chan string) error {
select {
case <-ui.close:
return nil
case fn := <-ui.events:
fn()
case cmd, ok := <-cmds:
if !ok {
cmds = nil