Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating the UI with a writer correctly #199

Open
amlwwalker opened this issue Jun 27, 2019 · 1 comment
Open

Updating the UI with a writer correctly #199

amlwwalker opened this issue Jun 27, 2019 · 1 comment

Comments

@amlwwalker
Copy link

How can I update the UI (I am outputting logs to the UI) properly so that it updates automatically, rather than updating when I manually press the up/down arrows?

I am using the io.Writer interface:

func (cGui *clientGui) Write(p []byte) (n int, err error) {
	cGui.gui.Update(func(g *gocui.Gui) error {
		logCounter = logCounter + 1
		fmt.Fprintf(cGui.views["logging"], strconv.Itoa(logCounter)+" - "+string(p))
		return nil
	})
	return len(p), nil
}

The above works however if multiple things write to it at once, then the logs get jumbled on the console. The image below is the output, as you can see, lines 11,12,13 have been jumbled with the lines 10 and 14 (the number on the left is the logCounter variable from above, and should be consecutive.
image

If I do

func (cGui *clientGui) Write(p []byte) (n int, err error) {
	fmt.Fprintf(cGui.views["logging"], strconv.Itoa(logCounter)+" - "+string(p))
	return len(p), nil
}

It works, but I have to manually "do something" like press a key to get the log output to appear.

Thanks

@cheeseprocedure
Copy link

I'm late to the game here, but hopefully this'll help someone: when I ran into this, the solution was to wrap the write to the view in *Gui.Update.

From the Godocs:

IMPORTANT: Views can only be created, destroyed or updated in three ways: from the Layout function within managers, from keybinding callbacks or via *Gui.Update(). The reason for this is that it allows gocui to be concurrent-safe. So, if you want to update your GUI from a goroutine, you must use *Gui.Update().

Moving my fmt.Fprintln calls into g.Update resulted in realtime updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants