From 822c234a82d94ca58562631f7ef14ab3094546b2 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Tue, 19 May 2020 13:16:02 -0400 Subject: [PATCH] Use a mutual exclusion lock when writing to stdout --- boba.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/boba.go b/boba.go index 2cb1faa..ee03f2e 100644 --- a/boba.go +++ b/boba.go @@ -4,6 +4,7 @@ import ( "io" "os" "strings" + "sync" "github.com/muesli/termenv" ) @@ -45,6 +46,8 @@ type Program struct { init Init update Update view View + + mutex sync.Mutex } // Quit is a command that tells the program to exit @@ -64,6 +67,8 @@ func NewProgram(init Init, update Update, view View) *Program { init: init, update: update, view: view, + + mutex: sync.Mutex{}, } } @@ -156,6 +161,8 @@ func (p *Program) Start() error { func (p *Program) render(model Model, linesRendered int) int { view := p.view(model) + p.mutex.Lock() + // We need to add carriage returns to ensure that the cursor travels to the // start of a column after a newline view = strings.Replace(view, "\n", "\r\n", -1) @@ -164,6 +171,8 @@ func (p *Program) render(model Model, linesRendered int) int { termenv.ClearLines(linesRendered) } _, _ = io.WriteString(os.Stdout, view) + + p.mutex.Unlock() return strings.Count(view, "\r\n") }