Skip to content

Commit 5fac5de

Browse files
author
Guilherme Ferreira
committed
Extracted basic funcionality and added tests
1 parent 4c2e459 commit 5fac5de

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sloop

main.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,39 @@ import (
66
"github.com/go-vgo/robotgo"
77
)
88

9+
func checkLoop(screenWidth, mouseX, activationMargin int) string {
10+
11+
if mouseX+activationMargin > screenWidth {
12+
return "left"
13+
} else if mouseX-activationMargin < 0 {
14+
return "right"
15+
}
16+
17+
return ""
18+
}
19+
920
func main() {
1021

22+
var (
23+
activationMargin = 10
24+
jumpDelta = 1
25+
checkFrequency = 50
26+
)
27+
1128
for {
1229
screenWidth, _ := robotgo.GetScreenSize()
13-
1430
mouseX, mouseY := robotgo.GetMousePos()
1531

16-
if mouseX+10 > screenWidth {
17-
robotgo.MoveMouse(11, mouseY)
18-
} else if mouseX-10 < 0 {
19-
robotgo.MoveMouse(screenWidth-11, mouseY)
32+
loop := checkLoop(screenWidth, mouseX, activationMargin)
33+
34+
switch loop {
35+
case "left":
36+
robotgo.MoveMouse(activationMargin+jumpDelta, mouseY)
37+
case "right":
38+
robotgo.MoveMouse(screenWidth-(activationMargin+jumpDelta), mouseY)
2039
}
2140

22-
time.Sleep(50 * time.Millisecond)
41+
time.Sleep(time.Duration(checkFrequency) * time.Millisecond)
2342
}
2443

2544
}

main_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import "testing"
4+
5+
func Test_checkLoop(t *testing.T) {
6+
type args struct {
7+
screenWidth int
8+
mouseX int
9+
activationMargin int
10+
}
11+
tests := []struct {
12+
name string
13+
args args
14+
want string
15+
}{
16+
{"Test Left Jump", args{100, 91, 10}, "left"},
17+
{"Test Right Jump", args{100, 1, 10}, "right"},
18+
{"Test No Jump", args{100, 50, 10}, ""},
19+
}
20+
for _, tt := range tests {
21+
t.Run(tt.name, func(t *testing.T) {
22+
if got := checkLoop(tt.args.screenWidth, tt.args.mouseX, tt.args.activationMargin); got != tt.want {
23+
t.Errorf("checkLoop() = %v, want %v", got, tt.want)
24+
}
25+
})
26+
}
27+
}

0 commit comments

Comments
 (0)