From 92f9474f883b33e119528a016d5a70937079580a Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Thu, 25 Apr 2019 01:39:19 +0000 Subject: [PATCH] slow hacks --- ked/buffer.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ ked/cursor.go | 39 +++++++++++++++++++++++++++++++++++++ ked/go.mod | 3 +++ ked/ked.go | 6 ++++++ 4 files changed, 102 insertions(+) create mode 100644 ked/buffer.go create mode 100644 ked/cursor.go create mode 100644 ked/go.mod create mode 100644 ked/ked.go diff --git a/ked/buffer.go b/ked/buffer.go new file mode 100644 index 0000000..2d73157 --- /dev/null +++ b/ked/buffer.go @@ -0,0 +1,54 @@ +package main + +import ( + "os" + "bufio" + "fmt" +) + +type Buffer struct { + contents []string + filename string + cursor Cursor + dirty bool +} + +func NewBuffer(filename string) (*Buffer, error) { + buffer := &Buffer{ + contents: []string{""}, + filename: filename, + cursor: Cursor{row: 0, col: 0}, + } + + err := buffer.Load() + return buffer, err +} + +func (buffer *Buffer) Load() error { + if buffer.filename == "" { + return nil + } + + file, err := os.Open(buffer.filename) + if err != nil { + if os.IsNotExist(err) { + return nil + } + return err + } + defer file.Close() + + buffer.contents = []string{} + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + buffer.contents = append(buffer.contents, line) + fmt.Printf("'%s'\n", line) + } + + return nil +} + +func (buffer *Buffer) Close() error { + return nil +} diff --git a/ked/cursor.go b/ked/cursor.go new file mode 100644 index 0000000..f362485 --- /dev/null +++ b/ked/cursor.go @@ -0,0 +1,39 @@ +package main + +func clamp(int v, int m) int { + if v < 0 { + return 0 + } + if v > m { + return m + } + return v +} + +type Cursor struct { + row int + col int +} + +func (c Cursor) clamp(dr, dc, maxRow, maxCol int) Cursor { + return Cursor{ + row: clamp(c.row+dr, maxRow), + col: clamp(c.col+dc, maxCol), + } +} + +func (c Cursor) left(maxRow, maxCol int) Cursor { + return c.clamp(0, -1, maxRow, maxCol) +} + +func (c Cursor) right(maxRow, maxCol) Cursor { + return c.clamp(0, 1, maxRow, maxCol) +} + +func (c Cursor) up(maxRow, maxCol) Cursor { + return c.clamp(-1, 0, maxRow, maxCol) +} + +func (c Cursor) down(maxRow, maxCol) Cursor { + return c.clamp(1, 0, maxRow, maxCol) +} diff --git a/ked/go.mod b/ked/go.mod new file mode 100644 index 0000000..f31d1d6 --- /dev/null +++ b/ked/go.mod @@ -0,0 +1,3 @@ +module gitlab.com/kisom/ked + +go 1.12 diff --git a/ked/ked.go b/ked/ked.go new file mode 100644 index 0000000..a259add --- /dev/null +++ b/ked/ked.go @@ -0,0 +1,6 @@ +package main + +func main() { + buffer := NewBuffer("ked.go") + buffer.Close() +}