Skip to content

Commit 8354fad

Browse files
committed
Initial commit.
0 parents  commit 8354fad

File tree

16 files changed

+1354
-0
lines changed

16 files changed

+1354
-0
lines changed

.github/workflows/ci.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Build
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
tags:
7+
- "*"
8+
pull_request:
9+
branches: [ master ]
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v2
16+
- name: Set up Go
17+
uses: actions/setup-go@v2
18+
with:
19+
go-version: 1.16
20+
- name: golangci-lint
21+
uses: golangci/golangci-lint-action@v2
22+
with:
23+
version: v1.29
24+
- name: golint
25+
uses: Jerome1337/[email protected]
26+
- name: Test
27+
run: go test ./...

LICENSE

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
MIT License
2+
3+
Copyright (c) Jason Walton <[email protected]> (https://www.thedreaming.org)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10+
11+
12+
13+
This software was ported from https://github.com/chalk/supports-color/, which has the following license:
14+
15+
MIT License
16+
17+
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)
18+
19+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
20+
21+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
22+
23+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# supports-color
2+
3+
Go library to detect whether a terminal supports color, and enables ANSI color support in recent Windows 10 builds.
4+
5+
This is a port of the Node.js package [supports-color](https://github.com/chalk/supports-color) v8.1.1 by [Sindre Sorhus](https://github.com/sindresorhus) and [Josh Junon](https://github.com/qix-).
6+
7+
## Install
8+
9+
```sh
10+
$ go get github.com/jwalton/go-supportscolor
11+
```
12+
13+
## Usage
14+
15+
```go
16+
import (
17+
"fmt"
18+
"github.com/jwalton/go-supportscolor"
19+
)
20+
21+
if supportscolor.Stdout().SupportsColor {
22+
fmt.Println("Terminal stdout supports color")
23+
}
24+
25+
if supportscolor.Stdout().Has256 {
26+
fmt.Println("Terminal stdout supports 256 colors")
27+
}
28+
29+
if supportscolor.Stderr().Has16m {
30+
fmt.Println("Terminal stderr supports 16 million colors (true color)")
31+
}
32+
```
33+
34+
## Windows 10 Support
35+
36+
`supportscolor` is cross-platform, and will work on Linux and MacOS systems, but will also work on Windows 10.
37+
38+
Many ANSI color libraries for Go do a poor job of handling colors in Windows. This is because historically, Windows has not supported ANSI color codes, so hacks like [ansicon](https://github.com/adoxa/ansicon) or [go-colorable](https://github.com/mattn/go-colorable) were required. However, Windows 10 has supported ANSI escape codes since 2017 (build 10586 for 256 color support, and build 14931 for 16.7 million true color support). In [Windows Terminal](https://github.com/Microsoft/Terminal) this is enabled by default, but in `CMD.EXE` or PowerShell, ANSI support must be enabled via [`ENABLE_VIRTUAL_TERMINAL_PROCESSING`](https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences).
39+
40+
This library takes care of all of this for you, though - if you call `supportscolor.Stdout()` on a modern build of Windows 10, it will set the `ENABLE_VIRTUAL_TERMINAL_PROCESSING` console mode automatically if required, and return the correct color level, and then you can just write ANSI escape codes to stdout and not worry about it. If someone uses your app on an old version of Windows, this will return `SupportsColor == false`, and you can write black and white to stdout.
41+
42+
## API
43+
44+
Returns a `supportscolor.Support` with a `Stdout()` and `Stderr()` function for testing either stream. (There's one for stdout and one for stderr, because if you run `mycmd > foo.txt` then stdout would be redirected to a file, and since it would not be a TTY would not have color support, while stderr would still be going to the console and would have color support.)
45+
46+
The `Stdout()`/`Stderr()` objects specify a level of support for color through a `.Level` property and a corresponding flag:
47+
48+
- `.Level = None` and `.SupportsColor = false`: No color support
49+
- `.Level = Basic` and `.SupportsColor = true`: Basic color support (16 colors)
50+
- `.Level = Ansi256` and `.Has256 = true`: 256 color support
51+
- `.Level = Ansi16m` and `.Has16m = true`: True color support (16 million colors)
52+
53+
### `supportscolor.SupportsColor(fd, ...options)`
54+
55+
Additionally, `supportscolor` exposes the `.SupportsColor()` function that takes an arbitrary file descriptor (e.g. `os.Stdout.Fd()`) and options and will (re-)evaluate color support for an arbitrary stream.
56+
57+
For example, `supportscolor.Stdout()` is the equivalent of `supportscolor.SupportsColor(os.Stdout.Fd())`.
58+
59+
Available options are:
60+
61+
- `supportscolor.IsTTYOption(isTTY bool)` - Force whether the given file should be considered a TTY or not. If this not specified, TTY status will be detected automatically via `term.IsTerminal()`.
62+
- `supportscolor.SniffFlagsOption(sniffFlags bool)` - By default it is `true`, which instructs `SupportsColor()` to sniff `os.Args` for the multitude of `--color` flags (see _Info_ below). If `false`, then `os.Args` is not considered when determining color support.
63+
64+
## Info
65+
66+
By default, supportscolor checks `os.Args` for the `--color` and `--no-color` CLI flags.
67+
68+
For situations where using `--color` is not possible, use the environment variable `FORCE_COLOR=1` (level 1 - 16 colors), `FORCE_COLOR=2` (level 2 - 256 colors), or `FORCE_COLOR=3` (level 3 - true color) to forcefully enable color, or `FORCE_COLOR=0` to forcefully disable. The use of `FORCE_COLOR` overrides all other color support checks.
69+
70+
Explicit 256/True color mode can be enabled using the `--color=256` and `--color=16m` flags, respectively.

colorlevel_string.go

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

colorlevels.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package supportscolor
2+
3+
//go:generate stringer -type=ColorLevel
4+
5+
// ColorLevel represents the ANSI color level supported by the terminal.
6+
type ColorLevel int
7+
8+
const (
9+
// None represents a terminal that does not support color at all.
10+
None ColorLevel = 0
11+
// Basic represents a terminal with basic 16 color support.
12+
Basic ColorLevel = 1
13+
// Ansi256 represents a terminal with 256 color support.
14+
Ansi256 ColorLevel = 2
15+
// Ansi16m represents a terminal with full true color support.
16+
Ansi16m ColorLevel = 3
17+
)

di.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package supportscolor
2+
3+
import (
4+
"os"
5+
"runtime"
6+
7+
hasflag "github.com/jwalton/go-supportscolor/pkg/hasFlag"
8+
"golang.org/x/term"
9+
)
10+
11+
type environment interface {
12+
LookupEnv(name string) (string, bool)
13+
Getenv(name string) string
14+
HasFlag(name string) bool
15+
IsTerminal(fd int) bool
16+
getWindowsVersion() (majorVersion, minorVersion, buildNumber uint32)
17+
osEnableColor() bool
18+
getGOOS() string
19+
}
20+
21+
type defaultEnvironmentType struct{}
22+
23+
func (*defaultEnvironmentType) LookupEnv(name string) (string, bool) {
24+
return os.LookupEnv(name)
25+
}
26+
27+
func (*defaultEnvironmentType) Getenv(name string) string {
28+
return os.Getenv(name)
29+
}
30+
31+
func (*defaultEnvironmentType) HasFlag(flag string) bool {
32+
return hasflag.HasFlag(flag)
33+
}
34+
35+
func (*defaultEnvironmentType) IsTerminal(fd int) bool {
36+
// TODO: Replace with github.com/mattn/go-isatty?
37+
return term.IsTerminal(int(fd))
38+
}
39+
40+
func (*defaultEnvironmentType) getWindowsVersion() (majorVersion, minorVersion, buildNumber uint32) {
41+
return getWindowsVersion()
42+
}
43+
44+
func (*defaultEnvironmentType) osEnableColor() bool {
45+
return enableColor()
46+
}
47+
48+
func (*defaultEnvironmentType) getGOOS() string {
49+
return runtime.GOOS
50+
}
51+
52+
var defaultEnvironment = defaultEnvironmentType{}

example_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package supportscolor_test
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/jwalton/go-supportscolor"
7+
)
8+
9+
func ExampleSupportsColor() {
10+
if supportscolor.Stdout().SupportsColor {
11+
fmt.Println("\u001b[31mThis is Red!\u001b[39m")
12+
} else {
13+
fmt.Println("This is not red.")
14+
}
15+
16+
// Output: This is not red.
17+
}

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/jwalton/go-supportscolor
2+
3+
go 1.15
4+
5+
require (
6+
golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43
7+
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d
8+
)

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2+
golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43 h1:SgQ6LNaYJU0JIuEHv9+s6EbhSCwYeAf5Yvj6lpYlqAE=
3+
golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4+
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=
5+
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=

osutils.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// +build !windows
2+
3+
package supportscolor
4+
5+
func getWindowsVersion() (majorVersion, minorVersion, buildNumber uint32) {
6+
return 0, 0, 0
7+
}
8+
9+
// enableColor will enable color in the terminal. Returns true if color was
10+
// enabled, false otherwise.
11+
func enableColor() bool {
12+
return true
13+
}

0 commit comments

Comments
 (0)