@@ -3,6 +3,7 @@ package console
33import (
44 "errors"
55 "fmt"
6+ "io"
67 "os"
78 "runtime"
89 "strconv"
@@ -18,10 +19,12 @@ var (
1819
1920type Console iface.Console
2021
22+ // Create a new pty
2123func New (coder string , colorAble bool ) Console {
2224 return newNative (coder , colorAble , 50 , 50 )
2325}
2426
27+ // Create a new pty and initialize the size
2528func NewWithSize (coder string , colorAble bool , Cols , Rows uint ) Console {
2629 return newNative (coder , colorAble , Cols , Rows )
2730}
@@ -49,6 +52,7 @@ func newNative(coder string, colorAble bool, Cols, Rows uint) Console {
4952 return & console
5053}
5154
55+ // Read data from pty console
5256func (c * console ) Read (b []byte ) (int , error ) {
5357 if c .file == nil {
5458 return 0 , ErrProcessNotStarted
@@ -57,6 +61,7 @@ func (c *console) Read(b []byte) (int, error) {
5761 return c .StdOut ().Read (b )
5862}
5963
64+ // Write data to the pty console
6065func (c * console ) Write (b []byte ) (int , error ) {
6166 if c .file == nil {
6267 return 0 , ErrProcessNotStarted
@@ -65,11 +70,25 @@ func (c *console) Write(b []byte) (int, error) {
6570 return c .StdIn ().Write (b )
6671}
6772
73+ func (c * console ) StdIn () io.Writer {
74+ return c .stdIn
75+ }
76+
77+ func (c * console ) StdOut () io.Reader {
78+ return c .stdOut
79+ }
80+
81+ func (c * console ) StdErr () io.Reader {
82+ return c .stdErr
83+ }
84+
85+ // Add environment variables before start
6886func (c * console ) AddENV (environ []string ) error {
6987 c .env = append (c .env , environ ... )
7088 return nil
7189}
7290
91+ // close the pty and kill the subroutine
7392func (c * console ) Close () error {
7493 if c .file == nil {
7594 return ErrProcessNotStarted
@@ -78,6 +97,7 @@ func (c *console) Close() error {
7897 return c .file .Close ()
7998}
8099
100+ // wait for the pty subroutine to exit
81101func (c * console ) Wait () (* os.ProcessState , error ) {
82102 proc , err := c .findProcess ()
83103 if err != nil {
@@ -86,6 +106,7 @@ func (c *console) Wait() (*os.ProcessState, error) {
86106 return proc .Wait ()
87107}
88108
109+ // Send system signals to pty subroutines
89110func (c * console ) Signal (sig os.Signal ) error {
90111 proc , err := c .findProcess ()
91112 if err != nil {
@@ -112,6 +133,7 @@ func (c *console) ResizeWithString(sizeText string) error {
112133 return c .SetSize (uint (cols ), uint (rows ))
113134}
114135
136+ // Get pty window size
115137func (c * console ) GetSize () (uint , uint ) {
116138 return c .initialCols , c .initialRows
117139}
0 commit comments