slow hacks

This commit is contained in:
Kyle Isom 2019-04-25 01:39:19 +00:00
parent 695b45d9c0
commit 92f9474f88
4 changed files with 102 additions and 0 deletions

54
ked/buffer.go Normal file
View File

@ -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
}

39
ked/cursor.go Normal file
View File

@ -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)
}

3
ked/go.mod Normal file
View File

@ -0,0 +1,3 @@
module gitlab.com/kisom/ked
go 1.12

6
ked/ked.go Normal file
View File

@ -0,0 +1,6 @@
package main
func main() {
buffer := NewBuffer("ked.go")
buffer.Close()
}