| 1 |
747 |
jeremybenn |
// Copyright 2009 The Go Authors. All rights reserved.
|
| 2 |
|
|
// Use of this source code is governed by a BSD-style
|
| 3 |
|
|
// license that can be found in the LICENSE file.
|
| 4 |
|
|
|
| 5 |
|
|
// Package tabwriter implements a write filter (tabwriter.Writer) that
|
| 6 |
|
|
// translates tabbed columns in input into properly aligned text.
|
| 7 |
|
|
//
|
| 8 |
|
|
// The package is using the Elastic Tabstops algorithm described at
|
| 9 |
|
|
// http://nickgravgaard.com/elastictabstops/index.html.
|
| 10 |
|
|
//
|
| 11 |
|
|
package tabwriter
|
| 12 |
|
|
|
| 13 |
|
|
import (
|
| 14 |
|
|
"bytes"
|
| 15 |
|
|
"io"
|
| 16 |
|
|
"unicode/utf8"
|
| 17 |
|
|
)
|
| 18 |
|
|
|
| 19 |
|
|
// ----------------------------------------------------------------------------
|
| 20 |
|
|
// Filter implementation
|
| 21 |
|
|
|
| 22 |
|
|
// A cell represents a segment of text terminated by tabs or line breaks.
|
| 23 |
|
|
// The text itself is stored in a separate buffer; cell only describes the
|
| 24 |
|
|
// segment's size in bytes, its width in runes, and whether it's an htab
|
| 25 |
|
|
// ('\t') terminated cell.
|
| 26 |
|
|
//
|
| 27 |
|
|
type cell struct {
|
| 28 |
|
|
size int // cell size in bytes
|
| 29 |
|
|
width int // cell width in runes
|
| 30 |
|
|
htab bool // true if the cell is terminated by an htab ('\t')
|
| 31 |
|
|
}
|
| 32 |
|
|
|
| 33 |
|
|
// A Writer is a filter that inserts padding around tab-delimited
|
| 34 |
|
|
// columns in its input to align them in the output.
|
| 35 |
|
|
//
|
| 36 |
|
|
// The Writer treats incoming bytes as UTF-8 encoded text consisting
|
| 37 |
|
|
// of cells terminated by (horizontal or vertical) tabs or line
|
| 38 |
|
|
// breaks (newline or formfeed characters). Cells in adjacent lines
|
| 39 |
|
|
// constitute a column. The Writer inserts padding as needed to
|
| 40 |
|
|
// make all cells in a column have the same width, effectively
|
| 41 |
|
|
// aligning the columns. It assumes that all characters have the
|
| 42 |
|
|
// same width except for tabs for which a tabwidth must be specified.
|
| 43 |
|
|
// Note that cells are tab-terminated, not tab-separated: trailing
|
| 44 |
|
|
// non-tab text at the end of a line does not form a column cell.
|
| 45 |
|
|
//
|
| 46 |
|
|
// The Writer assumes that all Unicode code points have the same width;
|
| 47 |
|
|
// this may not be true in some fonts.
|
| 48 |
|
|
//
|
| 49 |
|
|
// If DiscardEmptyColumns is set, empty columns that are terminated
|
| 50 |
|
|
// entirely by vertical (or "soft") tabs are discarded. Columns
|
| 51 |
|
|
// terminated by horizontal (or "hard") tabs are not affected by
|
| 52 |
|
|
// this flag.
|
| 53 |
|
|
//
|
| 54 |
|
|
// If a Writer is configured to filter HTML, HTML tags and entities
|
| 55 |
|
|
// are passed through. The widths of tags and entities are
|
| 56 |
|
|
// assumed to be zero (tags) and one (entities) for formatting purposes.
|
| 57 |
|
|
//
|
| 58 |
|
|
// A segment of text may be escaped by bracketing it with Escape
|
| 59 |
|
|
// characters. The tabwriter passes escaped text segments through
|
| 60 |
|
|
// unchanged. In particular, it does not interpret any tabs or line
|
| 61 |
|
|
// breaks within the segment. If the StripEscape flag is set, the
|
| 62 |
|
|
// Escape characters are stripped from the output; otherwise they
|
| 63 |
|
|
// are passed through as well. For the purpose of formatting, the
|
| 64 |
|
|
// width of the escaped text is always computed excluding the Escape
|
| 65 |
|
|
// characters.
|
| 66 |
|
|
//
|
| 67 |
|
|
// The formfeed character ('\f') acts like a newline but it also
|
| 68 |
|
|
// terminates all columns in the current line (effectively calling
|
| 69 |
|
|
// Flush). Cells in the next line start new columns. Unless found
|
| 70 |
|
|
// inside an HTML tag or inside an escaped text segment, formfeed
|
| 71 |
|
|
// characters appear as newlines in the output.
|
| 72 |
|
|
//
|
| 73 |
|
|
// The Writer must buffer input internally, because proper spacing
|
| 74 |
|
|
// of one line may depend on the cells in future lines. Clients must
|
| 75 |
|
|
// call Flush when done calling Write.
|
| 76 |
|
|
//
|
| 77 |
|
|
type Writer struct {
|
| 78 |
|
|
// configuration
|
| 79 |
|
|
output io.Writer
|
| 80 |
|
|
minwidth int
|
| 81 |
|
|
tabwidth int
|
| 82 |
|
|
padding int
|
| 83 |
|
|
padbytes [8]byte
|
| 84 |
|
|
flags uint
|
| 85 |
|
|
|
| 86 |
|
|
// current state
|
| 87 |
|
|
buf bytes.Buffer // collected text excluding tabs or line breaks
|
| 88 |
|
|
pos int // buffer position up to which cell.width of incomplete cell has been computed
|
| 89 |
|
|
cell cell // current incomplete cell; cell.width is up to buf[pos] excluding ignored sections
|
| 90 |
|
|
endChar byte // terminating char of escaped sequence (Escape for escapes, '>', ';' for HTML tags/entities, or 0)
|
| 91 |
|
|
lines [][]cell // list of lines; each line is a list of cells
|
| 92 |
|
|
widths []int // list of column widths in runes - re-used during formatting
|
| 93 |
|
|
}
|
| 94 |
|
|
|
| 95 |
|
|
func (b *Writer) addLine() { b.lines = append(b.lines, []cell{}) }
|
| 96 |
|
|
|
| 97 |
|
|
// Reset the current state.
|
| 98 |
|
|
func (b *Writer) reset() {
|
| 99 |
|
|
b.buf.Reset()
|
| 100 |
|
|
b.pos = 0
|
| 101 |
|
|
b.cell = cell{}
|
| 102 |
|
|
b.endChar = 0
|
| 103 |
|
|
b.lines = b.lines[0:0]
|
| 104 |
|
|
b.widths = b.widths[0:0]
|
| 105 |
|
|
b.addLine()
|
| 106 |
|
|
}
|
| 107 |
|
|
|
| 108 |
|
|
// Internal representation (current state):
|
| 109 |
|
|
//
|
| 110 |
|
|
// - all text written is appended to buf; tabs and line breaks are stripped away
|
| 111 |
|
|
// - at any given time there is a (possibly empty) incomplete cell at the end
|
| 112 |
|
|
// (the cell starts after a tab or line break)
|
| 113 |
|
|
// - cell.size is the number of bytes belonging to the cell so far
|
| 114 |
|
|
// - cell.width is text width in runes of that cell from the start of the cell to
|
| 115 |
|
|
// position pos; html tags and entities are excluded from this width if html
|
| 116 |
|
|
// filtering is enabled
|
| 117 |
|
|
// - the sizes and widths of processed text are kept in the lines list
|
| 118 |
|
|
// which contains a list of cells for each line
|
| 119 |
|
|
// - the widths list is a temporary list with current widths used during
|
| 120 |
|
|
// formatting; it is kept in Writer because it's re-used
|
| 121 |
|
|
//
|
| 122 |
|
|
// |<---------- size ---------->|
|
| 123 |
|
|
// | |
|
| 124 |
|
|
// |<- width ->|<- ignored ->| |
|
| 125 |
|
|
// | | | |
|
| 126 |
|
|
// [---processed---tab------------......]
|
| 127 |
|
|
// ^ ^ ^
|
| 128 |
|
|
// | | |
|
| 129 |
|
|
// buf start of incomplete cell pos
|
| 130 |
|
|
|
| 131 |
|
|
// Formatting can be controlled with these flags.
|
| 132 |
|
|
const (
|
| 133 |
|
|
// Ignore html tags and treat entities (starting with '&'
|
| 134 |
|
|
// and ending in ';') as single characters (width = 1).
|
| 135 |
|
|
FilterHTML uint = 1 << iota
|
| 136 |
|
|
|
| 137 |
|
|
// Strip Escape characters bracketing escaped text segments
|
| 138 |
|
|
// instead of passing them through unchanged with the text.
|
| 139 |
|
|
StripEscape
|
| 140 |
|
|
|
| 141 |
|
|
// Force right-alignment of cell content.
|
| 142 |
|
|
// Default is left-alignment.
|
| 143 |
|
|
AlignRight
|
| 144 |
|
|
|
| 145 |
|
|
// Handle empty columns as if they were not present in
|
| 146 |
|
|
// the input in the first place.
|
| 147 |
|
|
DiscardEmptyColumns
|
| 148 |
|
|
|
| 149 |
|
|
// Always use tabs for indentation columns (i.e., padding of
|
| 150 |
|
|
// leading empty cells on the left) independent of padchar.
|
| 151 |
|
|
TabIndent
|
| 152 |
|
|
|
| 153 |
|
|
// Print a vertical bar ('|') between columns (after formatting).
|
| 154 |
|
|
// Discarded columns appear as zero-width columns ("||").
|
| 155 |
|
|
Debug
|
| 156 |
|
|
)
|
| 157 |
|
|
|
| 158 |
|
|
// A Writer must be initialized with a call to Init. The first parameter (output)
|
| 159 |
|
|
// specifies the filter output. The remaining parameters control the formatting:
|
| 160 |
|
|
//
|
| 161 |
|
|
// minwidth minimal cell width including any padding
|
| 162 |
|
|
// tabwidth width of tab characters (equivalent number of spaces)
|
| 163 |
|
|
// padding padding added to a cell before computing its width
|
| 164 |
|
|
// padchar ASCII char used for padding
|
| 165 |
|
|
// if padchar == '\t', the Writer will assume that the
|
| 166 |
|
|
// width of a '\t' in the formatted output is tabwidth,
|
| 167 |
|
|
// and cells are left-aligned independent of align_left
|
| 168 |
|
|
// (for correct-looking results, tabwidth must correspond
|
| 169 |
|
|
// to the tab width in the viewer displaying the result)
|
| 170 |
|
|
// flags formatting control
|
| 171 |
|
|
//
|
| 172 |
|
|
// To format in tab-separated columns with a tab stop of 8:
|
| 173 |
|
|
// b.Init(w, 8, 1, 8, '\t', 0);
|
| 174 |
|
|
//
|
| 175 |
|
|
// To format in space-separated columns with at least 4 spaces between columns:
|
| 176 |
|
|
// b.Init(w, 0, 4, 8, ' ', 0);
|
| 177 |
|
|
//
|
| 178 |
|
|
func (b *Writer) Init(output io.Writer, minwidth, tabwidth, padding int, padchar byte, flags uint) *Writer {
|
| 179 |
|
|
if minwidth < 0 || tabwidth < 0 || padding < 0 {
|
| 180 |
|
|
panic("negative minwidth, tabwidth, or padding")
|
| 181 |
|
|
}
|
| 182 |
|
|
b.output = output
|
| 183 |
|
|
b.minwidth = minwidth
|
| 184 |
|
|
b.tabwidth = tabwidth
|
| 185 |
|
|
b.padding = padding
|
| 186 |
|
|
for i := range b.padbytes {
|
| 187 |
|
|
b.padbytes[i] = padchar
|
| 188 |
|
|
}
|
| 189 |
|
|
if padchar == '\t' {
|
| 190 |
|
|
// tab padding enforces left-alignment
|
| 191 |
|
|
flags &^= AlignRight
|
| 192 |
|
|
}
|
| 193 |
|
|
b.flags = flags
|
| 194 |
|
|
|
| 195 |
|
|
b.reset()
|
| 196 |
|
|
|
| 197 |
|
|
return b
|
| 198 |
|
|
}
|
| 199 |
|
|
|
| 200 |
|
|
// debugging support (keep code around)
|
| 201 |
|
|
func (b *Writer) dump() {
|
| 202 |
|
|
pos := 0
|
| 203 |
|
|
for i, line := range b.lines {
|
| 204 |
|
|
print("(", i, ") ")
|
| 205 |
|
|
for _, c := range line {
|
| 206 |
|
|
print("[", string(b.buf.Bytes()[pos:pos+c.size]), "]")
|
| 207 |
|
|
pos += c.size
|
| 208 |
|
|
}
|
| 209 |
|
|
print("\n")
|
| 210 |
|
|
}
|
| 211 |
|
|
print("\n")
|
| 212 |
|
|
}
|
| 213 |
|
|
|
| 214 |
|
|
// local error wrapper so we can distinguish errors we want to return
|
| 215 |
|
|
// as errors from genuine panics (which we don't want to return as errors)
|
| 216 |
|
|
type osError struct {
|
| 217 |
|
|
err error
|
| 218 |
|
|
}
|
| 219 |
|
|
|
| 220 |
|
|
func (b *Writer) write0(buf []byte) {
|
| 221 |
|
|
n, err := b.output.Write(buf)
|
| 222 |
|
|
if n != len(buf) && err == nil {
|
| 223 |
|
|
err = io.ErrShortWrite
|
| 224 |
|
|
}
|
| 225 |
|
|
if err != nil {
|
| 226 |
|
|
panic(osError{err})
|
| 227 |
|
|
}
|
| 228 |
|
|
}
|
| 229 |
|
|
|
| 230 |
|
|
func (b *Writer) writeN(src []byte, n int) {
|
| 231 |
|
|
for n > len(src) {
|
| 232 |
|
|
b.write0(src)
|
| 233 |
|
|
n -= len(src)
|
| 234 |
|
|
}
|
| 235 |
|
|
b.write0(src[0:n])
|
| 236 |
|
|
}
|
| 237 |
|
|
|
| 238 |
|
|
var (
|
| 239 |
|
|
newline = []byte{'\n'}
|
| 240 |
|
|
tabs = []byte("\t\t\t\t\t\t\t\t")
|
| 241 |
|
|
)
|
| 242 |
|
|
|
| 243 |
|
|
func (b *Writer) writePadding(textw, cellw int, useTabs bool) {
|
| 244 |
|
|
if b.padbytes[0] == '\t' || useTabs {
|
| 245 |
|
|
// padding is done with tabs
|
| 246 |
|
|
if b.tabwidth == 0 {
|
| 247 |
|
|
return // tabs have no width - can't do any padding
|
| 248 |
|
|
}
|
| 249 |
|
|
// make cellw the smallest multiple of b.tabwidth
|
| 250 |
|
|
cellw = (cellw + b.tabwidth - 1) / b.tabwidth * b.tabwidth
|
| 251 |
|
|
n := cellw - textw // amount of padding
|
| 252 |
|
|
if n < 0 {
|
| 253 |
|
|
panic("internal error")
|
| 254 |
|
|
}
|
| 255 |
|
|
b.writeN(tabs, (n+b.tabwidth-1)/b.tabwidth)
|
| 256 |
|
|
return
|
| 257 |
|
|
}
|
| 258 |
|
|
|
| 259 |
|
|
// padding is done with non-tab characters
|
| 260 |
|
|
b.writeN(b.padbytes[0:], cellw-textw)
|
| 261 |
|
|
}
|
| 262 |
|
|
|
| 263 |
|
|
var vbar = []byte{'|'}
|
| 264 |
|
|
|
| 265 |
|
|
func (b *Writer) writeLines(pos0 int, line0, line1 int) (pos int) {
|
| 266 |
|
|
pos = pos0
|
| 267 |
|
|
for i := line0; i < line1; i++ {
|
| 268 |
|
|
line := b.lines[i]
|
| 269 |
|
|
|
| 270 |
|
|
// if TabIndent is set, use tabs to pad leading empty cells
|
| 271 |
|
|
useTabs := b.flags&TabIndent != 0
|
| 272 |
|
|
|
| 273 |
|
|
for j, c := range line {
|
| 274 |
|
|
if j > 0 && b.flags&Debug != 0 {
|
| 275 |
|
|
// indicate column break
|
| 276 |
|
|
b.write0(vbar)
|
| 277 |
|
|
}
|
| 278 |
|
|
|
| 279 |
|
|
if c.size == 0 {
|
| 280 |
|
|
// empty cell
|
| 281 |
|
|
if j < len(b.widths) {
|
| 282 |
|
|
b.writePadding(c.width, b.widths[j], useTabs)
|
| 283 |
|
|
}
|
| 284 |
|
|
} else {
|
| 285 |
|
|
// non-empty cell
|
| 286 |
|
|
useTabs = false
|
| 287 |
|
|
if b.flags&AlignRight == 0 { // align left
|
| 288 |
|
|
b.write0(b.buf.Bytes()[pos : pos+c.size])
|
| 289 |
|
|
pos += c.size
|
| 290 |
|
|
if j < len(b.widths) {
|
| 291 |
|
|
b.writePadding(c.width, b.widths[j], false)
|
| 292 |
|
|
}
|
| 293 |
|
|
} else { // align right
|
| 294 |
|
|
if j < len(b.widths) {
|
| 295 |
|
|
b.writePadding(c.width, b.widths[j], false)
|
| 296 |
|
|
}
|
| 297 |
|
|
b.write0(b.buf.Bytes()[pos : pos+c.size])
|
| 298 |
|
|
pos += c.size
|
| 299 |
|
|
}
|
| 300 |
|
|
}
|
| 301 |
|
|
}
|
| 302 |
|
|
|
| 303 |
|
|
if i+1 == len(b.lines) {
|
| 304 |
|
|
// last buffered line - we don't have a newline, so just write
|
| 305 |
|
|
// any outstanding buffered data
|
| 306 |
|
|
b.write0(b.buf.Bytes()[pos : pos+b.cell.size])
|
| 307 |
|
|
pos += b.cell.size
|
| 308 |
|
|
} else {
|
| 309 |
|
|
// not the last line - write newline
|
| 310 |
|
|
b.write0(newline)
|
| 311 |
|
|
}
|
| 312 |
|
|
}
|
| 313 |
|
|
return
|
| 314 |
|
|
}
|
| 315 |
|
|
|
| 316 |
|
|
// Format the text between line0 and line1 (excluding line1); pos
|
| 317 |
|
|
// is the buffer position corresponding to the beginning of line0.
|
| 318 |
|
|
// Returns the buffer position corresponding to the beginning of
|
| 319 |
|
|
// line1 and an error, if any.
|
| 320 |
|
|
//
|
| 321 |
|
|
func (b *Writer) format(pos0 int, line0, line1 int) (pos int) {
|
| 322 |
|
|
pos = pos0
|
| 323 |
|
|
column := len(b.widths)
|
| 324 |
|
|
for this := line0; this < line1; this++ {
|
| 325 |
|
|
line := b.lines[this]
|
| 326 |
|
|
|
| 327 |
|
|
if column < len(line)-1 {
|
| 328 |
|
|
// cell exists in this column => this line
|
| 329 |
|
|
// has more cells than the previous line
|
| 330 |
|
|
// (the last cell per line is ignored because cells are
|
| 331 |
|
|
// tab-terminated; the last cell per line describes the
|
| 332 |
|
|
// text before the newline/formfeed and does not belong
|
| 333 |
|
|
// to a column)
|
| 334 |
|
|
|
| 335 |
|
|
// print unprinted lines until beginning of block
|
| 336 |
|
|
pos = b.writeLines(pos, line0, this)
|
| 337 |
|
|
line0 = this
|
| 338 |
|
|
|
| 339 |
|
|
// column block begin
|
| 340 |
|
|
width := b.minwidth // minimal column width
|
| 341 |
|
|
discardable := true // true if all cells in this column are empty and "soft"
|
| 342 |
|
|
for ; this < line1; this++ {
|
| 343 |
|
|
line = b.lines[this]
|
| 344 |
|
|
if column < len(line)-1 {
|
| 345 |
|
|
// cell exists in this column
|
| 346 |
|
|
c := line[column]
|
| 347 |
|
|
// update width
|
| 348 |
|
|
if w := c.width + b.padding; w > width {
|
| 349 |
|
|
width = w
|
| 350 |
|
|
}
|
| 351 |
|
|
// update discardable
|
| 352 |
|
|
if c.width > 0 || c.htab {
|
| 353 |
|
|
discardable = false
|
| 354 |
|
|
}
|
| 355 |
|
|
} else {
|
| 356 |
|
|
break
|
| 357 |
|
|
}
|
| 358 |
|
|
}
|
| 359 |
|
|
// column block end
|
| 360 |
|
|
|
| 361 |
|
|
// discard empty columns if necessary
|
| 362 |
|
|
if discardable && b.flags&DiscardEmptyColumns != 0 {
|
| 363 |
|
|
width = 0
|
| 364 |
|
|
}
|
| 365 |
|
|
|
| 366 |
|
|
// format and print all columns to the right of this column
|
| 367 |
|
|
// (we know the widths of this column and all columns to the left)
|
| 368 |
|
|
b.widths = append(b.widths, width) // push width
|
| 369 |
|
|
pos = b.format(pos, line0, this)
|
| 370 |
|
|
b.widths = b.widths[0 : len(b.widths)-1] // pop width
|
| 371 |
|
|
line0 = this
|
| 372 |
|
|
}
|
| 373 |
|
|
}
|
| 374 |
|
|
|
| 375 |
|
|
// print unprinted lines until end
|
| 376 |
|
|
return b.writeLines(pos, line0, line1)
|
| 377 |
|
|
}
|
| 378 |
|
|
|
| 379 |
|
|
// Append text to current cell.
|
| 380 |
|
|
func (b *Writer) append(text []byte) {
|
| 381 |
|
|
b.buf.Write(text)
|
| 382 |
|
|
b.cell.size += len(text)
|
| 383 |
|
|
}
|
| 384 |
|
|
|
| 385 |
|
|
// Update the cell width.
|
| 386 |
|
|
func (b *Writer) updateWidth() {
|
| 387 |
|
|
b.cell.width += utf8.RuneCount(b.buf.Bytes()[b.pos:b.buf.Len()])
|
| 388 |
|
|
b.pos = b.buf.Len()
|
| 389 |
|
|
}
|
| 390 |
|
|
|
| 391 |
|
|
// To escape a text segment, bracket it with Escape characters.
|
| 392 |
|
|
// For instance, the tab in this string "Ignore this tab: \xff\t\xff"
|
| 393 |
|
|
// does not terminate a cell and constitutes a single character of
|
| 394 |
|
|
// width one for formatting purposes.
|
| 395 |
|
|
//
|
| 396 |
|
|
// The value 0xff was chosen because it cannot appear in a valid UTF-8 sequence.
|
| 397 |
|
|
//
|
| 398 |
|
|
const Escape = '\xff'
|
| 399 |
|
|
|
| 400 |
|
|
// Start escaped mode.
|
| 401 |
|
|
func (b *Writer) startEscape(ch byte) {
|
| 402 |
|
|
switch ch {
|
| 403 |
|
|
case Escape:
|
| 404 |
|
|
b.endChar = Escape
|
| 405 |
|
|
case '<':
|
| 406 |
|
|
b.endChar = '>'
|
| 407 |
|
|
case '&':
|
| 408 |
|
|
b.endChar = ';'
|
| 409 |
|
|
}
|
| 410 |
|
|
}
|
| 411 |
|
|
|
| 412 |
|
|
// Terminate escaped mode. If the escaped text was an HTML tag, its width
|
| 413 |
|
|
// is assumed to be zero for formatting purposes; if it was an HTML entity,
|
| 414 |
|
|
// its width is assumed to be one. In all other cases, the width is the
|
| 415 |
|
|
// unicode width of the text.
|
| 416 |
|
|
//
|
| 417 |
|
|
func (b *Writer) endEscape() {
|
| 418 |
|
|
switch b.endChar {
|
| 419 |
|
|
case Escape:
|
| 420 |
|
|
b.updateWidth()
|
| 421 |
|
|
if b.flags&StripEscape == 0 {
|
| 422 |
|
|
b.cell.width -= 2 // don't count the Escape chars
|
| 423 |
|
|
}
|
| 424 |
|
|
case '>': // tag of zero width
|
| 425 |
|
|
case ';':
|
| 426 |
|
|
b.cell.width++ // entity, count as one rune
|
| 427 |
|
|
}
|
| 428 |
|
|
b.pos = b.buf.Len()
|
| 429 |
|
|
b.endChar = 0
|
| 430 |
|
|
}
|
| 431 |
|
|
|
| 432 |
|
|
// Terminate the current cell by adding it to the list of cells of the
|
| 433 |
|
|
// current line. Returns the number of cells in that line.
|
| 434 |
|
|
//
|
| 435 |
|
|
func (b *Writer) terminateCell(htab bool) int {
|
| 436 |
|
|
b.cell.htab = htab
|
| 437 |
|
|
line := &b.lines[len(b.lines)-1]
|
| 438 |
|
|
*line = append(*line, b.cell)
|
| 439 |
|
|
b.cell = cell{}
|
| 440 |
|
|
return len(*line)
|
| 441 |
|
|
}
|
| 442 |
|
|
|
| 443 |
|
|
func handlePanic(err *error) {
|
| 444 |
|
|
if e := recover(); e != nil {
|
| 445 |
|
|
*err = e.(osError).err // re-panics if it's not a local osError
|
| 446 |
|
|
}
|
| 447 |
|
|
}
|
| 448 |
|
|
|
| 449 |
|
|
// Flush should be called after the last call to Write to ensure
|
| 450 |
|
|
// that any data buffered in the Writer is written to output. Any
|
| 451 |
|
|
// incomplete escape sequence at the end is considered
|
| 452 |
|
|
// complete for formatting purposes.
|
| 453 |
|
|
//
|
| 454 |
|
|
func (b *Writer) Flush() (err error) {
|
| 455 |
|
|
defer b.reset() // even in the presence of errors
|
| 456 |
|
|
defer handlePanic(&err)
|
| 457 |
|
|
|
| 458 |
|
|
// add current cell if not empty
|
| 459 |
|
|
if b.cell.size > 0 {
|
| 460 |
|
|
if b.endChar != 0 {
|
| 461 |
|
|
// inside escape - terminate it even if incomplete
|
| 462 |
|
|
b.endEscape()
|
| 463 |
|
|
}
|
| 464 |
|
|
b.terminateCell(false)
|
| 465 |
|
|
}
|
| 466 |
|
|
|
| 467 |
|
|
// format contents of buffer
|
| 468 |
|
|
b.format(0, 0, len(b.lines))
|
| 469 |
|
|
|
| 470 |
|
|
return
|
| 471 |
|
|
}
|
| 472 |
|
|
|
| 473 |
|
|
var hbar = []byte("---\n")
|
| 474 |
|
|
|
| 475 |
|
|
// Write writes buf to the writer b.
|
| 476 |
|
|
// The only errors returned are ones encountered
|
| 477 |
|
|
// while writing to the underlying output stream.
|
| 478 |
|
|
//
|
| 479 |
|
|
func (b *Writer) Write(buf []byte) (n int, err error) {
|
| 480 |
|
|
defer handlePanic(&err)
|
| 481 |
|
|
|
| 482 |
|
|
// split text into cells
|
| 483 |
|
|
n = 0
|
| 484 |
|
|
for i, ch := range buf {
|
| 485 |
|
|
if b.endChar == 0 {
|
| 486 |
|
|
// outside escape
|
| 487 |
|
|
switch ch {
|
| 488 |
|
|
case '\t', '\v', '\n', '\f':
|
| 489 |
|
|
// end of cell
|
| 490 |
|
|
b.append(buf[n:i])
|
| 491 |
|
|
b.updateWidth()
|
| 492 |
|
|
n = i + 1 // ch consumed
|
| 493 |
|
|
ncells := b.terminateCell(ch == '\t')
|
| 494 |
|
|
if ch == '\n' || ch == '\f' {
|
| 495 |
|
|
// terminate line
|
| 496 |
|
|
b.addLine()
|
| 497 |
|
|
if ch == '\f' || ncells == 1 {
|
| 498 |
|
|
// A '\f' always forces a flush. Otherwise, if the previous
|
| 499 |
|
|
// line has only one cell which does not have an impact on
|
| 500 |
|
|
// the formatting of the following lines (the last cell per
|
| 501 |
|
|
// line is ignored by format()), thus we can flush the
|
| 502 |
|
|
// Writer contents.
|
| 503 |
|
|
if err = b.Flush(); err != nil {
|
| 504 |
|
|
return
|
| 505 |
|
|
}
|
| 506 |
|
|
if ch == '\f' && b.flags&Debug != 0 {
|
| 507 |
|
|
// indicate section break
|
| 508 |
|
|
b.write0(hbar)
|
| 509 |
|
|
}
|
| 510 |
|
|
}
|
| 511 |
|
|
}
|
| 512 |
|
|
|
| 513 |
|
|
case Escape:
|
| 514 |
|
|
// start of escaped sequence
|
| 515 |
|
|
b.append(buf[n:i])
|
| 516 |
|
|
b.updateWidth()
|
| 517 |
|
|
n = i
|
| 518 |
|
|
if b.flags&StripEscape != 0 {
|
| 519 |
|
|
n++ // strip Escape
|
| 520 |
|
|
}
|
| 521 |
|
|
b.startEscape(Escape)
|
| 522 |
|
|
|
| 523 |
|
|
case '<', '&':
|
| 524 |
|
|
// possibly an html tag/entity
|
| 525 |
|
|
if b.flags&FilterHTML != 0 {
|
| 526 |
|
|
// begin of tag/entity
|
| 527 |
|
|
b.append(buf[n:i])
|
| 528 |
|
|
b.updateWidth()
|
| 529 |
|
|
n = i
|
| 530 |
|
|
b.startEscape(ch)
|
| 531 |
|
|
}
|
| 532 |
|
|
}
|
| 533 |
|
|
|
| 534 |
|
|
} else {
|
| 535 |
|
|
// inside escape
|
| 536 |
|
|
if ch == b.endChar {
|
| 537 |
|
|
// end of tag/entity
|
| 538 |
|
|
j := i + 1
|
| 539 |
|
|
if ch == Escape && b.flags&StripEscape != 0 {
|
| 540 |
|
|
j = i // strip Escape
|
| 541 |
|
|
}
|
| 542 |
|
|
b.append(buf[n:j])
|
| 543 |
|
|
n = i + 1 // ch consumed
|
| 544 |
|
|
b.endEscape()
|
| 545 |
|
|
}
|
| 546 |
|
|
}
|
| 547 |
|
|
}
|
| 548 |
|
|
|
| 549 |
|
|
// append leftover text
|
| 550 |
|
|
b.append(buf[n:])
|
| 551 |
|
|
n = len(buf)
|
| 552 |
|
|
return
|
| 553 |
|
|
}
|
| 554 |
|
|
|
| 555 |
|
|
// NewWriter allocates and initializes a new tabwriter.Writer.
|
| 556 |
|
|
// The parameters are the same as for the the Init function.
|
| 557 |
|
|
//
|
| 558 |
|
|
func NewWriter(output io.Writer, minwidth, tabwidth, padding int, padchar byte, flags uint) *Writer {
|
| 559 |
|
|
return new(Writer).Init(output, minwidth, tabwidth, padding, padchar, flags)
|
| 560 |
|
|
}
|