aboutsummaryrefslogtreecommitdiff
path: root/editor/editor.go
diff options
context:
space:
mode:
authorPetter Rodhelind <petter.rodhelind@gmail.com>2018-03-16 12:33:26 +0100
committerPetter Rodhelind <petter.rodhelind@gmail.com>2018-03-16 12:33:26 +0100
commitfea1a1adbb03d657724be9b9994ba6b11dfe625f (patch)
tree59632674ec570a8f1db6d3154ce49b255ac63b6e /editor/editor.go
parent2395485d075f80117fe3ce25ef339bb1ffecf160 (diff)
downloadpoe-fea1a1adbb03d657724be9b9994ba6b11dfe625f.tar.gz
poe-fea1a1adbb03d657724be9b9994ba6b11dfe625f.tar.bz2
poe-fea1a1adbb03d657724be9b9994ba6b11dfe625f.zip
Redesign.
Diffstat (limited to 'editor/editor.go')
-rw-r--r--editor/editor.go79
1 files changed, 56 insertions, 23 deletions
diff --git a/editor/editor.go b/editor/editor.go
index 67f0ab3..87b3eff 100644
--- a/editor/editor.go
+++ b/editor/editor.go
@@ -1,7 +1,11 @@
package editor
import (
+ "fmt"
+ "os"
+ "os/exec"
"path/filepath"
+ "strings"
"time"
"github.com/prodhe/poe/gapbuffer"
@@ -12,49 +16,45 @@ 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)
+ Edit(bufid int64, cmd string) string
}
// New returns an empty editor with no buffers loaded.
func New() Editor {
- e := &editor{}
+ e := &ed{}
e.buffers = map[int64]*Buffer{}
e.workdir, _ = filepath.Abs(".")
- e.initCommands()
return e
}
-// editor implements Editor.
-type editor struct {
+// ed implements Editor.
+type ed 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) {
+func (e *ed) 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 {
+func (e *ed) Buffer(id int64) *Buffer {
if _, ok := e.buffers[id]; ok {
- e.current = id
+ return e.buffers[id]
}
- return e.buffers[id]
+ return nil
}
// Buffers returns a slice of IDs and a slice of buffers.
-func (e *editor) Buffers() ([]int64, []*Buffer) {
+func (e *ed) Buffers() ([]int64, []*Buffer) {
ids := make([]int64, 0, len(e.buffers))
bs := make([]*Buffer, 0, len(e.buffers))
for i, b := range e.buffers {
@@ -64,23 +64,18 @@ func (e *editor) Buffers() ([]int64, []*Buffer) {
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) {
+func (e *ed) CloseBuffer(id int64) {
delete(e.buffers, id)
}
// Len returns number of buffers currently in the editor.
-func (e *editor) Len() int {
+func (e *ed) Len() int {
return len(e.buffers)
}
// WorkDir returns the base working directory of the editor.
-func (e *editor) WorkDir() string {
+func (e *ed) WorkDir() string {
if e.workdir == "" {
d, _ := filepath.Abs(".")
return d
@@ -89,7 +84,7 @@ func (e *editor) WorkDir() string {
}
// LoadBuffers reads files from disk and loads them into windows. Screen need to be initialized.
-func (e *editor) LoadBuffers(fns []string) {
+func (e *ed) LoadBuffers(fns []string) {
// load given filenames and append to buffer list
for _, fn := range fns {
_, buf := e.NewBuffer()
@@ -102,6 +97,44 @@ func (e *editor) LoadBuffers(fns []string) {
}
}
-func (e *editor) genBufferID() int64 {
+func (e *ed) genBufferID() int64 {
return time.Now().UnixNano()
}
+
+func (e *ed) Edit(bufid int64, args string) string {
+ if len(args) < 1 {
+ return ""
+ }
+
+ if _, ok := e.buffers[bufid]; !ok {
+ // no such bufid
+ return ""
+ }
+
+ switch args[0] {
+ case 'f':
+ var names []string
+ for _, buf := range e.buffers {
+ names = append(names, fmt.Sprintf("%s", buf.Name()))
+ }
+ return fmt.Sprintf("buffers:\n%s", strings.Join(names, "\n"))
+ case '!':
+ os.Chdir(e.buffers[bufid].WorkDir())
+ cmd := strings.Split(string(args[1:]), " ")
+ path, err := exec.LookPath(cmd[0])
+ if err != nil { // path not found or not executable
+ //return fmt.Sprintf("cannot execute: %s", cmd[0])
+ return ""
+ }
+ out, err := exec.Command(path, cmd[1:]...).Output()
+ if err != nil {
+ return fmt.Sprintf("error: %s", err)
+ break
+ }
+ outstr := string(out)
+ return outstr
+ }
+
+ // no match
+ return "?"
+}