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

Right alignment of last column #29

Open
mj-64 opened this issue Mar 9, 2020 · 8 comments · May be fixed by #30
Open

Right alignment of last column #29

mj-64 opened this issue Mar 9, 2020 · 8 comments · May be fixed by #30

Comments

@mj-64
Copy link

mj-64 commented Mar 9, 2020

Right alignment of the last column only works when there is a trailing tab on the line. Otherwise the last column is left-aligned which looks strange.
If this is a known restriction and not a bug it could possibly be documented.

@BurntSushi
Copy link
Owner

Could you please provide a reproduction? Please specify inputs, actual outputs and expected outputs.

@mj-64
Copy link
Author

mj-64 commented Mar 9, 2020

use std::io::{self, Write};
use tabwriter::{Alignment, TabWriter};

fn main() {
    // No trailing \t, last col is not right-aligned
    let mut writer = TabWriter::new(io::stdout()).alignment(Alignment::Right);
    write!(&mut writer, "col1\tcol2\tcol3\n").unwrap();
    write!(&mut writer, "1\t2\t3\n").unwrap();
    writer.flush().unwrap();
    // Trailing \t, last col is right-aligned
    let mut writer1 = TabWriter::new(io::stdout()).alignment(Alignment::Right);
    write!(&mut writer1, "col1\tcol2\tcol3\t\n").unwrap();
    write!(&mut writer1, "1\t2\t3\t\n").unwrap();
    writer1.flush().unwrap();
}

Output:

col1  col2  col3
   1     2  3
col1  col2  col3  
   1     2     3  

Expected output:

col1  col2  col3
   1     2     3
col1  col2  col3  
   1     2     3  

@BurntSushi
Copy link
Owner

That does not seem expected to me. Patches are welcome.

cc @alex-ozdemir

@mj-64
Copy link
Author

mj-64 commented Mar 9, 2020

Thanks for looking into this. I came up with that workaround of using a trailing tab by reading the original thread about alignment here: #24 (comment)

Trailing tabs work for me but I think it should be mentioned in the docs if it cannot be easily fixed.

@mj-64
Copy link
Author

mj-64 commented Mar 10, 2020

I looked at Go's implementation of tabwriter at https://golang.org/pkg/text/tabwriter/ and it seems that it shows exactly the same behavior (look at the trailing tab example). So maybe you want to keep it that way to be compatible with the Go tabwriter.

Still, I think it is kind of surprising to the user if he wants to output a table with tabwriter and the last column is not right aligned, despite specifying Alignment::Right.

I guess if it is documented properly like the Go version it's ok, because the user can always output a trailing \t.

@alex-ozdemir
Copy link
Contributor

alex-ozdemir commented Mar 10, 2020 via email

@mj-64
Copy link
Author

mj-64 commented Mar 10, 2020

Basically we have two options:

  • Fix that behavior so the last cell is also right/center aligned.
  • Keep it how it is and add a documentation that you need to add a trailing \t. for right/center alignment.

Since the crate shares its name with the Go package, I think it is reasonable to keep it compatible with it. Also, when outputting a table row, adding a \t after each element is easier than having a special case for the first element. It is basically the same as considering \t as a cell-ending token visa a cell-separating token.

I think the owner of the package should decide.

@BurntSushi
Copy link
Owner

It's been a long time since I've looked into the implementation here.

If there's an easy way to fix this that doesn't involve completely rewriting everything, then we should do it.

There was never any intent on my part to be specifically compatible with Go's tabwriter package. Go just inspired this crate.

alex-ozdemir added a commit to alex-ozdemir/tabwriter that referenced this issue Mar 11, 2020
Previously, a **cell** was a block of text to the left of some tab.

This meant that a table like this:

   aa\tbb
   a\tb

would be formatted as

   aa bb
    a b

in right-alignment mode because the b's were not actually in a cell, and
thus didn't get aligned.

This commit modifies the notion of a "cell" in right- and
center-alignment mode to also include text to the right of the last tab
on the line.

This text is now considered part of a cell and aligned accordingly.

Implemented as describe above, this change would cause many trailing
spaces to be printed in right- and center-alignment modes, including on
empty lines.

To alleviate this, trailing spaces in the final cell of a line are not
printed.

fixes BurntSushi#29
@alex-ozdemir alex-ozdemir linked a pull request Mar 11, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants