github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/terminal/impl_others.go (about)

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