2015-04-16 17:01:37 -04:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package windows
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2015-09-03 16:01:56 -04:00
|
|
|
ansiterm "github.com/Azure/go-ansiterm"
|
|
|
|
"github.com/Azure/go-ansiterm/winterm"
|
2015-04-16 17:01:37 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ansiWriter wraps a standard output file (e.g., os.Stdout) providing ANSI sequence translation.
|
|
|
|
type ansiWriter struct {
|
|
|
|
file *os.File
|
|
|
|
fd uintptr
|
2015-09-03 16:01:56 -04:00
|
|
|
infoReset *winterm.CONSOLE_SCREEN_BUFFER_INFO
|
2015-04-16 17:01:37 -04:00
|
|
|
command []byte
|
|
|
|
escapeSequence []byte
|
|
|
|
inAnsiSequence bool
|
2015-09-03 16:01:56 -04:00
|
|
|
parser *ansiterm.AnsiParser
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func newAnsiWriter(nFile int) *ansiWriter {
|
2016-05-24 16:03:13 -04:00
|
|
|
initLogger()
|
2015-09-03 16:01:56 -04:00
|
|
|
file, fd := winterm.GetStdFile(nFile)
|
|
|
|
info, err := winterm.GetConsoleScreenBufferInfo(fd)
|
2015-04-16 17:01:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-03 16:01:56 -04:00
|
|
|
parser := ansiterm.CreateParser("Ground", winterm.CreateWinEventHandler(fd, file))
|
2015-04-16 17:01:37 -04:00
|
|
|
logger.Infof("newAnsiWriter: parser %p", parser)
|
|
|
|
|
|
|
|
aw := &ansiWriter{
|
|
|
|
file: file,
|
|
|
|
fd: fd,
|
|
|
|
infoReset: info,
|
2015-09-03 16:01:56 -04:00
|
|
|
command: make([]byte, 0, ansiterm.ANSI_MAX_CMD_LENGTH),
|
|
|
|
escapeSequence: []byte(ansiterm.KEY_ESC_CSI),
|
2015-04-16 17:01:37 -04:00
|
|
|
parser: parser,
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Infof("newAnsiWriter: aw.parser %p", aw.parser)
|
|
|
|
logger.Infof("newAnsiWriter: %v", aw)
|
|
|
|
return aw
|
|
|
|
}
|
|
|
|
|
|
|
|
func (aw *ansiWriter) Fd() uintptr {
|
|
|
|
return aw.fd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes len(p) bytes from p to the underlying data stream.
|
|
|
|
func (aw *ansiWriter) Write(p []byte) (total int, err error) {
|
|
|
|
if len(p) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Infof("Write: % x", p)
|
|
|
|
logger.Infof("Write: %s", string(p))
|
|
|
|
return aw.parser.Parse(p)
|
|
|
|
}
|