github.com/opentofu/opentofu@v1.7.1/internal/terminal/impl_others.go (about)

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