Skip to content

Commit

Permalink
Merge pull request #46 from mattn/enablecolor
Browse files Browse the repository at this point in the history
Add EnableColorsStdout
  • Loading branch information
mattn committed Feb 28, 2020
2 parents d58279a + 68e95eb commit 4e32bdb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions colorable_appengine.go
Expand Up @@ -27,3 +27,11 @@ func NewColorableStdout() io.Writer {
func NewColorableStderr() io.Writer {
return os.Stderr
}

// EnableColorsStdout enable colors if possible.
func EnableColorsStdout(enabled *bool) func() {
if enabled != nil {
*enabled = true
}
return func() {}
}
8 changes: 8 additions & 0 deletions colorable_others.go
Expand Up @@ -28,3 +28,11 @@ func NewColorableStdout() io.Writer {
func NewColorableStderr() io.Writer {
return os.Stderr
}

// EnableColorsStdout enable colors if possible.
func EnableColorsStdout(enabled *bool) func() {
if enabled != nil {
*enabled = true
}
return func() {}
}
21 changes: 21 additions & 0 deletions colorable_windows.go
Expand Up @@ -81,6 +81,7 @@ var (
procSetConsoleCursorInfo = kernel32.NewProc("SetConsoleCursorInfo")
procSetConsoleTitle = kernel32.NewProc("SetConsoleTitleW")
procGetConsoleMode = kernel32.NewProc("GetConsoleMode")
procSetConsoleMode = kernel32.NewProc("SetConsoleMode")
procCreateConsoleScreenBuffer = kernel32.NewProc("CreateConsoleScreenBuffer")
)

Expand Down Expand Up @@ -1010,3 +1011,23 @@ func n256setup() {
n256backAttr[i] = c.backgroundAttr()
}
}

// EnableColorsStdout enable colors if possible.
func EnableColorsStdout(enabled *bool) func() {
var mode uint32
h := os.Stdout.Fd()
if r, _, _ := procGetConsoleMode.Call(h, uintptr(unsafe.Pointer(&mode))); r != 0 {
if r, _, _ = procSetConsoleMode.Call(h, uintptr(mode|cENABLE_VIRTUAL_TERMINAL_PROCESSING)); r != 0 {
if enabled != nil {
*enabled = true
}
return func() {
procSetConsoleMode.Call(h, uintptr(mode))
}
}
}
if enabled != nil {
*enabled = true
}
return func() {}
}

0 comments on commit 4e32bdb

Please sign in to comment.