Complete kilo chapter 5, fix backspace and delete behavior
All checks were successful
timw4mail/gilo/pipeline/head This commit looks good
All checks were successful
timw4mail/gilo/pipeline/head This commit looks good
This commit is contained in:
parent
4668ba5628
commit
3672c5c3e9
@ -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)
|
||||
}
|
||||
|
@ -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()
|
||||
|
@ -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()
|
||||
}
|
||||
|
@ -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 ""
|
||||
}
|
@ -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 {
|
Loading…
Reference in New Issue
Block a user