forked from methos2016/DuckyManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.go
90 lines (74 loc) · 1.78 KB
/
state.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
package main
import "github.com/nsf/termbox-go"
// State is just a holder for the current state of the program
type State struct {
Title string
Scripts []Script
Position int
PositionUpper int
}
// DefaultState will return the default values for a state
func DefaultState(scripts []Script) State {
return State{
Title: "Main",
Scripts: scripts,
Position: 0,
PositionUpper: 0,
}
}
// Down handles a keystroke on the "down" arrow
func (s *State) Down() {
if s.Position+1 < len(s.Scripts) {
s.Position++
_, h := termbox.Size()
// Magic numbers... "-" because of the title at the bottom
if s.Position-s.PositionUpper > h-3 {
s.PositionUpper++
}
}
}
// Up handles a keystroke on the "up" arrow
func (s *State) Up() {
if s.Position-1 >= 0 {
s.Position--
if s.Position < s.PositionUpper {
s.PositionUpper--
}
}
}
// Home handles a keystroke on the "home" key
func (s *State) Home() {
s.Position = 0
s.PositionUpper = 0
}
// End handles a keystroke on the "end" key
func (s *State) End() {
s.Position = len(s.Scripts) - 1
_, h := termbox.Size()
s.PositionUpper = len(s.Scripts) - h
if s.PositionUpper < 0 {
s.PositionUpper = 0
}
}
// SwitchKey will pick the correct function for the pressed key
func (s *State) SwitchKey(ev termbox.Event) {
switch ev.Key {
case termbox.KeyArrowDown:
s.Down()
case termbox.KeyArrowUp:
s.Up()
case termbox.KeyHome:
s.Home()
case termbox.KeyEnd:
s.End()
default:
// will call mainLoop by itself if needed, then come back
if err := pickFunctionality(ev, *s); err != nil {
l.Println(translate.ErrPickingFunc + ": " + err.Error())
}
}
}
// GetCurrentScript will return the script on the current position
func (s *State) GetCurrentScript() Script {
return s.Scripts[s.Position]
}