Skip to content

Commit 4e32bdb

Browse files
authored
Merge pull request #46 from mattn/enablecolor
Add EnableColorsStdout
2 parents d58279a + 68e95eb commit 4e32bdb

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

colorable_appengine.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@ func NewColorableStdout() io.Writer {
2727
func NewColorableStderr() io.Writer {
2828
return os.Stderr
2929
}
30+
31+
// EnableColorsStdout enable colors if possible.
32+
func EnableColorsStdout(enabled *bool) func() {
33+
if enabled != nil {
34+
*enabled = true
35+
}
36+
return func() {}
37+
}

colorable_others.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,11 @@ func NewColorableStdout() io.Writer {
2828
func NewColorableStderr() io.Writer {
2929
return os.Stderr
3030
}
31+
32+
// EnableColorsStdout enable colors if possible.
33+
func EnableColorsStdout(enabled *bool) func() {
34+
if enabled != nil {
35+
*enabled = true
36+
}
37+
return func() {}
38+
}

colorable_windows.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ var (
8181
procSetConsoleCursorInfo = kernel32.NewProc("SetConsoleCursorInfo")
8282
procSetConsoleTitle = kernel32.NewProc("SetConsoleTitleW")
8383
procGetConsoleMode = kernel32.NewProc("GetConsoleMode")
84+
procSetConsoleMode = kernel32.NewProc("SetConsoleMode")
8485
procCreateConsoleScreenBuffer = kernel32.NewProc("CreateConsoleScreenBuffer")
8586
)
8687

@@ -1010,3 +1011,23 @@ func n256setup() {
10101011
n256backAttr[i] = c.backgroundAttr()
10111012
}
10121013
}
1014+
1015+
// EnableColorsStdout enable colors if possible.
1016+
func EnableColorsStdout(enabled *bool) func() {
1017+
var mode uint32
1018+
h := os.Stdout.Fd()
1019+
if r, _, _ := procGetConsoleMode.Call(h, uintptr(unsafe.Pointer(&mode))); r != 0 {
1020+
if r, _, _ = procSetConsoleMode.Call(h, uintptr(mode|cENABLE_VIRTUAL_TERMINAL_PROCESSING)); r != 0 {
1021+
if enabled != nil {
1022+
*enabled = true
1023+
}
1024+
return func() {
1025+
procSetConsoleMode.Call(h, uintptr(mode))
1026+
}
1027+
}
1028+
}
1029+
if enabled != nil {
1030+
*enabled = true
1031+
}
1032+
return func() {}
1033+
}

0 commit comments

Comments
 (0)