This commit is part of work to make gh more scriptable. It includes both some general purpose helpers towards this goal as well as improvements to the issue commands. Other commands will follow. - Adds `utils/terminal.go` for finding out about gh's execution environment - introduces `stubTerminal` for either faking being attached to a tty or not during tests - updates issue commands to behave better when not attached to a tty: - issue list doesn't print fuzzy dates - issue list doesn't print header - issue list prints state explicitly - issue create no longer hangs - issue create fails with clear error unless both -t and -b are specified - issue view prints raw issue body - issue view prints metadata in a consistent, linewise format
49 lines
982 B
Go
49 lines
982 B
Go
package utils
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/mattn/go-colorable"
|
|
"github.com/mgutz/ansi"
|
|
)
|
|
|
|
var (
|
|
_isStdoutTerminal, checkedTerminal bool
|
|
|
|
// Outputs ANSI color if stdout is a tty
|
|
Magenta = makeColorFunc("magenta")
|
|
Cyan = makeColorFunc("cyan")
|
|
Red = makeColorFunc("red")
|
|
Yellow = makeColorFunc("yellow")
|
|
Blue = makeColorFunc("blue")
|
|
Green = makeColorFunc("green")
|
|
Gray = makeColorFunc("black+h")
|
|
Bold = makeColorFunc("default+b")
|
|
)
|
|
|
|
// NewColorable returns an output stream that handles ANSI color sequences on Windows
|
|
func NewColorable(w io.Writer) io.Writer {
|
|
if f, isFile := w.(*os.File); isFile {
|
|
return colorable.NewColorable(f)
|
|
}
|
|
return w
|
|
}
|
|
|
|
func makeColorFunc(color string) func(string) string {
|
|
cf := ansi.ColorFunc(color)
|
|
return func(arg string) string {
|
|
if isColorEnabled() {
|
|
return cf(arg)
|
|
}
|
|
return arg
|
|
}
|
|
}
|
|
|
|
func isColorEnabled() bool {
|
|
if os.Getenv("NO_COLOR") != "" {
|
|
return false
|
|
}
|
|
|
|
return isStdoutTerminal()
|
|
}
|