Skip to content

Updating the UI with a writer correctly #199

Open
@amlwwalker

Description

@amlwwalker

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions