-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor.go
101 lines (83 loc) · 1.37 KB
/
cursor.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
90
91
92
93
94
95
96
97
98
99
100
101
package tui
// Cursor is a tool for keeping track of state in a 2D array
// You need to provide a height and width so that the changes can be clamped
type Cursor struct {
row, col, height, width int
}
func NewCursor(height, width int) Cursor {
c := Cursor{}
c.SetSize(height, width)
return c
}
func (c *Cursor) Position() (int, int) {
return c.row, c.col
}
func (c *Cursor) Size() (int, int) {
return (c.height + 1), (c.width + 1)
}
func (c *Cursor) Up() bool {
if c.row > 0 {
c.row--
return true
}
return false
}
func (c *Cursor) Down() bool {
if c.row < c.height {
c.row++
return true
}
return false
}
func (c *Cursor) Left() bool {
if c.col > 0 {
c.col--
return true
}
return false
}
func (c *Cursor) Right() bool {
if c.col < c.width {
c.col++
return true
}
return false
}
func (c *Cursor) Top() {
c.row = 0
}
func (c *Cursor) Bottom() {
c.row = c.height
}
func (c *Cursor) SetSize(height, width int) {
if height > 0 {
c.height = height - 1
} else {
c.height = 0
}
if c.row > c.height {
c.row = c.height
}
if width > 0 {
c.width = width - 1
} else {
c.width = 0
}
if c.col > c.width {
c.col = c.width
}
}
func (c *Cursor) SetPosition(row, col int) {
if row < 0 {
row = 0
} else if row > c.height {
row = c.height
}
c.row = row
if col < 0 {
col = 0
} else if col > c.width {
col = c.width
}
c.col = col
}