conpty

package module
v0.0.0-...-e4c15aa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2022 License: MIT Imports: 5 Imported by: 0

README

conpty

Windows Pseudo Console (ConPTY) for Golang

See:

https://devblogs.microsoft.com/commandline/windows-command-line-introducing-the-windows-pseudo-console-conpty/

Usage

package main

import (
	"context"
	"io"
	"log"
	"os"

	"github.com/UserExistsError/conpty"
)

func main() {
	commandLine := `c:\windows\system32\cmd.exe`
	cpty, err := conpty.Start(commandLine)
	if err != nil {
		log.Fatalf("Failed to spawn a pty:  %v", err)
	}
	defer cpty.Close()

	go func() {
		go io.Copy(os.Stdout, cpty)
		io.Copy(cpty, os.Stdin)
	}()

	exitCode, err := cpty.Wait(context.Background())
	if err != nil {
		log.Fatalf("Error: %v", err)
	}
	log.Printf("ExitCode: %d", exitCode)
}

Documentation

Rendered for windows/amd64

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConPtyUnsupported = errors.New("ConPty is not available on this version of Windows")
)

Functions

func IsConPtyAvailable

func IsConPtyAvailable() bool

Types

type ConPty

type ConPty struct {
	// contains filtered or unexported fields
}

func Start

func Start(commandLine string, options ...ConPtyOption) (*ConPty, error)

Start a new process specified in `commandLine` and attach a pseudo console using the Windows ConPty API. If ConPty is not available, ErrConPtyUnsupported will be returned.

On successful return, an instance of ConPty is returned. You must call Close() on this to release any resources associated with the process. To get the exit code of the process, you can call Wait().

func (*ConPty) Close

func (cpty *ConPty) Close() error

Close all open handles and terminate the process.

func (*ConPty) Read

func (cpty *ConPty) Read(p []byte) (int, error)

func (*ConPty) Resize

func (cpty *ConPty) Resize(width, height int) error

func (*ConPty) Wait

func (cpty *ConPty) Wait(ctx context.Context) (uint32, error)

Wait for the process to exit and return the exit code. If context is canceled, Wait() will return STILL_ACTIVE and an error indicating the context was canceled.

func (*ConPty) Write

func (cpty *ConPty) Write(p []byte) (int, error)

type ConPtyOption

type ConPtyOption func(args *conPtyArgs)

func ConPtyDimensions

func ConPtyDimensions(width, height int) ConPtyOption

Directories

Path Synopsis
examples
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL