Skip to content

Commit

Permalink
Add ability to intercept streams with middleware
Browse files Browse the repository at this point in the history
Add the ability to add support for such things as zmodem and/or other
stream middleware's
  • Loading branch information
mgazza committed Aug 14, 2024
1 parent 8a75938 commit d950d7a
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions term.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type Terminal struct {
printData []byte
printer Printer
cmd *exec.Cmd
middleware Middleware
}

// Printer is used for spooling print data when its received.
Expand Down Expand Up @@ -266,8 +267,12 @@ func (t *Terminal) open() error {
if err != nil {
return err
}
t.in = in
t.out = out

t.in, t.out = in, out
if t.middleware != nil {
t.out, t.in = t.middleware.SetupMiddleware(out, in)
}

t.pty = pty

t.updatePTYSize()
Expand Down Expand Up @@ -353,8 +358,10 @@ func (t *Terminal) RunWithConnection(in io.WriteCloser, out io.Reader) error {
for t.config.Columns == 0 { // don't load the TTY until our output is configured
time.Sleep(time.Millisecond * 50)
}
t.in = in
t.out = out
t.in, t.out = in, out
if t.middleware != nil {
t.out, t.in = t.middleware.SetupMiddleware(out, in)
}

t.run()

Expand Down Expand Up @@ -497,3 +504,20 @@ func (t *Terminal) Dragged(d *fyne.DragEvent) {
func (t *Terminal) DragEnd() {
t.selecting = false
}

// SetMiddleware sets the middleware function that will be used when creating a new terminal.
// The middleware function is responsible for setting up the I/O readers and writers.
func (t *Terminal) SetMiddleware(mw Middleware) {
t.middleware = mw
}

// Middleware is an interface that defines the methods required to set up
// the input (reader) and output (writer) streams for the terminal.
// Implementations of this interface can modify or wrap the reader and writer.
type Middleware interface {
// SetupMiddleware configures the input and output streams for the terminal.
// It takes an input reader (r) and an output writer (w) as arguments.
// The function returns a possibly modified reader and writer that
// the terminal will use for I/O operations.
SetupMiddleware(r io.Reader, w io.WriteCloser) (io.Reader, io.WriteCloser)
}

0 comments on commit d950d7a

Please sign in to comment.