-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselector.go
48 lines (40 loc) · 1.08 KB
/
selector.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
package gmir
import (
"fmt"
"github.com/codesoap/gmir/selector"
)
// AddDigitToSelector adds a single digit to the end of v.selector.
func (v *View) AddDigitToSelector(digit int) {
if digit < 0 || digit > 9 {
panic(fmt.Sprintf("'%d' is not a digit", digit))
}
v.selector += fmt.Sprint(digit)
}
// ClearSelector sets v.selector to an empty string.
func (v *View) ClearSelector() {
v.selector = ""
}
// SelectorNumber returns the currently selected index.
func (v View) SelectorIndex() int {
return selector.ToIndex(v.selector)
}
// SelectorIsValid returns true, if the selector is complete and
// resolves to a valid selectable.
func (v View) SelectorIsValid() bool {
return selector.IsComplete(v.selector) && v.selectorIsInRange()
}
func (v View) selectorIsInRange() bool {
i := selector.ToIndex(v.selector)
switch v.selectable {
case link:
return i < len(v.links())
case heading:
return i < len(v.headings())
}
panic("unknown selectable")
}
// LinkURL returns the URL for v.selector.
func (v View) LinkURL() string {
i := selector.ToIndex(v.selector)
return v.links()[i].URL()
}