New class Watcher

This commit is contained in:
HAYAMA_Kaoru
2024-11-29 11:59:56 +09:00
parent eec555f935
commit 0961e035fc
2 changed files with 73 additions and 10 deletions
+59
View File
@@ -0,0 +1,59 @@
package main
import (
"io"
"os"
"strings"
"github.com/aymanbagabas/go-pty"
)
type Watcher struct {
ch <-chan string
lastline string
}
func NewWatcher(ptmx pty.Pty) *Watcher {
pipeline := make(chan string, 1024)
go io.Copy(ptmx, os.Stdin)
go func() {
for {
var buffer [1024]byte
n, err := ptmx.Read(buffer[:])
if err != nil {
close(pipeline)
return
}
// If the code below spends a lot of time,
// It hangs up to io.Copy(ptmx, os.Stdin)
// The reason is unknown.
os.Stdout.Write(buffer[:n])
pipeline <- string(buffer[:n])
}
}()
return &Watcher{ch: pipeline}
}
func (W *Watcher) updateLastLine() {
newLinePos := strings.LastIndexByte(W.lastline, '\n')
if newLinePos >= 0 {
W.lastline = W.lastline[newLinePos+1:]
} else {
W.lastline = ""
}
}
func (W *Watcher) Expect(words ...string) int {
for frag := range W.ch {
W.lastline += frag
for i, word := range words {
if strings.Contains(W.lastline, word) {
W.updateLastLine()
return i
}
}
W.updateLastLine()
}
return -1
}