Documentation
¶
Overview ¶
Package output is the bounded output buffer the tools and code mode share: an Accumulator keeps the head, the tail or both ends of a long stream within a byte budget, and may spill the whole of it to a file.
It is a package of its own, importing only the standard library, so that a program can bound a script's output without linking the source tools and the tree-sitter grammars that come with them. Package tools keeps its own names for these types as aliases.
Index ¶
- Constants
- type Accumulator
- func (a *Accumulator) Close() error
- func (a *Accumulator) FileError() error
- func (a *Accumulator) SpillPath() string
- func (a *Accumulator) String() string
- func (a *Accumulator) Total() int64
- func (a *Accumulator) Truncated() bool
- func (a *Accumulator) UseFile(w io.WriteCloser, path string)
- func (a *Accumulator) Write(p []byte) (int, error)
- type TruncateMode
- type TruncatedBy
Constants ¶
const DefaultByteLimit = 50 * 1024
DefaultByteLimit is the budget NewAccumulator uses when it is given none: 50 KB.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Accumulator ¶
type Accumulator struct {
// Cap is the byte budget for the retained window.
Cap int
// Mode selects which end survives.
Mode TruncateMode
// SpillDir, when non-empty, enables the spill file.
SpillDir string
// SpillPrefix names the spill file.
SpillPrefix string
// contains filtered or unexported fields
}
Accumulator is a bounded rolling accumulator for subprocess output.
It retains a bounded HEAD and a bounded TAIL, discarding the middle as bytes arrive, so peak memory is roughly 2×cap regardless of how much a subprocess writes. The obvious alternative — buffer everything, truncate at the end — is quadratic in output size and out-of-memories on precisely the runaway command the cap exists to contain.
It also lazily spills the COMPLETE output to a file on first overflow, so the full text stays available to the caller and the audit trail while only the bounded window reaches the model.
func NewAccumulator ¶
func NewAccumulator(capBytes int, mode TruncateMode) *Accumulator
NewAccumulator returns an accumulator with the given budget and mode. A budget of zero or less means DefaultByteLimit.
func (*Accumulator) Close ¶
func (a *Accumulator) Close() error
Close releases the spill file, or the destination UseFile installed.
func (*Accumulator) FileError ¶
func (a *Accumulator) FileError() error
FileError returns the first error of writing the complete output, or nil: a spill directory or file that could not be created, or a failed write to the spill file or to the destination UseFile installed. After it, nothing more is written there, and the retained window is unaffected.
func (*Accumulator) SpillPath ¶
func (a *Accumulator) SpillPath() string
SpillPath returns the spill file path, or "" if nothing spilled. When a destination was installed via UseFile, the path is always returned (even for empty output).
func (*Accumulator) String ¶
func (a *Accumulator) String() string
String renders the retained window, with a marker naming what was elided.
A truncation the model cannot act on costs a turn. Every marker therefore names the number of bytes elided and, where one exists, the spill path — which is reachable through `execute`, because `execute` is not path-contained.
func (*Accumulator) Total ¶
func (a *Accumulator) Total() int64
Total reports every byte written, including discarded ones.
func (*Accumulator) Truncated ¶
func (a *Accumulator) Truncated() bool
Truncated reports whether anything was discarded.
func (*Accumulator) UseFile ¶
func (a *Accumulator) UseFile(w io.WriteCloser, path string)
UseFile installs an already opened destination for the complete output, in place of a spill file: every byte written goes to w, SpillPath reports path from the start, including for empty output, and no temporary file is created. Close closes w. A caller that names its own log file uses it.
type TruncateMode ¶
type TruncateMode int
TruncateMode selects which end of the stream survives truncation.
const ( // TruncateHead keeps the BEGINNING. Right for a file read or a listing, // where the interesting part is at the top. TruncateHead TruncateMode = iota // TruncateTail keeps the END. Right for `execute`: a failing build puts // its error at the end of the log, and head-truncation preserves the // banner and discards the failure. TruncateTail // TruncateMiddle keeps both ends and elides the centre. TruncateMiddle )
type TruncatedBy ¶
type TruncatedBy string
TruncatedBy names which limit cut a result short. It is a closed enum, so a consumer switching on it cannot miss a case.
const ( TruncatedByLines TruncatedBy = "lines" TruncatedByBytes TruncatedBy = "bytes" )