Skip to content

Commit

Permalink
Fix color support for Windows 10 (stripe#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe authored Aug 14, 2019
1 parent 6ae903a commit bdce130
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ require (
github.com/x-cray/logrus-prefixed-formatter v0.5.2
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e // indirect
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e
golang.org/x/text v0.3.2 // indirect
)
12 changes: 11 additions & 1 deletion pkg/ansi/ansi.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"runtime"
"time"

"github.com/briandowns/spinner"
Expand Down Expand Up @@ -84,7 +85,16 @@ func StartSpinner(msg string, w io.Writer) *spinner.Spinner {
return nil
}

s := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
// See https://github.com/briandowns/spinner#available-character-sets for
// list of available charsets
charSetIdx := 11
if runtime.GOOS == "windows" {
// Less fancy, but uses ASCII characters so works with Windows default
// console.
charSetIdx = 8
}

s := spinner.New(spinner.CharSets[charSetIdx], 100*time.Millisecond)
s.Writer = w

if msg != "" {
Expand Down
23 changes: 23 additions & 0 deletions pkg/ansi/init_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// +build windows

package ansi

import (
"os"

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

// enableAnsiColors enables support for ANSI color sequences in Windows
// default console. Note that this only works with Windows 10.
func enableAnsiColors() {
stdout := windows.Handle(os.Stdout.Fd())
var originalMode uint32

windows.GetConsoleMode(stdout, &originalMode)
windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
}

func init() {
enableAnsiColors()
}

0 comments on commit bdce130

Please sign in to comment.