mirror of
https://github.com/hymkor/lispect.git
synced 2026-09-02 15:17:49 -04:00
Add (*Watcher) ExpectWithTimeout
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
|
|
||||||
@@ -20,8 +21,10 @@ func loop(ptmx pty.Pty) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = watcher.Expect("100")
|
i := watcher.ExpectWithTimeout(time.Duration(10*time.Second), "100")
|
||||||
|
// i := watcher.Expect("100")
|
||||||
io.WriteString(ptmx, "exit\r")
|
io.WriteString(ptmx, "exit\r")
|
||||||
|
println(i)
|
||||||
return sh.Wait()
|
return sh.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+33
-8
@@ -4,6 +4,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/aymanbagabas/go-pty"
|
"github.com/aymanbagabas/go-pty"
|
||||||
)
|
)
|
||||||
@@ -44,16 +45,40 @@ func (W *Watcher) updateLastLine() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (W *Watcher) checkWords(token string, words []string) int {
|
||||||
|
W.lastline += token
|
||||||
|
for i, word := range words {
|
||||||
|
if strings.Contains(W.lastline, word) {
|
||||||
|
W.updateLastLine()
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
W.updateLastLine()
|
||||||
|
return -1
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (W *Watcher) Expect(words ...string) int {
|
func (W *Watcher) Expect(words ...string) int {
|
||||||
for frag := range W.ch {
|
for token := range W.ch {
|
||||||
W.lastline += frag
|
if found := W.checkWords(token, words); found >= 0 {
|
||||||
for i, word := range words {
|
return found
|
||||||
if strings.Contains(W.lastline, word) {
|
|
||||||
W.updateLastLine()
|
|
||||||
return i
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
W.updateLastLine()
|
|
||||||
}
|
}
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (W *Watcher) ExpectWithTimeout(d time.Duration, words ...string) int {
|
||||||
|
timer := time.NewTimer(d)
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case token := <-W.ch:
|
||||||
|
if found := W.checkWords(token, words); found >= 0 {
|
||||||
|
return found
|
||||||
|
}
|
||||||
|
case <-timer.C:
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user