github.com/mutagen-io/mutagen@v0.18.0-rc1/cmd/terminal_windows.go (about)

     1  package cmd
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"os/exec"
     8  
     9  	isatty "github.com/mattn/go-isatty"
    10  )
    11  
    12  // HandleTerminalCompatibility automatically restarts the current process inside
    13  // a terminal compatibility emulator if necessary. It currently only handles the
    14  // case of mintty consoles on Windows requiring a relaunch of the current
    15  // command inside winpty.
    16  func HandleTerminalCompatibility() {
    17  	// If we're not running inside a mintty-based terminal, then there's nothing
    18  	// that we need to do.
    19  	if !isatty.IsCygwinTerminal(os.Stdout.Fd()) {
    20  		return
    21  	}
    22  
    23  	// Since we're running inside a mintty-based terminal, we need to relaunch
    24  	// using winpty, so first attempt to locate it.
    25  	winpty, err := exec.LookPath("winpty")
    26  	if err != nil {
    27  		Fatal(errors.New("running inside mintty terminal and unable to locate winpty"))
    28  	}
    29  
    30  	// Compute the path to the current executable.
    31  	executable, err := os.Executable()
    32  	if err != nil {
    33  		Fatal(fmt.Errorf("running inside mintty terminal and unable to locate current executable: %w", err))
    34  	}
    35  
    36  	// Build the argument list for winpty.
    37  	arguments := make([]string, 0, len(os.Args))
    38  	arguments = append(arguments, executable)
    39  	arguments = append(arguments, os.Args[1:]...)
    40  
    41  	// Create the command that we'll run.
    42  	command := exec.Command(winpty, arguments...)
    43  
    44  	// Set up its input/output streams.
    45  	command.Stdin = os.Stdin
    46  	command.Stdout = os.Stdout
    47  	command.Stderr = os.Stderr
    48  
    49  	// Run the command and terminate with its exit code.
    50  	command.Run()
    51  	os.Exit(command.ProcessState.ExitCode())
    52  }