Skip to content

AsyncInteractChannels #35

Description

@network4all

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions