1
0
Fork 0

Complete kilo chapter 5, fix backspace and delete behavior
timw4mail/gilo/pipeline/head This commit looks good Details

This commit is contained in:
Timothy Warren 2021-04-06 10:30:12 -04:00
parent 4668ba5628
commit 3672c5c3e9
5 changed files with 34 additions and 13 deletions

View File

@ -74,7 +74,7 @@ func (e *editor) drawRows(ab *gilo.Buffer) {
} }
rowLen := e.document.GetRow(fileRow).RenderSize() - e.offset.X 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) 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 { if e.document.RowCount() == 0 && y == e.screen.Rows/3 {
welcome := fmt.Sprintf("Gilo editor -- version %s", gilo.Version) welcome := fmt.Sprintf("Gilo editor -- version %s", gilo.Version)
if len(welcome) > e.screen.Cols { 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 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) leftStatus := fmt.Sprintf("%.20s - %d lines %s", fileName, e.document.RowCount(), modified)
length := len(leftStatus) length := len(leftStatus)
if length > cols { if length > cols {
leftStatus = truncate(leftStatus, cols) leftStatus = gilo.Truncate(leftStatus, cols)
ab.Append(leftStatus) ab.Append(leftStatus)
ab.Append(terminal.ResetColor) ab.Append(terminal.ResetColor)
@ -161,7 +161,7 @@ func (e *editor) drawMessageBar(ab *gilo.Buffer) {
ab.Append("\r\n") ab.Append("\r\n")
ab.Append(terminal.ClearLine) 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 { if len(msg) > 0 && time.Since(e.status.created).Seconds() < 5.0 {
ab.Append(msg) ab.Append(msg)
} }

View File

@ -60,7 +60,10 @@ func (e *editor) Open(filename string) {
func (e *editor) Save() { func (e *editor) Save() {
if e.document.Filename == "" { 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() size := e.document.Save()
@ -107,6 +110,16 @@ func (e *editor) Prompt(prompt string) string {
} }
} else if key.IsAscii(ch) && !key.IsCtrl(ch) { } else if key.IsAscii(ch) && !key.IsCtrl(ch) {
buf.AppendRune(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 := e.cursor
at.X -= 1 at.X -= 1
e.document.DelChar(at) e.document.DelChar(at)
e.cursor.X -= 1
} else { } else {
// Move cursor to the current end of the previous line // Move cursor to the current end of the previous line
e.cursor.X = e.document.GetRow(e.cursor.Y - 1).Size() e.cursor.X = e.document.GetRow(e.cursor.Y - 1).Size()

View File

@ -45,6 +45,16 @@ func (b *Buffer) ToString() string {
return b.buf.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 { func (b *Buffer) Len() int {
return b.buf.Len() return b.buf.Len()
} }

View File

@ -1,10 +1,10 @@
// Helper functions // Helper functions
package editor package gilo
import "strings" import "strings"
// Truncate a string to a length // Truncate a string to a length
func truncate(s string, length int) string { func Truncate(s string, length int) string {
if length < 1 { if length < 1 {
return "" return ""
} }

View File

@ -1,4 +1,4 @@
package editor package gilo
import ( import (
"testing" "testing"
@ -6,7 +6,7 @@ import (
func TestTruncateString(t *testing.T) { func TestTruncateString(t *testing.T) {
firstString := "abcdefghijklmnopqrstuvwxyz" firstString := "abcdefghijklmnopqrstuvwxyz"
truncated := truncate(firstString, 13) truncated := Truncate(firstString, 13)
got := len(truncated) got := len(truncated)
want := 13 want := 13
@ -17,7 +17,7 @@ func TestTruncateString(t *testing.T) {
} }
func TestTruncateStringNegative(t *testing.T) { func TestTruncateStringNegative(t *testing.T) {
got := truncate("fdlkjf", -5) got := Truncate("fdlkjf", -5)
want := "" want := ""
if got != want { if got != want {
@ -28,7 +28,7 @@ func TestTruncateStringNegative(t *testing.T) {
func TestTruncateShorterString(t *testing.T) { func TestTruncateShorterString(t *testing.T) {
str := "abcdefg" str := "abcdefg"
got := truncate(str, 13) got := Truncate(str, 13)
want := str want := str
if got != want { if got != want {