code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/printer/interactive_windows.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package printer
    17  
    18  import (
    19  	"io"
    20  	"os"
    21  	"sync"
    22  
    23  	"github.com/muesli/termenv"
    24  	"golang.org/x/sys/windows"
    25  )
    26  
    27  var enableANSI sync.Once
    28  
    29  func NewInteractivePrinter(w io.Writer) *InteractivePrinter {
    30  	enableLegacyWindowsANSI()
    31  	profile := termenv.EnvColorProfile()
    32  	return &InteractivePrinter{
    33  		writer:       w,
    34  		profile:      profile,
    35  		checkMark:    termenv.String("* ").Foreground(profile.Color("2")).String(),
    36  		questionMark: termenv.String("? ").Foreground(profile.Color("5")).String(),
    37  		crossMark:    termenv.String("x ").Foreground(profile.Color("1")).String(),
    38  		bangMark:     termenv.String("! "),
    39  		arrow:        termenv.String("> "),
    40  	}
    41  }
    42  
    43  // enableANSIColors enables support for ANSI color sequences in the Windows
    44  // default console (cmd.exe and the PowerShell application). Note that this
    45  // only works with Windows 10. Also note that Windows Terminal supports colors
    46  // by default.
    47  //
    48  // Thanks for LipGloss for the hack:
    49  //
    50  //	https://github.com/charmbracelet/lipgloss
    51  func enableLegacyWindowsANSI() {
    52  	enableANSI.Do(func() {
    53  		stdout := windows.Handle(os.Stdout.Fd())
    54  		var originalMode uint32
    55  
    56  		windows.GetConsoleMode(stdout, &originalMode)
    57  		windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
    58  	})
    59  }