diff --git a/console/common.go b/console/common.go index a9870b5..83e2185 100644 --- a/console/common.go +++ b/console/common.go @@ -3,6 +3,7 @@ package console import ( "errors" "fmt" + "io" "os" "runtime" "strconv" @@ -18,10 +19,12 @@ var ( type Console iface.Console +// Create a new pty func New(coder string, colorAble bool) Console { return newNative(coder, colorAble, 50, 50) } +// Create a new pty and initialize the size func NewWithSize(coder string, colorAble bool, Cols, Rows uint) Console { return newNative(coder, colorAble, Cols, Rows) } @@ -49,6 +52,7 @@ func newNative(coder string, colorAble bool, Cols, Rows uint) Console { return &console } +// Read data from pty console func (c *console) Read(b []byte) (int, error) { if c.file == nil { return 0, ErrProcessNotStarted @@ -57,6 +61,7 @@ func (c *console) Read(b []byte) (int, error) { return c.StdOut().Read(b) } +// Write data to the pty console func (c *console) Write(b []byte) (int, error) { if c.file == nil { return 0, ErrProcessNotStarted @@ -65,11 +70,25 @@ func (c *console) Write(b []byte) (int, error) { return c.StdIn().Write(b) } +func (c *console) StdIn() io.Writer { + return c.stdIn +} + +func (c *console) StdOut() io.Reader { + return c.stdOut +} + +func (c *console) StdErr() io.Reader { + return c.stdErr +} + +// Add environment variables before start func (c *console) AddENV(environ []string) error { c.env = append(c.env, environ...) return nil } +// close the pty and kill the subroutine func (c *console) Close() error { if c.file == nil { return ErrProcessNotStarted @@ -78,6 +97,7 @@ func (c *console) Close() error { return c.file.Close() } +// wait for the pty subroutine to exit func (c *console) Wait() (*os.ProcessState, error) { proc, err := c.findProcess() if err != nil { @@ -86,6 +106,7 @@ func (c *console) Wait() (*os.ProcessState, error) { return proc.Wait() } +// Send system signals to pty subroutines func (c *console) Signal(sig os.Signal) error { proc, err := c.findProcess() if err != nil { @@ -112,6 +133,7 @@ func (c *console) ResizeWithString(sizeText string) error { return c.SetSize(uint(cols), uint(rows)) } +// Get pty window size func (c *console) GetSize() (uint, uint) { return c.initialCols, c.initialRows } diff --git a/console/console.go b/console/console.go index dede9b0..b7a280e 100644 --- a/console/console.go +++ b/console/console.go @@ -32,6 +32,7 @@ type console struct { env []string } +// start pty subroutine func (c *console) Start(dir string, command []string) error { cmd, err := c.buildCmd(command) if err != nil { @@ -71,18 +72,7 @@ func (c *console) buildCmd(args []string) (*exec.Cmd, error) { return cmd, nil } -func (c *console) StdIn() io.Writer { - return c.stdIn -} - -func (c *console) StdOut() io.Reader { - return c.stdOut -} - -func (c *console) StdErr() io.Reader { - return nil -} - +// set pty window size func (c *console) SetSize(cols uint, rows uint) error { c.initialRows = rows c.initialCols = cols @@ -92,6 +82,7 @@ func (c *console) SetSize(cols uint, rows uint) error { return pty.Setsize(c.file, &pty.Winsize{Cols: uint16(cols), Rows: uint16(rows)}) } +// Get the process id of the pty subprogram func (c *console) Pid() int { if c.cmd == nil { return 0 @@ -107,6 +98,7 @@ func (c *console) findProcess() (*os.Process, error) { return c.cmd.Process, nil } +// Force kill pty subroutine func (c *console) Kill() error { proc, err := c.findProcess() if err != nil { diff --git a/console/console_windows.go b/console/console_windows.go index a7a3ca2..28ad1ba 100644 --- a/console/console_windows.go +++ b/console/console_windows.go @@ -38,6 +38,7 @@ type console struct { env []string } +// start pty subroutine func (c *console) Start(dir string, command []string) error { dllDir, err := c.UnloadEmbeddedDeps() if err != nil { @@ -169,18 +170,7 @@ func releases(f *bytes.Reader, targetPath string) error { return nil } -func (c *console) StdIn() io.Writer { - return c.stdIn -} - -func (c *console) StdOut() io.Reader { - return c.stdOut -} - -func (c *console) StdErr() io.Reader { - return c.stdErr -} - +// set pty window size func (c *console) SetSize(cols uint, rows uint) error { c.initialRows = rows c.initialCols = cols @@ -194,10 +184,12 @@ func (c *console) SetSize(cols uint, rows uint) error { return nil } +// Get the process id of the pty subprogram func (c *console) Pid() int { if c.file == nil { return 0 } + return c.file.Pid() } @@ -208,6 +200,7 @@ func (c *console) findProcess() (*os.Process, error) { return os.FindProcess(c.Pid()) } +// Force kill pty subroutine func (c *console) Kill() error { _, err := c.findProcess() if err != nil {