commit 1d95780f78f454d1876b5428d531aea18ecf020e
parent 33b54b8c0a21d99fb62c0d6461d1872bfba99306
Author: dwrz <dwrz@dwrz.net>
Date: Sat, 10 Dec 2022 14:33:42 +0000
Fix editor canvas
Reset the buffer after rendering.
Protect the reused buffer with a lock.
Diffstat:
3 files changed, 16 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)
@@ -50,10 +53,11 @@ func (c *Canvas) Render(b *buffer.Buffer, msg *message.Message) error {
c.out.Write(c.buf.Bytes())
+ c.buf.Reset()
+
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,