From 1cc35c95a79437951c449f6aed0c7bd470f9e526 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Fri, 2 Apr 2021 13:11:54 -0400 Subject: [PATCH] Move more stuff around --- editor/constants.go | 18 ++++++- editor/document.go | 9 ++-- editor/draw.go | 61 +++++++++++----------- editor/editor.go | 1 + editor/input_test.go | 8 +-- editor/keymap.go | 17 ------ {editor => internal/buffer}/buffer.go | 16 +++--- {editor => internal/buffer}/buffer_test.go | 20 +++---- 8 files changed, 76 insertions(+), 74 deletions(-) delete mode 100644 editor/keymap.go rename {editor => internal/buffer}/buffer.go (67%) rename {editor => internal/buffer}/buffer_test.go (77%) diff --git a/editor/constants.go b/editor/constants.go index 73edecf..102a09a 100644 --- a/editor/constants.go +++ b/editor/constants.go @@ -1,7 +1,7 @@ package editor // ---------------------------------------------------------------------------- -// !Constants +// !General Constants // ---------------------------------------------------------------------------- const ( @@ -9,3 +9,19 @@ const ( KiloTabStop = 4 KiloQuitTimes = 3 ) + +// ---------------------------------------------------------------------------- +// !Constants representing input keys +// ---------------------------------------------------------------------------- + +const ( + keyUp = "ARROW_UP" + keyDown = "ARROW_DOWN" + keyLeft = "ARROW_LEFT" + keyRight = "ARROW_RIGHT" + keyPageUp = "PAGE_UP" + keyPageDown = "PAGE_DOWN" + keyHome = "HOME" + keyEnd = "END" + keyDelete = "DELETE" +) diff --git a/editor/document.go b/editor/document.go index 8c08caf..d56f473 100644 --- a/editor/document.go +++ b/editor/document.go @@ -4,6 +4,7 @@ import ( "bufio" "log" "os" + "timshome.page/gilo/internal/buffer" ) type document struct { @@ -83,14 +84,14 @@ func (d *document) appendRow(s string) { } func (d *document) toString() string { - buf := newBuffer() + buf := buffer.NewBuffer() for _, row := range d.rows { - buf.append(row.toString()) - buf.appendRune('\n') + buf.Append(row.toString()) + buf.AppendRune('\n') } - return buf.toString() + return buf.ToString() } func (d *document) rowCount() int { diff --git a/editor/draw.go b/editor/draw.go index 32f6f75..a86d072 100644 --- a/editor/draw.go +++ b/editor/draw.go @@ -5,6 +5,7 @@ import ( "fmt" "strings" "time" + "timshome.page/gilo/internal/buffer" "timshome.page/gilo/terminal" ) @@ -15,20 +16,20 @@ import ( func (e *editor) RefreshScreen() { e.scroll() - ab := newBuffer() + ab := buffer.NewBuffer() - ab.append(terminal.HideCursor) - ab.append(terminal.ResetCursor) + ab.Append(terminal.HideCursor) + ab.Append(terminal.ResetCursor) e.drawRows(ab) e.drawStatusBar(ab) e.drawMessageBar(ab) - ab.append(terminal.MoveCursor(e.renderX-e.offset.x, e.cursor.y-e.offset.y)) + ab.Append(terminal.MoveCursor(e.renderX-e.offset.x, e.cursor.y-e.offset.y)) - ab.append(terminal.ShowCursor) + ab.Append(terminal.ShowCursor) - terminal.Write(ab.toString()) + terminal.Write(ab.ToString()) } func (e *editor) scroll() { @@ -55,7 +56,7 @@ func (e *editor) scroll() { } } -func (e *editor) drawRows(ab *buffer) { +func (e *editor) drawRows(ab *buffer.Buffer) { for y := 0; y < e.screen.Rows; y++ { fileRow := y + e.offset.y @@ -67,21 +68,21 @@ func (e *editor) drawRows(ab *buffer) { // If the column offset is greater than the length of the row, // just display an empty row if e.offset.x > rawRow.size() { - ab.append("") + ab.Append("") continue } rowLen := e.document.rows[fileRow].rSize() - e.offset.x outputRow := truncate(string(e.document.rows[fileRow].render[e.offset.x:]), rowLen) - ab.append(outputRow) + ab.Append(outputRow) } - ab.appendLn(terminal.ClearLine) + ab.AppendLn(terminal.ClearLine) } } -func (e *editor) drawPlaceholderRow(y int, ab *buffer) { +func (e *editor) drawPlaceholderRow(y int, ab *buffer.Buffer) { if e.document.rowCount() == 0 && y == e.screen.Rows/3 { welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion) if len(welcome) > e.screen.Cols { @@ -90,25 +91,25 @@ func (e *editor) drawPlaceholderRow(y int, ab *buffer) { padding := (e.screen.Cols - len(welcome)) / 2 if padding > 0 { - ab.appendRune('~') + ab.AppendRune('~') padding-- } for padding > 0 { padding-- - ab.appendRune(' ') + ab.AppendRune(' ') } - ab.append(welcome) + ab.Append(welcome) } else { - ab.appendRune('~') + ab.AppendRune('~') } } -func (e *editor) drawStatusBar(ab *buffer) { +func (e *editor) drawStatusBar(ab *buffer.Buffer) { cols := e.screen.Cols - ab.append(terminal.InvertColor) + ab.Append(terminal.InvertColor) fileName := "[No Name]" if e.document.filename != "" { @@ -124,8 +125,8 @@ func (e *editor) drawStatusBar(ab *buffer) { if length > cols { leftStatus = truncate(leftStatus, cols) - ab.append(leftStatus) - ab.append(terminal.ResetColor) + ab.Append(leftStatus) + ab.Append(terminal.ResetColor) return } @@ -138,30 +139,30 @@ func (e *editor) drawStatusBar(ab *buffer) { paddingLength := cols - statusLength padding := strings.Repeat(" ", paddingLength) - ab.append(leftStatus) - ab.append(padding) - ab.append(rightStatus) + ab.Append(leftStatus) + ab.Append(padding) + ab.Append(rightStatus) - ab.append(terminal.ResetColor) + ab.Append(terminal.ResetColor) return } - ab.append(leftStatus) + ab.Append(leftStatus) // Pad the rest of the status line padding := strings.Repeat(" ", cols-length) - ab.append(padding) + ab.Append(padding) - ab.append(terminal.ResetColor) + ab.Append(terminal.ResetColor) } -func (e *editor) drawMessageBar(ab *buffer) { - ab.append("\r\n") - ab.append(terminal.ClearLine) +func (e *editor) drawMessageBar(ab *buffer.Buffer) { + ab.Append("\r\n") + ab.Append(terminal.ClearLine) msg := truncate(e.status.message, e.screen.Cols) if len(msg) > 0 && time.Since(e.status.created).Seconds() < 5.0 { - ab.append(msg) + ab.Append(msg) } } diff --git a/editor/editor.go b/editor/editor.go index 89754e0..637214d 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -1,3 +1,4 @@ +// The main interface/implementation of the editor object package editor import ( diff --git a/editor/input_test.go b/editor/input_test.go index b21c95b..a00c433 100644 --- a/editor/input_test.go +++ b/editor/input_test.go @@ -12,9 +12,9 @@ type moveCursor struct { } var cursorTests = []moveCursor{ - {[]string{"\x1b"}, false, &point{0, 0}}, + {[]string{string(rune(key.Esc))}, false, &point{0, 0}}, {[]string{keyRight}, true, &point{1, 0}}, - {[]string{keyEnd}, true, &point{14, 0}}, + {[]string{keyDown, keyEnd}, true, &point{14, 1}}, {[]string{keyEnd, keyHome}, true, &point{0, 0}}, {[]string{keyRight, keyLeft}, true, &point{0, 0}}, {[]string{keyDown, keyUp}, true, &point{0, 0}}, @@ -29,8 +29,8 @@ func TestMoveCursor(t *testing.T) { e.Open("editor.go") } - for _, key := range test.keys { - e.moveCursor(key) + for _, k := range test.keys { + e.moveCursor(k) } want := test.cursor got := e.cursor diff --git a/editor/keymap.go b/editor/keymap.go deleted file mode 100644 index 3922128..0000000 --- a/editor/keymap.go +++ /dev/null @@ -1,17 +0,0 @@ -package editor - -// ---------------------------------------------------------------------------- -// !Constants representing input keys -// ---------------------------------------------------------------------------- - -const ( - keyUp = "ARROW_UP" - keyDown = "ARROW_DOWN" - keyLeft = "ARROW_LEFT" - keyRight = "ARROW_RIGHT" - keyPageUp = "PAGE_UP" - keyPageDown = "PAGE_DOWN" - keyHome = "HOME" - keyEnd = "END" - keyDelete = "DELETE" -) diff --git a/editor/buffer.go b/internal/buffer/buffer.go similarity index 67% rename from editor/buffer.go rename to internal/buffer/buffer.go index ea8d868..d884682 100644 --- a/editor/buffer.go +++ b/internal/buffer/buffer.go @@ -1,4 +1,4 @@ -package editor +package buffer import ( "fmt" @@ -9,38 +9,38 @@ import ( // !Output Buffer // ---------------------------------------------------------------------------- -type buffer struct { +type Buffer struct { buf *strings.Builder } -func newBuffer() *buffer { +func NewBuffer() *Buffer { var buf strings.Builder - b := new(buffer) + b := new(Buffer) b.buf = &buf return b } -func (b *buffer) appendRune(r rune) int { +func (b *Buffer) AppendRune(r rune) int { size, _ := b.buf.WriteRune(r) return size } -func (b *buffer) append(s string) int { +func (b *Buffer) Append(s string) int { size, _ := b.buf.WriteString(s) return size } -func (b *buffer) appendLn(s string) int { +func (b *Buffer) AppendLn(s string) int { str := fmt.Sprintf("%s\r\n", s) size, _ := b.buf.WriteString(str) return size } -func (b *buffer) toString() string { +func (b *Buffer) ToString() string { return b.buf.String() } diff --git a/editor/buffer_test.go b/internal/buffer/buffer_test.go similarity index 77% rename from editor/buffer_test.go rename to internal/buffer/buffer_test.go index 9991756..6b7606c 100644 --- a/editor/buffer_test.go +++ b/internal/buffer/buffer_test.go @@ -1,11 +1,11 @@ -package editor +package buffer import "testing" func TestBufferAppendRune(t *testing.T) { - b := newBuffer() + b := NewBuffer() - got := b.appendRune('a') + got := b.AppendRune('a') want := 1 if got != want { @@ -14,9 +14,9 @@ func TestBufferAppendRune(t *testing.T) { } func TestBufferAppend(t *testing.T) { - b := newBuffer() + b := NewBuffer() - got := b.append("123") + got := b.Append("123") want := 3 if got != want { @@ -25,9 +25,9 @@ func TestBufferAppend(t *testing.T) { } func TestBufferAppendLn(t *testing.T) { - b := newBuffer() + b := NewBuffer() - got := b.appendLn("123") + got := b.AppendLn("123") // Adds carriage return and line feed want := 5 @@ -38,10 +38,10 @@ func TestBufferAppendLn(t *testing.T) { } func TestBufferToString(t *testing.T) { - b := newBuffer() - b.append("foo") + b := NewBuffer() + b.Append("foo") - got := b.toString() + got := b.ToString() want := "foo" if got != want {