Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncInteractChannels #35

Open
network4all opened this issue Nov 29, 2018 · 0 comments
Open

AsyncInteractChannels #35

network4all opened this issue Nov 29, 2018 · 0 comments

Comments

@network4all
Copy link

Hi,

I was testing "AsyncInteractChannels". When using:

send, receiver := child.AsyncInteractChannels()

Somehow the output looks buffered or waiting on lines '\n'. Would it be possible to create a version without buffering? So each character is passed without delay. Example code:

package main

import (
	"bufio"
	"fmt"
	"os"
	"time"

	"github.com/ThomasRooney/gexpect"
)

func main() {
	// open program
	child, err := gexpect.Spawn("/bin/sh")
	if err != nil {
		panic("could not open program " + err.Error())
	}
	defer child.Close()

	// open in/out channels
	doCommand, receiver := child.AsyncInteractChannels()
	doneChan := make(chan bool)
	tickChan := time.NewTicker(time.Millisecond * 10000).C

	// keyboard
	reader := bufio.NewReader(os.Stdin)
	keyboard := make(chan string)
	go func(keyboard chan string) {
		for {
			s, _, err := reader.ReadRune()
			if err != nil {
				close(keyboard)
				return
			}
			keyboard <- string(s)
		}
	}(keyboard)

	// the loop
	for {
		select {
		case msg, open := <-receiver:
			if !open {
				return
			}
			fmt.Printf("%s\n", msg)
		case <-tickChan:
			doCommand <- "echo example trigger 1000 seconds\n"
		case commandline := <-keyboard:
			doCommand <- commandline
		case <-doneChan:
			fmt.Println("Done")
			return
		}
	}
}

Regards, Arjen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant