Skip to content

Commit e888999

Browse files
committed
Feat: add notes
1 parent 490fcb8 commit e888999

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

console/common.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package console
33
import (
44
"errors"
55
"fmt"
6+
"io"
67
"os"
78
"runtime"
89
"strconv"
@@ -18,10 +19,12 @@ var (
1819

1920
type Console iface.Console
2021

22+
// Create a new pty
2123
func New(coder string, colorAble bool) Console {
2224
return newNative(coder, colorAble, 50, 50)
2325
}
2426

27+
// Create a new pty and initialize the size
2528
func 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
5256
func (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
6065
func (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
6886
func (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
7392
func (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
81101
func (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
89110
func (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
115137
func (c *console) GetSize() (uint, uint) {
116138
return c.initialCols, c.initialRows
117139
}

console/console.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type console struct {
3232
env []string
3333
}
3434

35+
// start pty subroutine
3536
func (c *console) Start(dir string, command []string) error {
3637
cmd, err := c.buildCmd(command)
3738
if err != nil {
@@ -71,18 +72,7 @@ func (c *console) buildCmd(args []string) (*exec.Cmd, error) {
7172
return cmd, nil
7273
}
7374

74-
func (c *console) StdIn() io.Writer {
75-
return c.stdIn
76-
}
77-
78-
func (c *console) StdOut() io.Reader {
79-
return c.stdOut
80-
}
81-
82-
func (c *console) StdErr() io.Reader {
83-
return nil
84-
}
85-
75+
// set pty window size
8676
func (c *console) SetSize(cols uint, rows uint) error {
8777
c.initialRows = rows
8878
c.initialCols = cols
@@ -92,6 +82,7 @@ func (c *console) SetSize(cols uint, rows uint) error {
9282
return pty.Setsize(c.file, &pty.Winsize{Cols: uint16(cols), Rows: uint16(rows)})
9383
}
9484

85+
// Get the process id of the pty subprogram
9586
func (c *console) Pid() int {
9687
if c.cmd == nil {
9788
return 0
@@ -107,6 +98,7 @@ func (c *console) findProcess() (*os.Process, error) {
10798
return c.cmd.Process, nil
10899
}
109100

101+
// Force kill pty subroutine
110102
func (c *console) Kill() error {
111103
proc, err := c.findProcess()
112104
if err != nil {

console/console_windows.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type console struct {
3838
env []string
3939
}
4040

41+
// start pty subroutine
4142
func (c *console) Start(dir string, command []string) error {
4243
dllDir, err := c.UnloadEmbeddedDeps()
4344
if err != nil {
@@ -169,18 +170,7 @@ func releases(f *bytes.Reader, targetPath string) error {
169170
return nil
170171
}
171172

172-
func (c *console) StdIn() io.Writer {
173-
return c.stdIn
174-
}
175-
176-
func (c *console) StdOut() io.Reader {
177-
return c.stdOut
178-
}
179-
180-
func (c *console) StdErr() io.Reader {
181-
return c.stdErr
182-
}
183-
173+
// set pty window size
184174
func (c *console) SetSize(cols uint, rows uint) error {
185175
c.initialRows = rows
186176
c.initialCols = cols
@@ -194,10 +184,12 @@ func (c *console) SetSize(cols uint, rows uint) error {
194184
return nil
195185
}
196186

187+
// Get the process id of the pty subprogram
197188
func (c *console) Pid() int {
198189
if c.file == nil {
199190
return 0
200191
}
192+
201193
return c.file.Pid()
202194
}
203195

@@ -208,6 +200,7 @@ func (c *console) findProcess() (*os.Process, error) {
208200
return os.FindProcess(c.Pid())
209201
}
210202

203+
// Force kill pty subroutine
211204
func (c *console) Kill() error {
212205
_, err := c.findProcess()
213206
if err != nil {

0 commit comments

Comments
 (0)