Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text/tabwriter: AlignRight does not pad or align right-most column #14412

Closed
spenczar opened this issue Feb 19, 2016 · 6 comments
Closed

text/tabwriter: AlignRight does not pad or align right-most column #14412

spenczar opened this issue Feb 19, 2016 · 6 comments
Milestone

Comments

@spenczar
Copy link
Contributor

What did you do?

I wrote a few lines to a tabwriter with AlignRight and with padding:

package main

import (
        "fmt"
        "os"
        "text/tabwriter"
)

func main() {
        flags := tabwriter.AlignRight | tabwriter.Debug
        w := tabwriter.NewWriter(os.Stderr, 0, 0, 1, '.', flags)
        fmt.Fprintln(w, "a\tb\tc")
        fmt.Fprintln(w, "aa\tbb\tcc")
        w.Flush()
}

What did you expect to see?

All three columns aligned right, padded with the padding rune:

..a|..b|..c
.aa|.bb|.cc

What did you see instead?

The first two columns work, but the right-most column is left aligned and without padding:

..a|..b|c
.aa|.bb|cc

Go 1.5.3 and tip (9aa630f) both exhibit this on linux/amd64.

A real-world example of output that might help show what this ends up doing:

                         codec  bytes  encode ns  append nsdecode ns
  simpleproto.SimpleProtoCodec   1255       6596       106810584

Pretty hard to read that last column.

@ianlancetaylor ianlancetaylor added this to the Go1.7 milestone Feb 19, 2016
@ianlancetaylor
Copy link
Contributor

CC @griesemer

@griesemer
Copy link
Contributor

Note the documentation in the tabwriter package:

A Writer is a filter that inserts padding around tab-delimited columns in its input to align them in the output. (https://golang.org/pkg/text/tabwriter/)

If you delimit (rather than separate) the colums with tabs it does what you want:

package main

import (
        "fmt"
        "os"
        "text/tabwriter"
)

func main() {
        flags := tabwriter.AlignRight | tabwriter.Debug
        w := tabwriter.NewWriter(os.Stderr, 0, 0, 1, '.', flags)
        fmt.Fprintln(w, "a\tb\tc\t")     // <<<<<< delimiting tab at end
        fmt.Fprintln(w, "aa\tbb\tcc\t")  // <<<<<< delimiting tab at end
        w.Flush()
}

And the output is:

..a|..b|..c|
.aa|.bb|.cc|

This is working as intended. Closing.

@spenczar
Copy link
Contributor Author

@griesemer, thank you for clarifying. Perhaps this needs a documentation update, since the tabwriter documentation says this:

The Writer treats incoming bytes as UTF-8 encoded text consisting of cells terminated by (horizontal or vertical) tabs or line breaks (newline or formfeed characters). Cells in adjacent lines constitute a column.

Cells can be terminated by line breaks; I'd think that the line breaks after the right-most column in my example code would have led tabwriter to interpret that last column as align-able.

Should the documentation be updated to say that the cells can only be terminated by horizontal or vertical tabs? I might still be misinterpreting.

@griesemer
Copy link
Contributor

@spenczar Fair enough. But then read further:

Note that cells are tab-terminated, not tab-separated: trailing non-tab text at the end of a line does not form a column cell.

This is emphasized twice. That said, I agree that this does appear to contradict the former sentence. I'll update the doc.

@gopherbot
Copy link
Contributor

CL https://golang.org/cl/19855 mentions this issue.

gopherbot pushed a commit that referenced this issue Feb 23, 2016
More clearly distinguish between tab-terminated cells
which are part of an (aligned) column, and non-tab terminated
cells which are not part of a column. Added additional examples.

For #14412.

Change-Id: If72607385752e221eaa2518238b11f48fbcb8a90
Reviewed-on: https://go-review.googlesource.com/19855
Reviewed-by: Alan Donovan <adonovan@google.com>
@spenczar
Copy link
Contributor Author

Thank you - I really appreciate the work on docs, boring as it may be. The examples make this sparklingly clear now.

@golang golang locked and limited conversation to collaborators Feb 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants