github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/process_unix.go (about)

     1  //go:build !windows && !plan9 && !js
     2  // +build !windows,!plan9,!js
     3  
     4  package lang
     5  
     6  import (
     7  	"io"
     8  	"os"
     9  	"os/signal"
    10  	"syscall"
    11  
    12  	"github.com/creack/pty"
    13  	"github.com/lmorg/murex/builtins/pipes/psuedotty"
    14  	"github.com/lmorg/murex/builtins/pipes/streams"
    15  	"github.com/lmorg/murex/lang/stdio"
    16  	"github.com/lmorg/murex/lang/types"
    17  	"github.com/lmorg/murex/utils/readline"
    18  )
    19  
    20  func ttys(p *Process) {
    21  	if p.CCExists != nil && p.CCExists(p.Name.String()) {
    22  		p.Stderr, p.CCErr = streams.NewTee(p.Stderr)
    23  		p.CCErr.SetDataType(types.Generic)
    24  
    25  		if p.Stdout.IsTTY() {
    26  			width, height, err := readline.GetSize(int(p.Stdout.File().Fd()))
    27  			if err != nil {
    28  				width, height = 80, 25
    29  			}
    30  
    31  			var tee stdio.Io
    32  			p.Stdout, tee, p.CCOut, err = psuedotty.NewTeePTY(width, height)
    33  			if err != nil {
    34  				p.Stdout, p.CCOut = streams.NewTee(p.Stdout)
    35  				return
    36  			}
    37  
    38  			ch := make(chan os.Signal, 1)
    39  			signal.Notify(ch, syscall.SIGWINCH)
    40  			go func() {
    41  				for range ch {
    42  					_ = pty.InheritSize(os.Stdout, p.Stdout.File())
    43  				}
    44  			}()
    45  
    46  			go func() {
    47  				_, _ = io.Copy(os.Stdout, tee)
    48  				signal.Stop(ch)
    49  				close(ch)
    50  			}()
    51  
    52  		} else {
    53  			p.Stdout, p.CCOut = streams.NewTee(p.Stdout)
    54  		}
    55  	}
    56  }