Refactoring of edit functions

This commit is contained in:
Roi Martin (@nibble_ds) 2014-01-23 21:26:53 +01:00
parent f9055c514a
commit 60c77d0e40

35
edit.go
View File

@ -6,16 +6,11 @@ package gocui
// editWrite writes a rune at the cursor position. // editWrite writes a rune at the cursor position.
func (v *View) editWrite(ch rune) error { func (v *View) editWrite(ch rune) error {
maxX, _ := v.Size()
v.writeRune(v.cx, v.cy, ch) v.writeRune(v.cx, v.cy, ch)
if v.cx == maxX-1 { if err := v.SetCursor(v.cx+1, v.cy); err != nil {
if err := v.SetOrigin(v.ox+1, v.oy); err != nil { if err := v.SetOrigin(v.ox+1, v.oy); err != nil {
return err return err
} }
} else {
if err := v.SetCursor(v.cx+1, v.cy); err != nil {
return err
}
} }
return nil return nil
} }
@ -25,14 +20,8 @@ func (v *View) editWrite(ch rune) error {
func (v *View) editDelete(back bool) error { func (v *View) editDelete(back bool) error {
if back { if back {
v.deleteRune(v.cx-1, v.cy) v.deleteRune(v.cx-1, v.cy)
if v.cx == 0 { if err := v.SetCursor(v.cx-1, v.cy); err != nil && v.ox > 0 {
if v.ox > 0 { if err := v.SetOrigin(v.ox-1, v.oy); err != nil {
if err := v.SetOrigin(v.ox-1, v.oy); err != nil {
return err
}
}
} else {
if err := v.SetCursor(v.cx-1, v.cy); err != nil {
return err return err
} }
} }
@ -44,19 +33,17 @@ func (v *View) editDelete(back bool) error {
// editLine inserts a new line under the cursor. // editLine inserts a new line under the cursor.
func (v *View) editLine() error { func (v *View) editLine() error {
_, maxY := v.Size()
v.addLine(v.cy + 1) v.addLine(v.cy + 1)
if v.cy == maxY-1 { if err := v.SetCursor(v.cx, v.cy+1); err != nil {
if err := v.SetOrigin(0, v.oy+1); err != nil { if err := v.SetOrigin(v.ox, v.oy+1); err != nil {
return err
}
if err := v.SetCursor(0, v.cy); err != nil {
return err
}
} else {
if err := v.SetCursor(0, v.cy+1); err != nil {
return err return err
} }
} }
if err := v.SetCursor(0, v.cy); err != nil {
return err
}
if err := v.SetOrigin(0, v.oy); err != nil {
return err
}
return nil return nil
} }