فهرست منبع

Merge pull request #46 from mattn/enablecolor

Add EnableColorsStdout
mattn 5 سال پیش
والد
کامیت
4e32bdb9fe
3فایلهای تغییر یافته به همراه37 افزوده شده و 0 حذف شده
  1. 8 0
      colorable_appengine.go
  2. 8 0
      colorable_others.go
  3. 21 0
      colorable_windows.go

+ 8 - 0
colorable_appengine.go

@@ -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 - 0
colorable_others.go

@@ -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 - 0
colorable_windows.go

@@ -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")
 )
 
@@ -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() {}
+}