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
44 lines
875 B
Go
44 lines
875 B
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/mattn/go-isatty"
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
)
|
|
|
|
func isStdoutTerminal() bool {
|
|
if !checkedTerminal {
|
|
_isStdoutTerminal = IsTerminal(os.Stdout)
|
|
checkedTerminal = true
|
|
}
|
|
return _isStdoutTerminal
|
|
}
|
|
|
|
// TODO I don't like this use of interface{} but we need to accept both io.Writer and io.Reader
|
|
// interfaces.
|
|
|
|
var IsTerminal = func(w interface{}) bool {
|
|
if f, isFile := w.(*os.File); isFile {
|
|
return isatty.IsTerminal(f.Fd()) || IsCygwinTerminal(f)
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func IsCygwinTerminal(w interface{}) bool {
|
|
if f, isFile := w.(*os.File); isFile {
|
|
return isatty.IsCygwinTerminal(f.Fd())
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
var TerminalSize = func(w interface{}) (int, int, error) {
|
|
if f, isFile := w.(*os.File); isFile {
|
|
return terminal.GetSize(int(f.Fd()))
|
|
}
|
|
|
|
return 0, 0, fmt.Errorf("%v is not a file", w)
|
|
}
|