diff --git a/internal/editor/draw.go b/internal/editor/draw.go index f13327b..cbf7cd1 100644 --- a/internal/editor/draw.go +++ b/internal/editor/draw.go @@ -74,7 +74,7 @@ func (e *editor) drawRows(ab *gilo.Buffer) { } rowLen := e.document.GetRow(fileRow).RenderSize() - e.offset.X - outputRow := truncate(e.document.GetRow(fileRow).Render(e.offset), rowLen) + outputRow := gilo.Truncate(e.document.GetRow(fileRow).Render(e.offset), rowLen) ab.Append(outputRow) } @@ -86,7 +86,7 @@ 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", gilo.Version) if len(welcome) > e.screen.Cols { - welcome = truncate(welcome, e.screen.Cols) + welcome = gilo.Truncate(welcome, e.screen.Cols) } padding := (e.screen.Cols - len(welcome)) / 2 @@ -123,7 +123,7 @@ func (e *editor) drawStatusBar(ab *gilo.Buffer) { leftStatus := fmt.Sprintf("%.20s - %d lines %s", fileName, e.document.RowCount(), modified) length := len(leftStatus) if length > cols { - leftStatus = truncate(leftStatus, cols) + leftStatus = gilo.Truncate(leftStatus, cols) ab.Append(leftStatus) ab.Append(terminal.ResetColor) @@ -161,7 +161,7 @@ func (e *editor) drawMessageBar(ab *gilo.Buffer) { ab.Append("\r\n") ab.Append(terminal.ClearLine) - msg := truncate(e.status.message, e.screen.Cols) + msg := gilo.Truncate(e.status.message, e.screen.Cols) if len(msg) > 0 && time.Since(e.status.created).Seconds() < 5.0 { ab.Append(msg) } diff --git a/internal/editor/editor.go b/internal/editor/editor.go index b66da9b..9dc4930 100644 --- a/internal/editor/editor.go +++ b/internal/editor/editor.go @@ -60,7 +60,10 @@ func (e *editor) Open(filename string) { func (e *editor) Save() { if e.document.Filename == "" { - e.document.Filename = e.Prompt("Save as: %s") + e.document.Filename = e.Prompt("Save as: %s (ESC to cancel)") + if e.document.Filename == "" { + e.SetStatusMessage("Save aborted") + } } size := e.document.Save() @@ -107,6 +110,16 @@ func (e *editor) Prompt(prompt string) string { } } else if key.IsAscii(ch) && !key.IsCtrl(ch) { buf.AppendRune(ch) + } else if ch == key.Backspace || ch == key.Ctrl('h') { + buf.Truncate(buf.Len() - 1) + } else if ch == key.Esc { + k := parseEscapeSequence() + if k == keyDelete { + buf.Truncate(buf.Len() - 1) + } else if k == string(rune(key.Esc)) { + e.SetStatusMessage("") + return "" + } } } } @@ -133,8 +146,6 @@ func (e *editor) delChar() { at := e.cursor at.X -= 1 e.document.DelChar(at) - - e.cursor.X -= 1 } else { // Move cursor to the current end of the previous line e.cursor.X = e.document.GetRow(e.cursor.Y - 1).Size() diff --git a/internal/gilo/buffer.go b/internal/gilo/buffer.go index a649422..b5b5a85 100644 --- a/internal/gilo/buffer.go +++ b/internal/gilo/buffer.go @@ -45,6 +45,16 @@ func (b *Buffer) ToString() string { return b.buf.String() } +func (b *Buffer) Truncate(length int) string { + current := b.buf.String() + truncated := Truncate(current, length) + + b.buf.Reset() + b.Append(truncated) + + return truncated +} + func (b *Buffer) Len() int { return b.buf.Len() } diff --git a/internal/editor/fn.go b/internal/gilo/fn.go similarity index 83% rename from internal/editor/fn.go rename to internal/gilo/fn.go index 488feda..20334c2 100644 --- a/internal/editor/fn.go +++ b/internal/gilo/fn.go @@ -1,10 +1,10 @@ // Helper functions -package editor +package gilo import "strings" // Truncate a string to a length -func truncate(s string, length int) string { +func Truncate(s string, length int) string { if length < 1 { return "" } diff --git a/internal/editor/fn_test.go b/internal/gilo/fn_test.go similarity index 83% rename from internal/editor/fn_test.go rename to internal/gilo/fn_test.go index fb3bf18..babccb7 100644 --- a/internal/editor/fn_test.go +++ b/internal/gilo/fn_test.go @@ -1,4 +1,4 @@ -package editor +package gilo import ( "testing" @@ -6,7 +6,7 @@ import ( func TestTruncateString(t *testing.T) { firstString := "abcdefghijklmnopqrstuvwxyz" - truncated := truncate(firstString, 13) + truncated := Truncate(firstString, 13) got := len(truncated) want := 13 @@ -17,7 +17,7 @@ func TestTruncateString(t *testing.T) { } func TestTruncateStringNegative(t *testing.T) { - got := truncate("fdlkjf", -5) + got := Truncate("fdlkjf", -5) want := "" if got != want { @@ -28,7 +28,7 @@ func TestTruncateStringNegative(t *testing.T) { func TestTruncateShorterString(t *testing.T) { str := "abcdefg" - got := truncate(str, 13) + got := Truncate(str, 13) want := str if got != want {