Skip to content

Commit

Permalink
Feat: add notes
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Sep 19, 2022
1 parent 490fcb8 commit e888999
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
22 changes: 22 additions & 0 deletions console/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package console
import (
"errors"
"fmt"
"io"
"os"
"runtime"
"strconv"
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}
16 changes: 4 additions & 12 deletions console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand Down
17 changes: 5 additions & 12 deletions console/console_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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()
}

Expand All @@ -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 {
Expand Down

0 comments on commit e888999

Please sign in to comment.