terminal: Use go-colorable

Add fallback to display colors on Windows.
This commit is contained in:
Yasuhiro Matsumoto
2016-03-24 19:55:48 +09:00
committed by Derek Parker
parent af4798e2a9
commit f6836cbcf1
51 changed files with 1073 additions and 4251 deletions

View File

@ -28,6 +28,7 @@ type Term struct {
line *liner.State
cmds *Commands
dumb bool
stdout io.Writer
InitFile string
}
@ -37,12 +38,23 @@ func New(client service.Client, conf *config.Config) *Term {
if conf != nil && conf.Aliases != nil {
cmds.Merge(conf.Aliases)
}
var w io.Writer
dumb := strings.ToLower(os.Getenv("TERM")) == "dumb"
if dumb {
w = os.Stdout
} else {
w = getColorableWriter()
}
return &Term{
prompt: "(dlv) ",
line: liner.NewLiner(),
client: client,
cmds: cmds,
dumb: !supportsEscapeCodes(),
dumb: dumb,
stdout: w,
}
}
@ -134,7 +146,7 @@ func (t *Term) Println(prefix, str string) {
if !t.dumb {
prefix = fmt.Sprintf("%s%s%s", terminalBlueEscapeCode, prefix, terminalResetEscapeCode)
}
fmt.Printf("%s%s\n", prefix, str)
fmt.Fprintf(t.stdout, "%s%s\n", prefix, str)
}
func (t *Term) promptForInput() (string, error) {

View File

@ -3,11 +3,12 @@
package terminal
import (
"io"
"os"
"strings"
)
// supportsEscapeCodes returns true if console handles escape codes.
func supportsEscapeCodes() bool {
return strings.ToLower(os.Getenv("TERM")) != "dumb"
// getColorableWriter returns two values. First is Writer supported colors.
// If return nil, colors will be disabled.
func getColorableWriter() io.Writer {
return os.Stdout
}

View File

@ -1,31 +1,35 @@
package terminal
import (
"io"
"os"
"strings"
"syscall"
"github.com/mattn/go-colorable"
)
// supportsEscapeCodes returns true if console handles escape codes.
func supportsEscapeCodes() bool {
if strings.ToLower(os.Getenv("TERM")) == "dumb" {
return false
}
// getColorableWriter returns two values. First is Writer supported colors.
// If return nil, colors will be disabled.
func getColorableWriter() io.Writer {
if strings.ToLower(os.Getenv("ConEmuANSI")) == "on" {
// The ConEmu terminal is installed. Use it.
return true
return os.Stdout
}
const ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
h, err := syscall.GetStdHandle(syscall.STD_OUTPUT_HANDLE)
if err != nil {
return false
return os.Stdout
}
var m uint32
err = syscall.GetConsoleMode(h, &m)
if err != nil {
return false
return os.Stdout
}
return m&ENABLE_VIRTUAL_TERMINAL_PROCESSING != 0
if m&ENABLE_VIRTUAL_TERMINAL_PROCESSING != 0 {
return os.Stdout
}
return colorable.NewColorableStdout()
}