-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposition.go
89 lines (75 loc) · 1.99 KB
/
position.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"errors"
"github.com/xyproto/vt100"
)
// Position represents a position on the screen, including how far down the view has scrolled
type Position struct {
sx int // the position of the cursor in the current scrollview
sy int // the position of the cursor in the current scrollview
offset int // how far one has scrolled
scrollSpeed int // how many lines to scroll, when scrolling
savedX int // for smart down cursor movement
}
// NewPosition returns a new Position struct
func NewPosition(scrollSpeed int) *Position {
return &Position{0, 0, 0, scrollSpeed, 0}
}
// Copy will create a new Position struct that is a copy of this one
func (p *Position) Copy() Position {
var p2 Position
p2.sx = p.sx
p2.sy = p.sy
p2.offset = p.offset
p2.scrollSpeed = p.scrollSpeed
p2.savedX = p.savedX
return p2
}
// ScreenX returns the screen X position in the current view
func (p *Position) ScreenX() int {
return p.sx
}
// ScreenY returns the screen Y position in the current view
func (p *Position) ScreenY() int {
return p.sy
}
// Offset returns the scroll offset for the current view
func (p *Position) Offset() int {
return p.offset
}
// SetX will set the screen X position
func (p *Position) SetX(x int) {
p.sx = x
}
// SetY will set the screen Y position
func (p *Position) SetY(y int) {
p.sy = y
}
// SetOffset will set the screen scolling offset
func (p *Position) SetOffset(offset int) {
p.offset = offset
}
// Up will move the cursor up
func (p *Position) Up() error {
if p.sy <= 0 {
return errors.New("already at the top of the canvas")
}
p.sy--
return nil
}
// Down will move the cursor down
func (p *Position) Down(c *vt100.Canvas) error {
h := 25
if c != nil {
h = int(c.H())
}
if p.sy >= h-1 {
return errors.New("already at the bottom of the canvas")
}
p.sy++
return nil
}
// AtStartOfLine returns true if the position is at the very start of the line, regardless of whitespace
func (p *Position) AtStartOfLine() bool {
return p.sx == 0
}