diff --git a/internal/editor/document/document.go b/internal/editor/document/document.go index 7bad7c0..ba553fb 100644 --- a/internal/editor/document/document.go +++ b/internal/editor/document/document.go @@ -75,7 +75,7 @@ func (d *Document) Save() int { return 0 } -func (d *Document) Row(at int) *Row { +func (d *Document) GetRow(at int) *Row { return d.rows[at] } diff --git a/internal/editor/draw.go b/internal/editor/draw.go index 02c7c57..f13327b 100644 --- a/internal/editor/draw.go +++ b/internal/editor/draw.go @@ -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.GetRow(e.cursor.Y).CursorXToRenderX(e.cursor.X) } if e.cursor.Y < e.offset.Y { @@ -63,7 +63,7 @@ func (e *editor) drawRows(ab *gilo.Buffer) { if fileRow >= e.document.RowCount() { e.drawPlaceholderRow(y, ab) } else { - rawRow := e.document.Row(fileRow) + rawRow := e.document.GetRow(fileRow) // If the column offset is greater than the length of the row, // just display an empty row @@ -73,8 +73,8 @@ func (e *editor) drawRows(ab *gilo.Buffer) { continue } - rowLen := e.document.Row(fileRow).RenderSize() - e.offset.X - outputRow := truncate(e.document.Row(fileRow).Render(e.offset), rowLen) + rowLen := e.document.GetRow(fileRow).RenderSize() - e.offset.X + outputRow := truncate(e.document.GetRow(fileRow).Render(e.offset), rowLen) ab.Append(outputRow) } diff --git a/internal/editor/editor.go b/internal/editor/editor.go index a3a59c1..459c2ab 100644 --- a/internal/editor/editor.go +++ b/internal/editor/editor.go @@ -6,6 +6,7 @@ import ( "time" doc "timshome.page/gilo/internal/editor/document" "timshome.page/gilo/internal/gilo" + "timshome.page/gilo/internal/key" "timshome.page/gilo/internal/terminal" ) @@ -58,6 +59,10 @@ func (e *editor) Open(filename string) { } func (e *editor) Save() { + //if e.document.Filename == "" { + // e.document.Filename = e.Prompt("Save as: %s") + //} + size := e.document.Save() if size > 0 { @@ -80,6 +85,31 @@ func (e *editor) ProcessKeypress() bool { return e.processKeypressChar(ch) } +func (e *editor) Prompt(prompt string) string { + buf := gilo.NewBuffer() + + for { + if buf.Len() > 0 { + e.SetStatusMessage(prompt, buf.ToString()) + } else { + e.SetStatusMessage(prompt) + } + + e.RefreshScreen() + + ch, _ := terminal.ReadKey() + if ch == key.Enter { + if buf.Len() != 0 { + e.SetStatusMessage("") + + return buf.ToString() + } else if ! key.IsCtrl(ch) && key.IsAscii(ch) { + buf.AppendRune(ch) + } + } + } +} + func (e *editor) insertChar(ch rune) { if e.cursor.Y == e.document.RowCount() { e.document.AppendRow("") @@ -106,7 +136,7 @@ func (e *editor) delChar() { e.cursor.X -= 1 } else { // Move cursor to the current end of the previous line - e.cursor.X = e.document.Row(e.cursor.Y - 1).Size() + e.cursor.X = e.document.GetRow(e.cursor.Y - 1).Size() // Move the contents of the current row to the previous e.document.MergeRows(e.cursor.Y - 1, e.cursor.Y) diff --git a/internal/editor/editor_test.go b/internal/editor/editor_test.go index 3125a15..333d8f6 100644 --- a/internal/editor/editor_test.go +++ b/internal/editor/editor_test.go @@ -18,7 +18,7 @@ func TestInsertChar(t *testing.T) { t.Errorf("A row was not created when the character was inserted") } - row := e.document.Row(0) + row := e.document.GetRow(0) if row.Size() != 1 { t.Errorf("Failed to add character to row. Row: %v", row) } diff --git a/internal/editor/input.go b/internal/editor/input.go index 5340542..5592161 100644 --- a/internal/editor/input.go +++ b/internal/editor/input.go @@ -30,7 +30,7 @@ func (e *editor) processKeypressChar(ch rune) bool { switch ch { case key.Ctrl('q'): 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.SetStatusMessage("WARNING!!! File has unsaved changes. Press Ctrl-Q %d more times to quit.", e.quitTimes) e.quitTimes -= 1 return true @@ -106,7 +106,7 @@ func (e *editor) moveCursor(key string) { if e.cursor.Y >= e.document.RowCount() { row = nil } else { - row = e.document.Row(e.cursor.Y) + row = e.document.GetRow(e.cursor.Y) } switch key { @@ -118,7 +118,7 @@ func (e *editor) moveCursor(key string) { // Move from beginning of current row to end of previous row if e.cursor.Y > 0 { e.cursor.Y -= 1 - e.cursor.X = e.document.Row(e.cursor.Y).Size() + e.cursor.X = e.document.GetRow(e.cursor.Y).Size() } case keyRight: if row != nil && e.cursor.X < row.Size() { @@ -161,7 +161,7 @@ func (e *editor) moveCursor(key string) { } if e.cursor.Y < e.document.RowCount() { - row = e.document.Row(e.cursor.Y) + row = e.document.GetRow(e.cursor.Y) rowLen := row.Size() // Snap to the end of a shorter line from a longer one diff --git a/internal/gilo/buffer.go b/internal/gilo/buffer.go index 9018231..e1be4cc 100644 --- a/internal/gilo/buffer.go +++ b/internal/gilo/buffer.go @@ -44,3 +44,7 @@ func (b *Buffer) AppendLn(s string) int { func (b *Buffer) ToString() string { return b.buf.String() } + +func (b *Buffer) Len() int { + return b.buf.Len() +} \ No newline at end of file diff --git a/internal/gilo/buffer_test.go b/internal/gilo/buffer_test.go index f35fa57..88f0b8b 100644 --- a/internal/gilo/buffer_test.go +++ b/internal/gilo/buffer_test.go @@ -2,7 +2,7 @@ package gilo import "testing" -func TestBufferAppendRune(t *testing.T) { +func TestBuffer_AppendRune(t *testing.T) { b := NewBuffer() got := b.AppendRune('a') @@ -13,7 +13,7 @@ func TestBufferAppendRune(t *testing.T) { } } -func TestBufferAppend(t *testing.T) { +func TestBuffer_Append(t *testing.T) { b := NewBuffer() got := b.Append("123") @@ -24,7 +24,7 @@ func TestBufferAppend(t *testing.T) { } } -func TestBufferAppendLn(t *testing.T) { +func TestBuffer_AppendLn(t *testing.T) { b := NewBuffer() got := b.AppendLn("123") @@ -37,7 +37,7 @@ func TestBufferAppendLn(t *testing.T) { } } -func TestBufferToString(t *testing.T) { +func TestBuffer_ToString(t *testing.T) { b := NewBuffer() b.Append("foo") @@ -48,3 +48,15 @@ func TestBufferToString(t *testing.T) { t.Errorf("Failed to convert to proper string") } } + +func TestBuffer_Len(t *testing.T) { + b := NewBuffer() + b.Append("foo") + + got := b.Len() + want := 3 + + if got != want { + t.Errorf("Wrong length for string, got %d, expected %d", got, want) + } +} diff --git a/internal/gilo/point_test.go b/internal/gilo/point_test.go new file mode 100644 index 0000000..5c07cc8 --- /dev/null +++ b/internal/gilo/point_test.go @@ -0,0 +1,19 @@ +package gilo + +import "testing" + +func TestDefaultPoint(t *testing.T) { + point := DefaultPoint() + + if point.X != 0 || point.Y != 0 { + t.Errorf("Failure to create proper Point") + } +} + +func TestNewPoint(t *testing.T) { + point := NewPoint(3, 5) + + if point.X != 3 || point.Y != 5 { + t.Errorf("Failure to create proper Point") + } +} diff --git a/internal/key/key.go b/internal/key/key.go index 566107d..2fe4769 100644 --- a/internal/key/key.go +++ b/internal/key/key.go @@ -11,13 +11,13 @@ const ( ) // Is this an ASCII character? -func isAscii(char rune) bool { +func IsAscii(char rune) bool { return char < 0x80 } // Is this an ASCII ctrl character? func IsCtrl(char rune) bool { - if !isAscii(char) { + if !IsAscii(char) { return false } @@ -26,7 +26,7 @@ func IsCtrl(char rune) bool { // Return the input code of a Ctrl-key chord. func Ctrl(char rune) rune { - if !isAscii(char) { + if !IsAscii(char) { return 0 } diff --git a/internal/key/key_test.go b/internal/key/key_test.go index 198b611..6ed22a4 100644 --- a/internal/key/key_test.go +++ b/internal/key/key_test.go @@ -17,7 +17,7 @@ var isAsciiTest = []isRune{ func TestIsAscii(t *testing.T) { for _, test := range isAsciiTest { - if output := isAscii(test.arg1); output != test.expected { + if output := IsAscii(test.arg1); output != test.expected { t.Errorf("Output %v not equal to expected %v for input %q", output, test.expected, test.arg1) } }