-
Notifications
You must be signed in to change notification settings - Fork 15
/
macro.go
39 lines (33 loc) · 952 Bytes
/
macro.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
package main
// Macro represents a series of keypresses that can be played back later
type Macro struct {
KeyPresses []string
index int // current position, when playing back
Recording bool
}
// NewMacro creates a new Macro struct
func NewMacro() *Macro {
var m Macro
m.KeyPresses = make([]string, 0)
return &m
}
// Add adds a keypress to this macro, when recording
func (m *Macro) Add(keyPress string) {
m.KeyPresses = append(m.KeyPresses, keyPress)
}
// Next returns the next keypress when playing back a macro, or an empty string
func (m *Macro) Next() string {
if kc := len(m.KeyPresses); kc == 0 || m.index >= kc {
return ""
}
defer func() { m.index++ }()
return m.KeyPresses[m.index]
}
// Home moves the current keypress index back to 0, for playing back the macro again
func (m *Macro) Home() {
m.index = 0
}
// Len returns the number of keypresses in this macro
func (m *Macro) Len() int {
return len(m.KeyPresses)
}