...

Source file src/github.com/moby/term/windows/ansi_writer.go

Documentation: github.com/moby/term/windows

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

View as plain text