Skip to content

Commit 0642f66

Browse files
committed
Add new function: shot
1 parent 22e24f1 commit 0642f66

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ func mains() error {
127127
L.SetGlobal("spawnctx", L.NewFunction(SpawnContext))
128128
L.SetGlobal("kill", L.NewFunction(Kill))
129129
L.SetGlobal("wait", L.NewFunction(Wait))
130+
L.SetGlobal("shot", L.NewFunction(Shot))
130131

131132
table := L.NewTable()
132133
for i, s := range flag.Args() {

shot.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"github.com/yuin/gopher-lua"
5+
6+
"github.com/hymkor/expect/internal/go-console/output"
7+
)
8+
9+
func shot(n int) ([]string, error) {
10+
if !useStderrOnGetRecentOutput {
11+
result, err := consoleoutput.GetRecentOutputByStdout(n)
12+
if err == nil {
13+
return result, nil
14+
}
15+
useStderrOnGetRecentOutput = true
16+
}
17+
return consoleoutput.GetRecentOutputByStderr(n)
18+
}
19+
20+
func Shot(L *lua.LState) int {
21+
n, ok := L.Get(-1).(lua.LNumber)
22+
if !ok {
23+
L.Push(lua.LNil)
24+
L.Push(lua.LString("Expected a number"))
25+
return 2
26+
}
27+
result, err := shot(int(n))
28+
if err != nil {
29+
L.Push(lua.LNil)
30+
L.Push(lua.LString(err.Error()))
31+
return 2
32+
}
33+
table := L.NewTable()
34+
for i, line := range result {
35+
L.SetTable(table, lua.LNumber(i+1), lua.LString(line))
36+
}
37+
L.Push(table)
38+
return 1
39+
}

0 commit comments

Comments
 (0)