Skip to content

Commit

Permalink
Don't enable ANSI in cmd.exe until absolutely necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Apr 6, 2021
1 parent fedd7a4 commit 874349b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions ansi_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

package lipgloss

// enableANSIColors is only needed on Windows.
func enableANSIColors() {}
// enableLegacyWindowsANSI is only needed on Windows.
func enableLegacyWindowsANSI() {}
15 changes: 10 additions & 5 deletions ansi_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ package lipgloss

import (
"os"
"sync"

"golang.org/x/sys/windows"
)

var enableANSI sync.Once

// enableANSIColors enables support for ANSI color sequences in the Windows
// default console (cmd.exe and the PowerShell application). Note that this
// only works with Windows 10. Also note that Windows Terminal supports colors
// by default.
func enableANSIColors() {
stdout := windows.Handle(os.Stdout.Fd())
var originalMode uint32
func enableLegacyWindowsANSI() {
enableANSI.Do(func() {
stdout := windows.Handle(os.Stdout.Fd())
var originalMode uint32

windows.GetConsoleMode(stdout, &originalMode)
windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
windows.GetConsoleMode(stdout, &originalMode)
windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
})
}
1 change: 0 additions & 1 deletion color.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var (
// actual check only once.
func ColorProfile() termenv.Profile {
getColorProfile.Do(func() {
enableANSIColors()
colorProfile = termenv.ColorProfile()
})
return colorProfile
Expand Down
4 changes: 4 additions & 0 deletions style.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ func (s Style) Render(str string) string {
useSpaceStyler = underlineSpaces || strikethroughSpaces
)

// Enable support for ANSI on the legacy Windows cmd.exe console. This is a
// no-op on non-Windows systems and on Windows runs only once.
enableLegacyWindowsANSI()

if bold {
te = te.Bold()
}
Expand Down

0 comments on commit 874349b

Please sign in to comment.