aboutsummaryrefslogtreecommitdiff
path: root/editor/editor.go
diff options
context:
space:
mode:
authorPetter Rodhelind <petter.rodhelind@gmail.com>2018-02-26 23:51:03 +0100
committerPetter Rodhelind <petter.rodhelind@gmail.com>2018-02-26 23:51:03 +0100
commit2395485d075f80117fe3ce25ef339bb1ffecf160 (patch)
tree87cabebd5ba1c7210adb5dabe253310f17694931 /editor/editor.go
parent0023e0929ac7075cd008e0093de58ddc89efd597 (diff)
downloadpoe-2395485d075f80117fe3ce25ef339bb1ffecf160.tar.gz
poe-2395485d075f80117fe3ce25ef339bb1ffecf160.tar.bz2
poe-2395485d075f80117fe3ce25ef339bb1ffecf160.zip
Total redesign. Separating editor parts from UI.
Diffstat (limited to 'editor/editor.go')
-rw-r--r--editor/editor.go107
1 files changed, 107 insertions, 0 deletions
diff --git a/editor/editor.go b/editor/editor.go
new file mode 100644
index 0000000..67f0ab3
--- /dev/null
+++ b/editor/editor.go
@@ -0,0 +1,107 @@
+package editor
+
+import (
+ "path/filepath"
+ "time"
+
+ "github.com/prodhe/poe/gapbuffer"
+)
+
+// Editor is the edit component that holds text buffers. A UI of some sort operates on the editor to manipulate buffers.
+type Editor interface {
+ NewBuffer() (id int64, buf *Buffer)
+ Buffer(id int64) *Buffer
+ Buffers() ([]int64, []*Buffer)
+ Current() *Buffer
+ LoadBuffers(filenames []string)
+ CloseBuffer(id int64)
+ WorkDir() string
+ Len() int
+ Run(cmd string)
+}
+
+// New returns an empty editor with no buffers loaded.
+func New() Editor {
+ e := &editor{}
+ e.buffers = map[int64]*Buffer{}
+ e.workdir, _ = filepath.Abs(".")
+ e.initCommands()
+ return e
+}
+
+// editor implements Editor.
+type editor struct {
+ buffers map[int64]*Buffer
+ current int64 // id ref to current buffer
+ workdir string
+}
+
+// NewBuffer creates an empty buffer and appends it to the editor. Returns the new id and the new buffer.
+func (e *editor) NewBuffer() (id int64, buf *Buffer) {
+ buf = &Buffer{buf: &gapbuffer.Buffer{}}
+ id = e.genBufferID()
+ e.buffers[id] = buf
+ e.current = id
+ return id, buf
+}
+
+// Buffer returns the buffer with given index. Nil if id not found.
+func (e *editor) Buffer(id int64) *Buffer {
+ if _, ok := e.buffers[id]; ok {
+ e.current = id
+ }
+ return e.buffers[id]
+}
+
+// Buffers returns a slice of IDs and a slice of buffers.
+func (e *editor) Buffers() ([]int64, []*Buffer) {
+ ids := make([]int64, 0, len(e.buffers))
+ bs := make([]*Buffer, 0, len(e.buffers))
+ for i, b := range e.buffers {
+ ids = append(ids, i)
+ bs = append(bs, b)
+ }
+ return ids, bs
+}
+
+// Current returns the current buffer.
+func (e *editor) Current() *Buffer {
+ return e.buffers[e.current]
+}
+
+// CloseBuffer deletes the given buffer from memory. No warnings. Here be dragons.
+func (e *editor) CloseBuffer(id int64) {
+ delete(e.buffers, id)
+}
+
+// Len returns number of buffers currently in the editor.
+func (e *editor) Len() int {
+ return len(e.buffers)
+}
+
+// WorkDir returns the base working directory of the editor.
+func (e *editor) WorkDir() string {
+ if e.workdir == "" {
+ d, _ := filepath.Abs(".")
+ return d
+ }
+ return e.workdir
+}
+
+// LoadBuffers reads files from disk and loads them into windows. Screen need to be initialized.
+func (e *editor) LoadBuffers(fns []string) {
+ // load given filenames and append to buffer list
+ for _, fn := range fns {
+ _, buf := e.NewBuffer()
+ buf.NewFile(fn)
+ buf.ReadFile()
+ }
+
+ if len(fns) == 0 {
+ e.NewBuffer()
+ }
+}
+
+func (e *editor) genBufferID() int64 {
+ return time.Now().UnixNano()
+}