go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/auth/internal/terminal_windows.go (about)

     1  // Copyright 2023 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  //go:build windows
    16  // +build windows
    17  
    18  package internal
    19  
    20  import (
    21  	"os"
    22  
    23  	"golang.org/x/sys/windows"
    24  )
    25  
    26  // EnableVirtualTerminal allows ANSI like escape sequences to be captured
    27  // when running in a windows terminal.
    28  func EnableVirtualTerminal() (supported bool, done func()) {
    29  	fd := windows.Handle(os.Stdout.Fd())
    30  	var originalMode uint32
    31  
    32  	err := windows.GetConsoleMode(fd, &originalMode)
    33  	if err != nil {
    34  		return false, nil
    35  	}
    36  
    37  	err = windows.SetConsoleMode(fd, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
    38  	if err != nil {
    39  		windows.SetConsoleMode(fd, originalMode)
    40  		return false, nil
    41  	}
    42  
    43  	return true, func() {
    44  		windows.SetConsoleMode(fd, originalMode)
    45  	}
    46  }
    47  
    48  // IsDumbTerminal is currently a dummy function on windows systems. Returns
    49  // false to assume using control characters.
    50  func IsDumbTerminal() bool {
    51  	return false
    52  }