commit 7d6ced9884a2fc1921718b5b19be7d11e98ee2a6
parent 33b54b8c0a21d99fb62c0d6461d1872bfba99306
Author: dwrz <dwrz@dwrz.net>
Date: Sat, 10 Dec 2022 14:33:42 +0000
Fix editor canvas
Protect the reused buffer with a lock.
Diffstat:
3 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/pkg/editor/canvas/canvas.go b/pkg/editor/canvas/canvas.go
@@ -3,23 +3,30 @@ package canvas
import (
"bytes"
"os"
+ "sync"
+ "code.dwrz.net/src/pkg/log"
"code.dwrz.net/src/pkg/terminal"
)
type Canvas struct {
- buf bytes.Buffer
+ log *log.Logger
out *os.File
terminal *terminal.Terminal
+
+ mu sync.Mutex
+ buf bytes.Buffer
}
type Parameters struct {
+ Log *log.Logger
Out *os.File
Terminal *terminal.Terminal
}
func New(p Parameters) *Canvas {
return &Canvas{
+ log: p.Log,
out: p.Out,
terminal: p.Terminal,
}
diff --git a/pkg/editor/canvas/render.go b/pkg/editor/canvas/render.go
@@ -12,6 +12,9 @@ import (
)
func (c *Canvas) Render(b *buffer.Buffer, msg *message.Message) error {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+
size, err := c.terminal.Size()
if err != nil {
return fmt.Errorf("failed to get terminal size: %w", err)
@@ -53,7 +56,6 @@ func (c *Canvas) Render(b *buffer.Buffer, msg *message.Message) error {
return nil
}
-// TODO: show a character cursor, not the terminal cursor.
func (c *Canvas) statusBar(b *buffer.Buffer, width, y, x int) {
c.buf.Write([]byte(terminal.EraseLine))
c.buf.WriteString(color.Inverse)
diff --git a/pkg/editor/editor.go b/pkg/editor/editor.go
@@ -39,7 +39,9 @@ func New(p Parameters) (*Editor, error) {
var editor = &Editor{
buffers: map[string]*buffer.Buffer{},
canvas: canvas.New(canvas.Parameters{
- Out: p.Out, Terminal: p.Terminal,
+ Log: p.Log,
+ Out: p.Out,
+ Terminal: p.Terminal,
}),
errs: make(chan error),
in: p.In,