github.com/hugorut/terraform@v1.1.3/src/terminal/impl_others.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package terminal
     5  
     6  import (
     7  	"os"
     8  
     9  	"golang.org/x/term"
    10  )
    11  
    12  // This is the implementation for all operating systems except Windows, where
    13  // we don't expect to need to do any special initialization to get a working
    14  // Virtual Terminal.
    15  //
    16  // For this implementation we just delegate everything upstream to
    17  // golang.org/x/term, since it already has a variety of different
    18  // implementations for quirks of more esoteric operating systems like plan9,
    19  // and will hopefully grow to include others as Go is ported to other platforms
    20  // in future.
    21  //
    22  // For operating systems that golang.org/x/term doesn't support either, it
    23  // defaults to indicating that nothing is a terminal and returns an error when
    24  // asked for a size, which we'll handle below.
    25  
    26  func configureOutputHandle(f *os.File) (*OutputStream, error) {
    27  	return &OutputStream{
    28  		File:       f,
    29  		isTerminal: isTerminalGolangXTerm,
    30  		getColumns: getColumnsGolangXTerm,
    31  	}, nil
    32  }
    33  
    34  func configureInputHandle(f *os.File) (*InputStream, error) {
    35  	return &InputStream{
    36  		File:       f,
    37  		isTerminal: isTerminalGolangXTerm,
    38  	}, nil
    39  }
    40  
    41  func isTerminalGolangXTerm(f *os.File) bool {
    42  	return term.IsTerminal(int(f.Fd()))
    43  }
    44  
    45  func getColumnsGolangXTerm(f *os.File) int {
    46  	width, _, err := term.GetSize(int(f.Fd()))
    47  	if err != nil {
    48  		// Suggests that it's either not a terminal at all or that we're on
    49  		// a platform that golang.org/x/term doesn't support. In both cases
    50  		// we'll just return the placeholder default value.
    51  		return defaultColumns
    52  	}
    53  	return width
    54  }