1
0
Fork 0

Fix some offset errors for cursor operations
timw4mail/gilo/pipeline/head This commit looks good Details

This commit is contained in:
Timothy Warren 2021-03-31 14:32:43 -04:00
parent ae1c91ac74
commit ae65be003c
5 changed files with 51 additions and 20 deletions

View File

@ -4,4 +4,5 @@ package editor
// !Constants
// ----------------------------------------------------------------------------
const KiloVersion = "0.0.1"
const KiloVersion = "0.0.1"
const KiloTabStop = 4

View File

@ -56,14 +56,15 @@ func (e *editor) drawRows(ab *buffer) {
// If the column offset is greater than the length of the row,
// just display an empty row
if e.offset.x > rawRow.Size() {
if e.offset.x > rawRow.size() {
ab.append("")
continue
}
row := truncateString(string(e.rows[fileRow].chars[e.offset.x:]), e.screen.Cols)
ab.append(row)
rowLen := e.rows[fileRow].rSize() - e.offset.x
outputRow := truncateString(string(e.rows[fileRow].render[e.offset.x:]), rowLen)
ab.append(outputRow)
}
ab.append(terminal.ClearLine)

View File

@ -86,7 +86,7 @@ func (e *editor) ProcessKeypress() bool {
func (e *editor) moveCursor (key string) {
var row *row
if e.cursor.y > len(e.rows) {
if e.cursor.y >= len(e.rows) {
row = nil
} else {
row = e.rows[e.cursor.y]
@ -101,15 +101,15 @@ 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.rows[e.cursor.y].Size()
e.cursor.x = e.rows[e.cursor.y].size()
}
case keyRight:
if row != nil && e.cursor.x < row.Size() {
if row != nil && e.cursor.x < row.size() {
e.cursor.x += 1
}
// Move from end of current line to beginning of next line
if row != nil && e.cursor.x == row.Size() {
if row != nil && e.cursor.x == row.size() && e.cursor.y < len(e.rows) - 1 {
e.cursor.y += 1
e.cursor.x = 0
}
@ -118,18 +118,28 @@ func (e *editor) moveCursor (key string) {
e.cursor.y -= 1
}
case keyDown:
if e.cursor.y < len(e.rows) {
if e.cursor.y < (len(e.rows) - 1) {
e.cursor.y += 1
}
case keyPageUp:
e.cursor.y = 0
if e.cursor.y > e.screen.Rows {
e.cursor.y -= e.screen.Rows
} else {
e.cursor.y = 0
}
case keyPageDown:
e.cursor.y = e.screen.Rows
if e.cursor.y + e.screen.Rows > len(e.rows) {
e.cursor.y += e.screen.Rows
} else {
e.cursor.y = len(e.rows) - 1
}
case keyHome:
e.cursor.x = 0
case keyEnd:
if row != nil {
e.cursor.x = row.Size()
e.cursor.x = row.size()
}
}
@ -137,7 +147,7 @@ func (e *editor) moveCursor (key string) {
row = nil
} else {
row = e.rows[e.cursor.y]
rowLen := row.Size()
rowLen := row.size()
// Snap to the end of a shorter line from a longer one
if e.cursor.x > rowLen {
@ -147,7 +157,8 @@ func (e *editor) moveCursor (key string) {
}
func (e *editor) appendRow(s string) {
newRow := NewRow(s)
newRow := newRow(s)
newRow.update()
e.rows = append(e.rows, newRow)
}

View File

@ -1,21 +1,39 @@
package editor
import "strings"
type row struct {
chars []rune
render []rune
}
func NewRow(s string) *row {
func newRow(s string) *row {
var chars []rune
var render []rune
for _, ch := range s {
chars = append(chars, ch)
render = append(render, ch)
}
return &row {chars}
return &row {chars, render}
}
func (r *row) Size() int {
func (r *row) size() int {
return len(r.chars)
}
func (r *row) rSize() int {
return len(r.render)
}
func (r *row) update() {
r.render = r.render[:0]
replacement := strings.Repeat(" ", KiloTabStop)
str := strings.ReplaceAll(string(r.chars), "\t", replacement)
for _, ch := range str {
r.render = append(r.render, ch)
}
}

View File

@ -4,7 +4,7 @@ import "testing"
func TestNewRow(t *testing.T) {
str := "abcdefg"
row := NewRow(str)
row := newRow(str)
got := string(row.chars)
want := str
@ -16,9 +16,9 @@ func TestNewRow(t *testing.T) {
func TestRowSize(t *testing.T) {
str := "abcdefg"
row := NewRow(str)
row := newRow(str)
got := row.Size()
got := row.size()
want := 7
if got != want {