summaryrefslogtreecommitdiff
path: root/vendor/github.com/juju/ansiterm/tabwriter.go
blob: 1ff6faaaf242ae36cbca3c1c7a9562386181f173 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package ansiterm

import (
	"io"

	"github.com/juju/ansiterm/tabwriter"
)

// NewTabWriter returns a writer that is able to set colors and styels.
// The ansi escape codes are stripped for width calculations.
func NewTabWriter(output io.Writer, minwidth, tabwidth, padding int, padchar byte, flags uint) *TabWriter {
	return new(TabWriter).Init(output, minwidth, tabwidth, padding, padchar, flags)
}

// TabWriter is a filter that inserts padding around tab-delimited
// columns in its input to align them in the output.
//
// It also setting of colors and styles over and above the standard
// tabwriter package.
type TabWriter struct {
	Writer
	tw tabwriter.Writer
}

// Flush should be called after the last call to Write to ensure
// that any data buffered in the Writer is written to output. Any
// incomplete escape sequence at the end is considered
// complete for formatting purposes.
//
func (t *TabWriter) Flush() error {
	return t.tw.Flush()
}

// SetColumnAlignRight will mark a particular column as align right.
// This is reset on the next flush.
func (t *TabWriter) SetColumnAlignRight(column int) {
	t.tw.SetColumnAlignRight(column)
}

// A Writer must be initialized with a call to Init. The first parameter (output)
// specifies the filter output. The remaining parameters control the formatting:
//
//	minwidth	minimal cell width including any padding
//	tabwidth	width of tab characters (equivalent number of spaces)
//	padding		padding added to a cell before computing its width
//	padchar		ASCII char used for padding
//			if padchar == '\t', the Writer will assume that the
//			width of a '\t' in the formatted output is tabwidth,
//			and cells are left-aligned independent of align_left
//			(for correct-looking results, tabwidth must correspond
//			to the tab width in the viewer displaying the result)
//	flags		formatting control
//
func (t *TabWriter) Init(output io.Writer, minwidth, tabwidth, padding int, padchar byte, flags uint) *TabWriter {
	writer, colorCapable := colorEnabledWriter(output)
	t.Writer = Writer{
		Writer:  t.tw.Init(writer, minwidth, tabwidth, padding, padchar, flags),
		noColor: !colorCapable,
	}
	return t
}