-
Notifications
You must be signed in to change notification settings - Fork 15
/
send.go
52 lines (44 loc) · 1010 Bytes
/
send.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
package main
import (
"fmt"
"strings"
"time"
"github.com/yuin/gopher-lua"
"github.com/hymkor/expect/internal/go-console/typekeyas"
)
func send(str string, wait int) {
fmt.Fprintf(echo, "%s%s%s", escSend, strings.Replace(str, "\r", "\n", -1), escEnd)
for _, c := range str {
typekeyas.Rune(conIn, c)
if wait > 0 {
time.Sleep(time.Second * time.Duration(wait) / 1000)
}
}
}
func getWaitFrom2ndArg(L *lua.LState) int {
if val, ok := L.Get(2).(lua.LNumber); ok {
return int(val)
}
return 0
}
// Send is the implement of the lua-function `send`
func Send(L *lua.LState) int {
send(L.ToString(1), getWaitFrom2ndArg(L))
L.Push(lua.LTrue)
return 1
}
// Sendln sends 1st arguments and CR
func Sendln(L *lua.LState) int {
send(L.ToString(1)+"\r", getWaitFrom2ndArg(L))
L.Push(lua.LTrue)
return 1
}
func SendVKey(L *lua.LState) int {
n := L.GetTop()
for i := 1; i <= n; i++ {
if vkey, ok := L.Get(i).(lua.LNumber); ok {
typekeyas.VirtualKey(conIn, int(vkey))
}
}
return 0
}