github.com/endophage/docker@v1.4.2-0.20161027011718-242853499895/pkg/term/windows/ansi_writer.go (about)

     1  // +build windows
     2  
     3  package windows
     4  
     5  import (
     6  	"io"
     7  	"os"
     8  
     9  	ansiterm "github.com/Azure/go-ansiterm"
    10  	"github.com/Azure/go-ansiterm/winterm"
    11  )
    12  
    13  // ansiWriter wraps a standard output file (e.g., os.Stdout) providing ANSI sequence translation.
    14  type ansiWriter struct {
    15  	file           *os.File
    16  	fd             uintptr
    17  	infoReset      *winterm.CONSOLE_SCREEN_BUFFER_INFO
    18  	command        []byte
    19  	escapeSequence []byte
    20  	inAnsiSequence bool
    21  	parser         *ansiterm.AnsiParser
    22  }
    23  
    24  // NewAnsiWriter returns an io.Writer that provides VT100 terminal emulation on top of a
    25  // Windows console output handle.
    26  func NewAnsiWriter(nFile int) io.Writer {
    27  	initLogger()
    28  	file, fd := winterm.GetStdFile(nFile)
    29  	info, err := winterm.GetConsoleScreenBufferInfo(fd)
    30  	if err != nil {
    31  		return nil
    32  	}
    33  
    34  	parser := ansiterm.CreateParser("Ground", winterm.CreateWinEventHandler(fd, file))
    35  	logger.Infof("newAnsiWriter: parser %p", parser)
    36  
    37  	aw := &ansiWriter{
    38  		file:           file,
    39  		fd:             fd,
    40  		infoReset:      info,
    41  		command:        make([]byte, 0, ansiterm.ANSI_MAX_CMD_LENGTH),
    42  		escapeSequence: []byte(ansiterm.KEY_ESC_CSI),
    43  		parser:         parser,
    44  	}
    45  
    46  	logger.Infof("newAnsiWriter: aw.parser %p", aw.parser)
    47  	logger.Infof("newAnsiWriter: %v", aw)
    48  	return aw
    49  }
    50  
    51  func (aw *ansiWriter) Fd() uintptr {
    52  	return aw.fd
    53  }
    54  
    55  // Write writes len(p) bytes from p to the underlying data stream.
    56  func (aw *ansiWriter) Write(p []byte) (total int, err error) {
    57  	if len(p) == 0 {
    58  		return 0, nil
    59  	}
    60  
    61  	logger.Infof("Write: % x", p)
    62  	logger.Infof("Write: %s", string(p))
    63  	return aw.parser.Parse(p)
    64  }