commit 67080321d318b0a7c24d36c91bde2bcc8197678f
parent 952c91f230f2f896eb2bed42929d5eed70ae562f
Author: dwrz <dwrz@dwrz.net>
Date: Thu, 8 Dec 2022 22:14:31 +0000
Refactor editor to use message pkg
Diffstat:
6 files changed, 31 insertions(+), 23 deletions(-)
diff --git a/pkg/editor/buffers.go b/pkg/editor/buffers.go
@@ -7,6 +7,7 @@ import (
"path/filepath"
"code.dwrz.net/src/pkg/editor/buffer"
+ "code.dwrz.net/src/pkg/editor/message"
)
// load files into editor buffers.
@@ -43,12 +44,10 @@ func (e *Editor) load(files []string) {
}
// If there was an error, report it to the user.
if err != nil {
- e.messages <- Message{
- Text: fmt.Sprintf(
- "failed to load buffer %s: %v",
- name, err,
- ),
- }
+ e.messages <- message.New(fmt.Sprintf(
+ "failed to load buffer %s: %v",
+ name, err,
+ ))
continue
}
@@ -61,7 +60,7 @@ func (e *Editor) load(files []string) {
}
}
- e.messages <- Message{Text: "loaded buffers"}
+ e.messages <- message.New("loaded buffers")
}
// setBuffer stores a buffer in the editor's buffer map.
diff --git a/pkg/editor/editor.go b/pkg/editor/editor.go
@@ -6,6 +6,7 @@ import (
"sync"
"code.dwrz.net/src/pkg/editor/buffer"
+ "code.dwrz.net/src/pkg/editor/message"
"code.dwrz.net/src/pkg/log"
"code.dwrz.net/src/pkg/terminal"
)
@@ -15,8 +16,8 @@ type Editor struct {
in *os.File
input chan input
log *log.Logger
- messages chan Message
out *os.File
+ messages chan *message.Message
terminal *terminal.Terminal
tmpdir string
@@ -40,8 +41,8 @@ func New(p Parameters) (*Editor, error) {
in: p.In,
input: make(chan input),
log: p.Log,
- messages: make(chan Message),
out: p.Out,
+ messages: make(chan *message.Message),
terminal: p.Terminal,
tmpdir: p.TempDir,
}
diff --git a/pkg/editor/input.go b/pkg/editor/input.go
@@ -5,6 +5,7 @@ import (
"fmt"
"unicode/utf8"
+ "code.dwrz.net/src/pkg/editor/message"
"code.dwrz.net/src/pkg/terminal"
)
@@ -254,17 +255,13 @@ func (e *Editor) processInput(input input) error {
case Save:
if err := e.active.Save(); err != nil {
go func() {
- e.messages <- Message{
- Text: fmt.Sprintf(
- "failed to save: %v", err,
- ),
- }
+ e.messages <- message.New(fmt.Sprintf(
+ "failed to save: %v", err,
+ ))
}()
}
go func() {
- e.messages <- Message{
- Text: fmt.Sprintf("saved file"),
- }
+ e.messages <- message.New("saved file")
}()
default:
diff --git a/pkg/editor/message.go b/pkg/editor/message.go
@@ -1,5 +0,0 @@
-package editor
-
-type Message struct {
- Text string
-}
diff --git a/pkg/editor/message/message.go b/pkg/editor/message/message.go
@@ -0,0 +1,15 @@
+package message
+
+import "time"
+
+type Message struct {
+ Text string
+ Time time.Time
+}
+
+func New(text string) *Message {
+ return &Message{
+ Text: text,
+ Time: time.Now(),
+ }
+}
diff --git a/pkg/editor/run.go b/pkg/editor/run.go
@@ -6,6 +6,7 @@ import (
"code.dwrz.net/src/pkg/build"
"code.dwrz.net/src/pkg/terminal"
+ "code.dwrz.net/src/pkg/editor/message"
)
func (e *Editor) Run(files []string) error {
@@ -30,7 +31,7 @@ func (e *Editor) Run(files []string) error {
// Set the initial message.
go func() {
time.Sleep(1 * time.Second)
- e.messages <- Message{Text: "Ctrl-Q: Quit"}
+ e.messages <- message.New("Ctrl-Q: Quit")
}()
// Main loop.