Prepare for Save As functionality
This commit is contained in:
parent
19ab06ac4f
commit
052a4ebbf4
@ -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]
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
19
internal/gilo/point_test.go
Normal file
19
internal/gilo/point_test.go
Normal file
@ -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")
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user