Skip to content

Commit

Permalink
Fixed issues brought up by @Rudi9719
Browse files Browse the repository at this point in the history
  • Loading branch information
mjarkk committed Oct 31, 2019
1 parent 4f51318 commit 57e883a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
50 changes: 33 additions & 17 deletions edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ func simpleEditor(v *View, key Key, ch rune, mod Modifier) {
case KeyEnter:
v.EditNewLine()
case KeyArrowDown:
v.MoveCursor(0, 1, false)
v.moveCursor(0, 1)
case KeyArrowUp:
v.MoveCursor(0, -1, false)
v.moveCursor(0, -1)
case KeyArrowLeft:
v.MoveCursor(-1, 0, false)
v.moveCursor(-1, 0)
case KeyArrowRight:
v.MoveCursor(1, 0, false)
v.moveCursor(1, 0)
case KeyTab:
v.EditWrite('\t')
case KeyEsc:
Expand All @@ -68,7 +68,7 @@ func simpleEditor(v *View, key Key, ch rune, mod Modifier) {
// EditWrite writes a rune at the cursor position.
func (v *View) EditWrite(ch rune) {
v.writeRune(v.cx, v.cy, ch)
v.MoveCursor(1, 0, true)
v.moveCursor(1, 0)
}

// EditDeleteToStartOfLine is the equivalent of pressing ctrl+U in your terminal, it deletes to the start of the line. Or if you are already at the start of the line, it deletes the newline character
Expand All @@ -89,7 +89,7 @@ func (v *View) EditDeleteToStartOfLine() {
func (v *View) EditGotoToStartOfLine() {
x, _ := v.Cursor()
for x > 0 {
v.MoveCursor(-1, 0, false)
v.moveCursor(-1, 0)
x, _ = v.Cursor()
}
}
Expand All @@ -104,12 +104,12 @@ func (v *View) EditGotoToEndOfLine() {
prevX := -1
for prevX != x {
prevX = x
v.MoveCursor(1, 0, false)
v.moveCursor(1, 0)
x, _ = v.Cursor()
}
} else {
// most left so now we're at the end of the original line
v.MoveCursor(-1, 0, false)
v.moveCursor(-1, 0)
}
}

Expand All @@ -121,7 +121,7 @@ func (v *View) EditDelete(back bool) {
return
}
if y >= len(v.lines) {
v.MoveCursor(-1, 0, true)
v.moveCursor(-1, 0)
return
}

Expand All @@ -138,7 +138,7 @@ func (v *View) EditDelete(back bool) {
}
if back { // middle/end of the line
n, _ := v.deleteRune(v.cx-1, v.cy)
v.MoveCursor(-n, 0, true)
v.moveCursor(-n, 0)
return
}
if x == len(v.lines[y]) { // end of the line
Expand All @@ -154,10 +154,16 @@ func (v *View) EditNewLine() {
v.ox = 0
v.cy = v.cy + 1
v.cx = 0
v.moveCursor(0, 1)
}

// MoveCursor mores the cursor relative from it's current possition
func (v *View) MoveCursor(dx, dy int, writeMode bool) {
func (v *View) MoveCursor(dx, dy int) {
v.moveCursor(dx, dy)
v.gui.userEvents <- userEvent{func(g *Gui) error { return nil }}
}

func (v *View) moveCursor(dx, dy int) {
newX, newY := v.cx+dx, v.cy+dy

if len(v.lines) == 0 {
Expand Down Expand Up @@ -198,14 +204,24 @@ func (v *View) MoveCursor(dx, dy int, writeMode bool) {
}
}

_, maxY := v.Size()
maxX, maxY := v.Size()
newXOnScreen, newYOnScreen, _ := v.linesPosOnScreen(newX, newY)

_, newXOnScreen, _ := v.linesPosOnScreen(newX, newY)
if newXOnScreen > v.oy+maxY-1 {
v.oy = newXOnScreen - maxY + 1
// Set the view offset
if newYOnScreen > v.oy+maxY-1 {
v.oy = newYOnScreen - maxY + 1
}
if newYOnScreen < v.oy {
v.oy = newYOnScreen
}
if newXOnScreen < v.oy {
v.oy = newXOnScreen

if !v.Wrap {
if newXOnScreen > v.ox+maxX-1 {
v.ox = newXOnScreen - maxX + 1
}
if newXOnScreen < v.ox {
v.ox = newXOnScreen
}
}

v.cx, v.cy = newX, newY
Expand Down
2 changes: 1 addition & 1 deletion gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (g *Gui) SetView(name string, x0, y0, x1, y1 int, overlaps byte) (*View, er
return v, nil
}

v := newView(name, x0, y0, x1, y1, g.outputMode)
v := g.newView(name, x0, y0, x1, y1, g.outputMode)
v.BgColor, v.FgColor = g.BgColor, g.FgColor
v.SelBgColor, v.SelFgColor = g.SelBgColor, g.SelFgColor
v.Overlaps = overlaps
Expand Down
8 changes: 6 additions & 2 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ type View struct {

// If HasLoader is true, the message will be appended with a spinning loader animation
HasLoader bool

// gui contains the view it's gui
gui *Gui
}

type cell struct {
Expand All @@ -137,7 +140,7 @@ func (l lineType) String() string {
}

// newView returns a new View object.
func newView(name string, x0, y0, x1, y1 int, mode OutputMode) *View {
func (g *Gui) newView(name string, x0, y0, x1, y1 int, mode OutputMode) *View {
v := &View{
name: name,
x0: x0,
Expand All @@ -149,6 +152,7 @@ func newView(name string, x0, y0, x1, y1 int, mode OutputMode) *View {
Editor: DefaultEditor,
tainted: true,
ei: newEscapeInterpreter(mode),
gui: g,
}
return v
}
Expand Down Expand Up @@ -587,9 +591,9 @@ func (v *View) linesPosOnScreen(x, y int) (viewX int, viewY int, visable bool) {

maxX, maxY := v.Size()
if !v.Wrap {
visable = y >= v.oy && y <= maxY && x >= v.ox && x < maxX
viewX = x
viewY = y
visable = viewY >= v.oy && viewY < v.oy+maxY && viewX >= v.ox && viewX < v.ox+maxX
return
}

Expand Down

0 comments on commit 57e883a

Please sign in to comment.