diff options
author | Robert Griesemer <gri@golang.org> | 2009-10-08 15:23:49 -0700 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2009-10-08 15:23:49 -0700 |
commit | 761d75d6552dd511f95a5615c76d78182e01ec11 (patch) | |
tree | b1d6da510cc8e10b3b223b19f674a35e8cd0a6dd /src/pkg/tabwriter | |
parent | deb75d27ad956ac46545f9d931a00210da8a771e (diff) | |
download | go-761d75d6552dd511f95a5615c76d78182e01ec11.tar.gz |
- debugging support
R=rsc
DELTA=110 (98 added, 0 deleted, 12 changed)
OCL=35487
CL=35490
Diffstat (limited to 'src/pkg/tabwriter')
-rw-r--r-- | src/pkg/tabwriter/tabwriter.go | 41 | ||||
-rw-r--r-- | src/pkg/tabwriter/tabwriter_test.go | 81 |
2 files changed, 110 insertions, 12 deletions
diff --git a/src/pkg/tabwriter/tabwriter.go b/src/pkg/tabwriter/tabwriter.go index 15ffc79cc..372ebff8b 100644 --- a/src/pkg/tabwriter/tabwriter.go +++ b/src/pkg/tabwriter/tabwriter.go @@ -21,7 +21,7 @@ import ( // ---------------------------------------------------------------------------- // Filter implementation -// A cell represents a segment of text delineated by tabs, form-feed, +// A cell represents a segment of text delineated by tabs, formfeed, // or newline chars. The text itself is stored in a separate buffer; // cell only describes the segment's size in bytes, its width in runes, // and whether it's an htab ('\t') or vtab ('\v') terminated call. @@ -59,10 +59,10 @@ type cell struct { // are simply passed through. The widths of tags and entities are // assumed to be zero (tags) and one (entities) for formatting purposes. // -// The form feed character ('\f') acts like a newline but it also +// The formfeed character ('\f') acts like a newline but it also // terminates all columns in the current line (effectively calling // Flush). Cells in the next line start new columns. Unless found -// inside an HTML tag, form feed characters appear as newlines in +// inside an HTML tag, formfeed characters appear as newlines in // the output. // // The Writer must buffer input internally, because proper spacing @@ -78,7 +78,7 @@ type Writer struct { flags uint; // current state - buf bytes.Buffer; // collected text w/o tabs, newlines, or form feed chars + buf bytes.Buffer; // collected text w/o tabs, newlines, or formfeed chars pos int; // buffer position up to which width of incomplete cell has been computed cell cell; // current incomplete cell; cell.width is up to buf[pos] w/o ignored sections html_char byte; // terminating char of html tag/entity, or 0 ('>', ';', or 0) @@ -111,9 +111,9 @@ func (b *Writer) reset() { // Internal representation (current state): // -// - all text written is appended to buf; form feed chars, tabs and newlines are stripped away +// - all text written is appended to buf; formfeed chars, tabs and newlines are stripped away // - at any given time there is a (possibly empty) incomplete cell at the end -// (the cell starts after a tab, form feed, or newline) +// (the cell starts after a tab, formfeed, or newline) // - cell.size is the number of bytes belonging to the cell so far // - cell.width is text width in runes of that cell from the start of the cell to // position pos; html tags and entities are excluded from this width if html @@ -146,6 +146,10 @@ const ( // Handle empty columns as if they were not present in // the input in the first place. DiscardEmptyColumns; + + // Print a vertical bar ('|') between columns (after formatting). + // Discarded colums appear as zero-width columns ("||"). + Debug; ) @@ -153,15 +157,21 @@ const ( // specifies the filter output. The remaining parameters control the formatting: // // cellwidth minimal cell width -// padding additional cell padding +// padding cell padding added to 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 cellwidth, -// and cells are left-aligned independent of align_left -// (for correct-looking results, cellwidth must correspond -// to the tab width in the viewer displaying the result) +// if padchar == '\t', the Writer will assume that the +// width of a '\t' in the formatted output is cellwidth, +// and cells are left-aligned independent of align_left +// (for correct-looking results, cellwidth must correspond +// to the tab width in the viewer displaying the result) // flags formatting control // +// To format in tab-separated columns with a tab stop of 8: +// b.Init(w, 8, 1, '\t', 0); +// +// To format in space-separated columns with at least 4 spaces between columns: +// b.Init(w, 1, 4, ' ', 0); +// func (b *Writer) Init(output io.Writer, cellwidth, padding int, padchar byte, flags uint) *Writer { if cellwidth < 0 { panic("negative cellwidth"); @@ -243,6 +253,8 @@ func (b *Writer) writePadding(textw, cellw int) os.Error { } +var vbar = []byte{'|'}; + func (b *Writer) writeLines(pos0 int, line0, line1 int) (pos int, err os.Error) { pos = pos0; for i := line0; i < line1; i++ { @@ -250,6 +262,11 @@ func (b *Writer) writeLines(pos0 int, line0, line1 int) (pos int, err os.Error) for j := 0; j < line.Len(); j++ { c := line.At(j).(cell); + if j > 0 && b.flags&Debug != 0 { + if err = b.write0(vbar); err != nil { + return; + } + } switch { default: // align left diff --git a/src/pkg/tabwriter/tabwriter_test.go b/src/pkg/tabwriter/tabwriter_test.go index b43a26980..927ed5e21 100644 --- a/src/pkg/tabwriter/tabwriter_test.go +++ b/src/pkg/tabwriter/tabwriter_test.go @@ -120,6 +120,13 @@ var tests = []entry { }, entry{ + "1 debug", + 8, 1, '.', Debug, + "", + "" + }, + + entry{ "2", 8, 1, '.', 0, "\n\n\n", @@ -169,6 +176,13 @@ var tests = []entry { }, entry{ + "5c debug", + 8, 1, '.', Debug, + "*\t*\t", + "*.......|*" + }, + + entry{ "5d", 8, 1, '.', AlignRight, "*\t*\t", @@ -232,6 +246,13 @@ var tests = []entry { }, entry{ + "7g debug", + 8, 1, '.', FilterHTML | Debug, + "g) f<o\t<b>bar</b>\t non-terminated entity &", + "g) f<o..|<b>bar</b>.....| non-terminated entity &" + }, + + entry{ "8", 8, 1, '*', 0, "Hello, world!\n", @@ -269,6 +290,16 @@ var tests = []entry { }, entry{ + "9c debug", + 0, 0, '.', Debug, + "1\t2\t3\t4\f" // \f causes a newline and flush + "11\t222\t3333\t44444\n", + + "1|2|3|4\n" + "11|222|3333|44444\n" + }, + + entry{ "10a", 5, 0, '.', 0, "1\t2\t3\t4\n", @@ -409,6 +440,24 @@ var tests = []entry { }, entry{ + "14 debug", + 0, 2, ' ', AlignRight | Debug, + ".0\t.3\t2.4\t-5.1\t\n" + "23.0\t12345678.9\t2.4\t-989.4\t\n" + "5.1\t12.0\t2.4\t-7.0\t\n" + ".0\t0.0\t332.0\t8908.0\t\n" + ".0\t-.3\t456.4\t22.1\t\n" + ".0\t1.2\t44.4\t-13.3\t\t", + + " .0| .3| 2.4| -5.1|\n" + " 23.0| 12345678.9| 2.4| -989.4|\n" + " 5.1| 12.0| 2.4| -7.0|\n" + " .0| 0.0| 332.0| 8908.0|\n" + " .0| -.3| 456.4| 22.1|\n" + " .0| 1.2| 44.4| -13.3|" + }, + + entry{ "15a", 4, 0, '.', 0, "a\t\tb", @@ -469,6 +518,22 @@ var tests = []entry { }, entry{ + "16b debug", + 100, 0, '\t', DiscardEmptyColumns | Debug, + "a\vb\v\vd\n" + "a\vb\v\vd\ve\n" + "a\n" + "a\vb\vc\vd\n" + "a\vb\vc\vd\ve\n", + + "a\t|b\t||d\n" + "a\t|b\t||d\t|e\n" + "a\n" + "a\t|b\t|c\t|d\n" + "a\t|b\t|c\t|d\t|e\n" + }, + + entry{ "16c", 100, 0, '\t', DiscardEmptyColumns, "a\tb\t\td\n" // hard tabs - do not discard column @@ -483,6 +548,22 @@ var tests = []entry { "a\tb\tc\td\n" "a\tb\tc\td\te\n" }, + + entry{ + "16c debug", + 100, 0, '\t', DiscardEmptyColumns | Debug, + "a\tb\t\td\n" // hard tabs - do not discard column + "a\tb\t\td\te\n" + "a\n" + "a\tb\tc\td\n" + "a\tb\tc\td\te\n", + + "a\t|b\t|\t|d\n" + "a\t|b\t|\t|d\t|e\n" + "a\n" + "a\t|b\t|c\t|d\n" + "a\t|b\t|c\t|d\t|e\n" + }, } |