mirror of https://github.com/jroimartin/gocui.git
Break lines on enter when needed
This commit is contained in:
parent
0193dee642
commit
f53d985c4e
2
edit.go
2
edit.go
|
@ -54,7 +54,7 @@ func (v *View) editDelete(back bool) error {
|
|||
|
||||
// editLine inserts a new line under the cursor.
|
||||
func (v *View) editLine() error {
|
||||
v.addLine(v.cy)
|
||||
v.breakLine(v.cx, v.cy)
|
||||
if err := v.SetCursor(v.cx, v.cy+1); err != nil {
|
||||
if err := v.SetOrigin(v.ox, v.oy+1); err != nil {
|
||||
return err
|
||||
|
|
27
view.go
27
view.go
|
@ -368,28 +368,39 @@ func (v *View) deleteRune(x, y int) error {
|
|||
if x < 0 || y < 0 || y >= len(v.lines) || v.lines[y] == nil || x >= len(v.lines[y]) {
|
||||
return errors.New("invalid point")
|
||||
}
|
||||
copy(v.lines[y][x:], v.lines[y][x+1:])
|
||||
v.lines[y][len(v.lines[y])-1] = ' '
|
||||
v.lines[y] = append(v.lines[y][:x], v.lines[y][x+1:]...)
|
||||
return nil
|
||||
}
|
||||
|
||||
// addLine adds a line into the view's internal buffer under the position
|
||||
// corresponding to the point (x, y).
|
||||
func (v *View) addLine(y int) error {
|
||||
// breakLine breaks a line of the internal buffer at the position corresponding
|
||||
// to the point (x, y).
|
||||
func (v *View) breakLine(x, y int) error {
|
||||
v.tainted = true
|
||||
|
||||
_, y, err := v.realPosition(0, y)
|
||||
x, y, err := v.realPosition(x, y)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
y = y + 1 // the line must be added under (x, y)
|
||||
|
||||
if y < 0 || y >= len(v.lines) {
|
||||
return errors.New("invalid point")
|
||||
}
|
||||
|
||||
if x < len(v.lines[y]) { // break line
|
||||
left := make([]rune, len(v.lines[y][x:]))
|
||||
copy(left, v.lines[y][x:])
|
||||
right := make([]rune, len(v.lines[y][:x]))
|
||||
copy(right, v.lines[y][:x])
|
||||
|
||||
v.lines[y] = left
|
||||
v.lines = append(v.lines, nil)
|
||||
copy(v.lines[y+1:], v.lines[y:])
|
||||
v.lines[y] = nil
|
||||
v.lines[y] = right
|
||||
} else { // new empty line
|
||||
v.lines = append(v.lines, nil)
|
||||
copy(v.lines[y+2:], v.lines[y+1:])
|
||||
v.lines[y+1] = nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue