gcla / gowid

Compositional widgets for terminal user interfaces, written in Go, inspired by urwid.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[ QUESTION ] Update widget NOW

Peter2121 opened this issue · comments

I use edit widget to show command's output. According to the gowid documentation, I use
app.Run(gowid.RunFunction(func(app gowid.IApp) { <some code> app.Redraw() } construction to update the content of the edit widget. It works correctly, but the updates received by the widget are not very frequent, really I get only one update at the end of my command, so I cannot see the progress of command execution. Is there any way to do more frequent updates? I tried to use app.RedrawTerminal() somehow, but the application crashes sometimes, probably due to race conditions.

Hi @Peter2121 - one thing to watch for is to ensure RedrawTerminal is only called from the "main" UI-updating goroutine. If you use app.Run(...), that ensures your function will be issued in that way. But app.Run(...) will call RedrawTerminal automatically for you anyway (maybe that shouldn't always be the case - I'll think about that) - so the explicit call of yours to RedrawTerminal would be redundant here. Do you run app.Run(...) in a timed loop of some kind for regular updates to your display? editor.go has an example of that; a goroutine is started to wait for a timer to expire; when it does, the editor status bar is updated, but via app.Run(...) to ensure execution in the main gowid goroutine. If you don't mind pasting some of your application code, I will try to debug more.

In cbsd-tui.go at line 777 I check if there are something new in a file. If yes - I need to update edit widget named logspace immediately with the additional text from buf. So, I do the following:

			logText = logspace.Text() + string(buf[:rbytes]) + "\n"
			app.RunThenRenderEvent(gowid.RunFunction(func(app gowid.IApp) {
				logspace.SetText(logText, app)
				logspace.SetCursorPos(utf8.RuneCountInString(logspace.Text()), app)
				app.Sync()
			}))

It works, but it seems that there is a delay between the update of the text in my file and in the widget.