gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/elvish/edit/tty/setup_unix.go (about)

     1  // +build !windows,!plan9
     2  
     3  package tty
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  
     9  	"github.com/u-root/u-root/cmds/core/elvish/sys"
    10  	"github.com/u-root/u-root/cmds/core/elvish/util"
    11  )
    12  
    13  const flushInputDuringSetup = false
    14  
    15  func setup(in, out *os.File) (func() error, error) {
    16  	// On Unix, use input file for changing termios. All fds pointing to the
    17  	// same terminal are equivalent.
    18  
    19  	fd := int(in.Fd())
    20  	term, err := sys.NewTermiosFromFd(fd)
    21  	if err != nil {
    22  		return nil, fmt.Errorf("can't get terminal attribute: %s", err)
    23  	}
    24  
    25  	savedTermios := term.Copy()
    26  
    27  	term.SetICanon(false)
    28  	term.SetEcho(false)
    29  	term.SetVMin(1)
    30  	term.SetVTime(0)
    31  
    32  	// Enforcing crnl translation on readline. Assuming user won't set
    33  	// inlcr or -onlcr, otherwise we have to hardcode all of them here.
    34  	term.SetICRNL(true)
    35  
    36  	err = term.ApplyToFd(fd)
    37  	if err != nil {
    38  		return nil, fmt.Errorf("can't set up terminal attribute: %s", err)
    39  	}
    40  
    41  	var errFlushInput error
    42  	if flushInputDuringSetup {
    43  		err = sys.FlushInput(fd)
    44  		if err != nil {
    45  			errFlushInput = fmt.Errorf("can't flush input: %s", err)
    46  		}
    47  	}
    48  
    49  	var errSetupVT error
    50  	err = setupVT(out)
    51  	if err != nil {
    52  		errSetupVT = fmt.Errorf("can't setup VT: %s", err)
    53  	}
    54  
    55  	restore := func() error {
    56  		return util.Errors(savedTermios.ApplyToFd(fd), restoreVT(out))
    57  	}
    58  
    59  	return restore, util.Errors(errFlushInput, errSetupVT)
    60  }