From 787166df3afd01931069963a1f78947495e0679d Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Fri, 2 Apr 2021 15:36:43 -0400 Subject: [PATCH] Move stuff again, just because --- editor/constants.go | 27 ------------------- gilo.go | 4 +-- .../editor/document}/document.go | 17 +++++++----- .../editor/document}/document_test.go | 2 +- {editor => internal/editor/document}/row.go | 15 +++++++---- .../editor/document}/row_test.go | 2 +- {editor => internal/editor}/draw.go | 24 ++++++++--------- {editor => internal/editor}/editor.go | 19 ++++++------- {editor => internal/editor}/editor_test.go | 4 +-- {editor => internal/editor}/fn.go | 0 {editor => internal/editor}/fn_test.go | 0 {editor => internal/editor}/input.go | 26 +++++++++++++++--- {editor => internal/editor}/input_test.go | 2 +- internal/{buffer => gilo}/buffer.go | 2 +- internal/{buffer => gilo}/buffer_test.go | 2 +- internal/gilo/constants.go | 11 ++++++++ internal/gilo/{shared.go => point.go} | 0 17 files changed, 84 insertions(+), 73 deletions(-) delete mode 100644 editor/constants.go rename {editor => internal/editor/document}/document.go (88%) rename {editor => internal/editor/document}/document_test.go (96%) rename {editor => internal/editor/document}/row.go (83%) rename {editor => internal/editor/document}/row_test.go (98%) rename {editor => internal/editor}/draw.go (83%) rename {editor => internal/editor}/editor.go (86%) rename {editor => internal/editor}/editor_test.go (91%) rename {editor => internal/editor}/fn.go (100%) rename {editor => internal/editor}/fn_test.go (100%) rename {editor => internal/editor}/input.go (86%) rename {editor => internal/editor}/input_test.go (98%) rename internal/{buffer => gilo}/buffer.go (97%) rename internal/{buffer => gilo}/buffer_test.go (98%) create mode 100644 internal/gilo/constants.go rename internal/gilo/{shared.go => point.go} (100%) diff --git a/editor/constants.go b/editor/constants.go deleted file mode 100644 index 102a09a..0000000 --- a/editor/constants.go +++ /dev/null @@ -1,27 +0,0 @@ -package editor - -// ---------------------------------------------------------------------------- -// !General Constants -// ---------------------------------------------------------------------------- - -const ( - KiloVersion = "0.0.1" - 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/gilo.go b/gilo.go index d803c1c..1fa661c 100644 --- a/gilo.go +++ b/gilo.go @@ -4,7 +4,7 @@ import ( "fmt" "golang.org/x/term" "os" - "timshome.page/gilo/editor" + "timshome.page/gilo/internal/editor" "timshome.page/gilo/internal/terminal" ) @@ -22,7 +22,7 @@ func main() { oldState := terminal.RawOn() defer cleanup(oldState) - e := editor.New() + e := editor.NewEditor() // If there is an argument passed, open it as a file to edit if len(os.Args) >= 2 && os.Args[1] != "" { diff --git a/editor/document.go b/internal/editor/document/document.go similarity index 88% rename from editor/document.go rename to internal/editor/document/document.go index 134911c..f0213b7 100644 --- a/editor/document.go +++ b/internal/editor/document/document.go @@ -1,16 +1,15 @@ -package editor +package document import ( "bufio" "log" "os" - "timshome.page/gilo/internal/buffer" "timshome.page/gilo/internal/gilo" ) type Document struct { dirty bool - filename string + Filename string rows []*Row } @@ -31,7 +30,7 @@ func (d *Document) Open(filename string) { } defer file.Close() - d.filename = filename + d.Filename = filename scanner := bufio.NewScanner(file) scanner.Split(bufio.ScanLines) @@ -43,11 +42,11 @@ func (d *Document) Open(filename string) { } func (d *Document) Save() int { - if d.filename == "" { + if d.Filename == "" { return 0 } - file, err := os.OpenFile(d.filename, os.O_RDWR|os.O_CREATE, 0644) + file, err := os.OpenFile(d.Filename, os.O_RDWR|os.O_CREATE, 0644) if err != nil { panic("failed to open/create file for saving") } @@ -89,7 +88,7 @@ func (d *Document) AppendRow(s string) { } func (d *Document) ToString() string { - buf := buffer.NewBuffer() + buf := gilo.NewBuffer() for _, row := range d.rows { buf.Append(row.toString()) @@ -105,10 +104,14 @@ func (d *Document) RowCount() int { func (d *Document) InsertChar(at *gilo.Point, ch rune) { d.rows[at.Y].insertRune(ch, at.X) + + d.dirty = true } func (d *Document) DelChar(at *gilo.Point) { d.rows[at.Y].deleteRune(at.X) + + d.dirty = true } func (d *Document) IsDirty() bool { diff --git a/editor/document_test.go b/internal/editor/document/document_test.go similarity index 96% rename from editor/document_test.go rename to internal/editor/document/document_test.go index a717329..fbc23a5 100644 --- a/editor/document_test.go +++ b/internal/editor/document/document_test.go @@ -1,4 +1,4 @@ -package editor +package document import "testing" diff --git a/editor/row.go b/internal/editor/document/row.go similarity index 83% rename from editor/row.go rename to internal/editor/document/row.go index 4112fed..33d5b95 100644 --- a/editor/row.go +++ b/internal/editor/document/row.go @@ -1,7 +1,8 @@ -package editor +package document import ( "strings" + "timshome.page/gilo/internal/gilo" ) type Row struct { @@ -29,6 +30,10 @@ func (r *Row) RenderSize() int { return len(r.render) } +func (r *Row) Render(at *gilo.Point) string { + return string(r.render[at.X:]) +} + func (r *Row) insertRune(ch rune, at int) { // If insertion index is invalid, just // append the rune to the end of the array @@ -75,7 +80,7 @@ func (r *Row) deleteRune(at int) { func (r *Row) update() { r.render = r.render[:0] - replacement := strings.Repeat(" ", KiloTabStop) + replacement := strings.Repeat(" ", gilo.TabSize) str := strings.ReplaceAll(string(r.chars), "\t", replacement) for _, ch := range str { @@ -87,13 +92,13 @@ func (r *Row) toString() string { return string(r.chars) } -func (r *Row) cursorXToRenderX(cursorX int) int { - renderX := 0 +func (r *Row) CursorXToRenderX(cursorX int) (renderX int) { + renderX = 0 i := 0 for ; i < cursorX; i++ { if r.chars[i] == '\t' { - renderX += (KiloTabStop - 1) - (renderX % KiloTabStop) + renderX += (gilo.TabSize - 1) - (renderX % gilo.TabSize) } renderX += 1 diff --git a/editor/row_test.go b/internal/editor/document/row_test.go similarity index 98% rename from editor/row_test.go rename to internal/editor/document/row_test.go index e8e0fbd..ccdfb6d 100644 --- a/editor/row_test.go +++ b/internal/editor/document/row_test.go @@ -1,4 +1,4 @@ -package editor +package document import "testing" diff --git a/editor/draw.go b/internal/editor/draw.go similarity index 83% rename from editor/draw.go rename to internal/editor/draw.go index 3a8c198..02c7c57 100644 --- a/editor/draw.go +++ b/internal/editor/draw.go @@ -5,7 +5,7 @@ import ( "fmt" "strings" "time" - "timshome.page/gilo/internal/buffer" + "timshome.page/gilo/internal/gilo" "timshome.page/gilo/internal/terminal" ) @@ -16,7 +16,7 @@ import ( func (e *editor) RefreshScreen() { e.scroll() - ab := buffer.NewBuffer() + ab := gilo.NewBuffer() ab.Append(terminal.HideCursor) ab.Append(terminal.ResetCursor) @@ -36,7 +36,7 @@ func (e *editor) scroll() { e.renderX = 0 if e.cursor.Y < e.document.RowCount() { - e.renderX = e.document.Row(e.cursor.Y).cursorXToRenderX(e.cursor.X) + e.renderX = e.document.Row(e.cursor.Y).CursorXToRenderX(e.cursor.X) } if e.cursor.Y < e.offset.Y { @@ -56,7 +56,7 @@ func (e *editor) scroll() { } } -func (e *editor) drawRows(ab *buffer.Buffer) { +func (e *editor) drawRows(ab *gilo.Buffer) { for y := 0; y < e.screen.Rows; y++ { fileRow := y + e.offset.Y @@ -74,7 +74,7 @@ func (e *editor) drawRows(ab *buffer.Buffer) { } rowLen := e.document.Row(fileRow).RenderSize() - e.offset.X - outputRow := truncate(string(e.document.Row(fileRow).render[e.offset.X:]), rowLen) + outputRow := truncate(e.document.Row(fileRow).Render(e.offset), rowLen) ab.Append(outputRow) } @@ -82,9 +82,9 @@ func (e *editor) drawRows(ab *buffer.Buffer) { } } -func (e *editor) drawPlaceholderRow(y int, ab *buffer.Buffer) { +func (e *editor) drawPlaceholderRow(y int, ab *gilo.Buffer) { if e.document.RowCount() == 0 && y == e.screen.Rows/3 { - welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion) + welcome := fmt.Sprintf("Gilo editor -- version %s", gilo.Version) if len(welcome) > e.screen.Cols { welcome = truncate(welcome, e.screen.Cols) } @@ -106,17 +106,17 @@ func (e *editor) drawPlaceholderRow(y int, ab *buffer.Buffer) { } } -func (e *editor) drawStatusBar(ab *buffer.Buffer) { +func (e *editor) drawStatusBar(ab *gilo.Buffer) { cols := e.screen.Cols ab.Append(terminal.InvertColor) fileName := "[No Name]" - if e.document.filename != "" { - fileName = e.document.filename + if e.document.Filename != "" { + fileName = e.document.Filename } modified := "" - if e.document.dirty { + if e.document.IsDirty() { modified = "(modified)" } @@ -157,7 +157,7 @@ func (e *editor) drawStatusBar(ab *buffer.Buffer) { ab.Append(terminal.ResetColor) } -func (e *editor) drawMessageBar(ab *buffer.Buffer) { +func (e *editor) drawMessageBar(ab *gilo.Buffer) { ab.Append("\r\n") ab.Append(terminal.ClearLine) diff --git a/editor/editor.go b/internal/editor/editor.go similarity index 86% rename from editor/editor.go rename to internal/editor/editor.go index 1221b92..1cff8cb 100644 --- a/editor/editor.go +++ b/internal/editor/editor.go @@ -4,6 +4,7 @@ package editor import ( "fmt" "time" + doc "timshome.page/gilo/internal/editor/document" "timshome.page/gilo/internal/gilo" "timshome.page/gilo/internal/terminal" ) @@ -21,13 +22,13 @@ type editor struct { screen *terminal.Screen cursor *gilo.Point offset *gilo.Point - document *Document + document *doc.Document status *statusMsg quitTimes uint8 renderX int } -func New() *editor { +func NewEditor() *editor { screen := terminal.Size() // Subtract rows for status bar and message bar/prompt @@ -35,7 +36,7 @@ func New() *editor { cursor := gilo.DefaultPoint() offset := gilo.DefaultPoint() - document := NewDocument() + document := doc.NewDocument() status := &statusMsg{ "", time.Now(), @@ -47,7 +48,7 @@ func New() *editor { offset, document, status, - KiloQuitTimes, + gilo.QuitTimes, 0, } } @@ -84,9 +85,8 @@ func (e *editor) insertChar(ch rune) { e.document.AppendRow("") } - e.document.Row(e.cursor.Y).insertRune(ch, e.cursor.X) + e.document.InsertChar(e.cursor, ch) e.cursor.X += 1 - e.document.dirty = true } func (e *editor) delChar() { @@ -95,9 +95,10 @@ func (e *editor) delChar() { } if e.cursor.X > 0 { - e.document.Row(e.cursor.Y).deleteRune(e.cursor.X - 1) + at := e.cursor + at.X -= 1 + e.document.DelChar(at) + e.cursor.X -= 1 } - - e.document.dirty = true } diff --git a/editor/editor_test.go b/internal/editor/editor_test.go similarity index 91% rename from editor/editor_test.go rename to internal/editor/editor_test.go index d9f2a2e..3125a15 100644 --- a/editor/editor_test.go +++ b/internal/editor/editor_test.go @@ -3,7 +3,7 @@ package editor import "testing" func TestNew(t *testing.T) { - e := New() + e := NewEditor() if e == nil { t.Errorf("Failed to create editor") @@ -11,7 +11,7 @@ func TestNew(t *testing.T) { } func TestInsertChar(t *testing.T) { - e := New() + e := NewEditor() e.insertChar('q') if e.document.RowCount() != 1 { diff --git a/editor/fn.go b/internal/editor/fn.go similarity index 100% rename from editor/fn.go rename to internal/editor/fn.go diff --git a/editor/fn_test.go b/internal/editor/fn_test.go similarity index 100% rename from editor/fn_test.go rename to internal/editor/fn_test.go diff --git a/editor/input.go b/internal/editor/input.go similarity index 86% rename from editor/input.go rename to internal/editor/input.go index 1ab138d..563ba2d 100644 --- a/editor/input.go +++ b/internal/editor/input.go @@ -1,17 +1,35 @@ package editor import ( + "timshome.page/gilo/internal/editor/document" + "timshome.page/gilo/internal/gilo" "timshome.page/gilo/internal/key" "timshome.page/gilo/internal/terminal" ) +// ---------------------------------------------------------------------------- +// !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" +) + /** * Determine what to do with an individual character of input */ func (e *editor) processKeypressChar(ch rune) bool { switch ch { case key.Ctrl('q'): - if e.document.dirty && e.quitTimes > 0 { + if e.document.IsDirty() && e.quitTimes > 0 { e.SetStatusMessage("WARNING!!! File has unsaved changes. Press Ctrl-Q %d more tiems to quite.", e.quitTimes) e.quitTimes -= 1 @@ -50,8 +68,8 @@ func (e *editor) processKeypressChar(ch rune) bool { // Clear the quit message and restart the // confirmation count if confirmation is not // completed - if e.quitTimes != KiloQuitTimes { - e.quitTimes = KiloQuitTimes + if e.quitTimes != gilo.QuitTimes { + e.quitTimes = gilo.QuitTimes e.SetStatusMessage("") } @@ -81,7 +99,7 @@ func (e *editor) processKeypressStr(key string) { } func (e *editor) moveCursor(key string) { - var row *Row + var row *document.Row if e.cursor.Y >= e.document.RowCount() { row = nil } else { diff --git a/editor/input_test.go b/internal/editor/input_test.go similarity index 98% rename from editor/input_test.go rename to internal/editor/input_test.go index 0f170fc..f3a234f 100644 --- a/editor/input_test.go +++ b/internal/editor/input_test.go @@ -24,7 +24,7 @@ var cursorTests = []moveCursor{ func TestMoveCursor(t *testing.T) { for _, test := range cursorTests { - e := New() + e := NewEditor() if test.withFile { e.Open("editor.go") diff --git a/internal/buffer/buffer.go b/internal/gilo/buffer.go similarity index 97% rename from internal/buffer/buffer.go rename to internal/gilo/buffer.go index d884682..9018231 100644 --- a/internal/buffer/buffer.go +++ b/internal/gilo/buffer.go @@ -1,4 +1,4 @@ -package buffer +package gilo import ( "fmt" diff --git a/internal/buffer/buffer_test.go b/internal/gilo/buffer_test.go similarity index 98% rename from internal/buffer/buffer_test.go rename to internal/gilo/buffer_test.go index 6b7606c..f35fa57 100644 --- a/internal/buffer/buffer_test.go +++ b/internal/gilo/buffer_test.go @@ -1,4 +1,4 @@ -package buffer +package gilo import "testing" diff --git a/internal/gilo/constants.go b/internal/gilo/constants.go new file mode 100644 index 0000000..b2e9bcf --- /dev/null +++ b/internal/gilo/constants.go @@ -0,0 +1,11 @@ +package gilo + +// ---------------------------------------------------------------------------- +// !General Constants +// ---------------------------------------------------------------------------- + +const ( + Version = "0.0.1" + TabSize = 4 + QuitTimes = 3 +) diff --git a/internal/gilo/shared.go b/internal/gilo/point.go similarity index 100% rename from internal/gilo/shared.go rename to internal/gilo/point.go