/go-console

Cross-platform PTY interface

Primary LanguageGoMIT LicenseMIT

go-console

Main GoDoc

go-console is a cross-platform PTY interface. On *nix platforms we rely on pty and on windows go-winpty (go-console will ship with winpty.dll and signed winpty-agent.exe from c:\Program Files\Microsoft VS Code\resources\app\node_modules.asar.unpacked\node-pty\build\Release using go:embed, so there's no need to include winpty binaries)

Credits:

Example

package main

import (
	"io"
	"log"
	"os"
	"runtime"
	"sync"

	"github.com/abakum/go-console"
)

func main() {

	proc, err := console.New(120, 60)
	if err != nil {
		panic(err)
	}
	defer proc.Close()

	var args []string

	log.Println("runtime.GOOS", runtime.GOOS)
	log.Println("runtime.GOARCH", runtime.GOARCH)
	if runtime.GOOS == "windows" {
		args = []string{"cmd.exe", "/c", "dir"}
	} else {
		args = []string{"ls", "-lah", "--color"}
	}

	if err := proc.Start(args); err != nil {
		panic(err)
	}

	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		defer wg.Done()

		_, err = io.Copy(os.Stdout, proc)
		if err != nil {
			log.Printf("Error: %v\n", err)
		}
	}()

	if _, err := proc.Wait(); err != nil {
		log.Printf("Wait err: %v\n", err)
	}

	wg.Wait()
}