Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
- macos-latest
- windows-latest
go:
- '1.19'
- '1.18'
- '1.17'
- '1.16'
- '1.15'
Expand Down
4 changes: 2 additions & 2 deletions colorable_appengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
)

// NewColorable returns new instance of Writer which handles escape sequence.
func NewColorable(file *os.File) io.Writer {
func NewColorable(file io.Writer) io.Writer {
if file == nil {
panic("nil passed instead of *os.File to NewColorable()")
panic("nil passed instead of io.Writer to NewColorable()")
}

return file
Expand Down
4 changes: 2 additions & 2 deletions colorable_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
)

// NewColorable returns new instance of Writer which handles escape sequence.
func NewColorable(file *os.File) io.Writer {
func NewColorable(file io.Writer) io.Writer {
if file == nil {
panic("nil passed instead of *os.File to NewColorable()")
panic("nil passed instead of io.Writer to NewColorable()")
}

return file
Expand Down
15 changes: 10 additions & 5 deletions colorable_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,19 @@ type Writer struct {
}

// NewColorable returns new instance of Writer which handles escape sequence from File.
func NewColorable(file *os.File) io.Writer {
func NewColorable(file io.Writer) io.Writer {
if file == nil {
panic("nil passed instead of *os.File to NewColorable()")
panic("nil passed instead of io.Writer to NewColorable()")
}

if isatty.IsTerminal(file.Fd()) {
if f, ok := file.(filelike); isatty.IsWriterTerminal(file) && ok {
fd := f.Fd()
var mode uint32
if r, _, _ := procGetConsoleMode.Call(file.Fd(), uintptr(unsafe.Pointer(&mode))); r != 0 && mode&cENABLE_VIRTUAL_TERMINAL_PROCESSING != 0 {
if r, _, _ := procGetConsoleMode.Call(fd, uintptr(unsafe.Pointer(&mode))); r != 0 && mode&cENABLE_VIRTUAL_TERMINAL_PROCESSING != 0 {
return file
}
var csbi consoleScreenBufferInfo
handle := syscall.Handle(file.Fd())
handle := syscall.Handle(fd)
procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
return &Writer{out: file, handle: handle, oldattr: csbi.attributes, oldpos: coord{0, 0}}
}
Expand All @@ -127,6 +128,10 @@ func NewColorableStderr() io.Writer {
return NewColorable(os.Stderr)
}

type filelike interface {
Fd() uintptr
}

var color256 = map[int]int{
0: 0x000000,
1: 0x800000,
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ module github.com/mattn/go-colorable
require github.com/mattn/go-isatty v0.0.16

go 1.15

// FIXME: remove, after merge of https://github.com/mattn/go-isatty/pull/81
replace github.com/mattn/go-isatty => github.com/cardil/mattn-isatty v0.0.0-20230228121058-48002435730c
Comment on lines +7 to +8
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this before merge

4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/cardil/mattn-isatty v0.0.0-20230228121058-48002435730c h1:vitLC/i2BOWniuLac5nzO8B4idybJbTDejvh2N0n4nE=
github.com/cardil/mattn-isatty v0.0.0-20230228121058-48002435730c/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=