-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve IsColorTerminal windows support
- Loading branch information
Showing
5 changed files
with
103 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//go:build !windows | ||
|
||
package jsoncolor | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"golang.org/x/term" | ||
) | ||
|
||
// IsColorTerminal returns true if w is a colorable terminal. | ||
// It respects [NO_COLOR], [FORCE_COLOR] and TERM=dumb environment variables. | ||
// | ||
// [NO_COLOR]: https://no-color.org/ | ||
// [FORCE_COLOR]: https://force-color.org/ | ||
func IsColorTerminal(w io.Writer) bool { | ||
if os.Getenv("NO_COLOR") != "" { | ||
return false | ||
} | ||
if os.Getenv("FORCE_COLOR") != "" { | ||
return true | ||
} | ||
if os.Getenv("TERM") == "dumb" { | ||
return false | ||
} | ||
|
||
if w == nil { | ||
return false | ||
} | ||
|
||
f, ok := w.(*os.File) | ||
if !ok { | ||
return false | ||
} | ||
|
||
if !term.IsTerminal(int(f.Fd())) { | ||
return false | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package jsoncolor | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
// IsColorTerminal returns true if w is a colorable terminal. | ||
// It respects [NO_COLOR], [FORCE_COLOR] and TERM=dumb environment variables. | ||
// | ||
// [NO_COLOR]: https://no-color.org/ | ||
// [FORCE_COLOR]: https://force-color.org/ | ||
func IsColorTerminal(w io.Writer) bool { | ||
if os.Getenv("NO_COLOR") != "" { | ||
return false | ||
} | ||
if os.Getenv("FORCE_COLOR") != "" { | ||
return true | ||
} | ||
if os.Getenv("TERM") == "dumb" { | ||
return false | ||
} | ||
|
||
if w == nil { | ||
return false | ||
} | ||
|
||
f, ok := w.(*os.File) | ||
if !ok { | ||
return false | ||
} | ||
fd := f.Fd() | ||
|
||
console := windows.Handle(fd) | ||
var mode uint32 | ||
if err := windows.GetConsoleMode(console, &mode); err != nil { | ||
return false | ||
} | ||
|
||
var want uint32 = windows.ENABLE_PROCESSED_OUTPUT | windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING | ||
if (mode & want) == want { | ||
return true | ||
} | ||
|
||
mode |= want | ||
if err := windows.SetConsoleMode(console, mode); err != nil { | ||
return false | ||
} | ||
|
||
return true | ||
} |