Giter Club home page Giter Club logo

Comments (8)

BurntSushi avatar BurntSushi commented on July 18, 2024

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

from tabwriter.

mj-64 avatar mj-64 commented on July 18, 2024
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  

from tabwriter.

BurntSushi avatar BurntSushi commented on July 18, 2024

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

cc @alex-ozdemir

from tabwriter.

mj-64 avatar mj-64 commented on July 18, 2024

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.

from tabwriter.

mj-64 avatar mj-64 commented on July 18, 2024

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.

from tabwriter.

alex-ozdemir avatar alex-ozdemir commented on July 18, 2024

from tabwriter.

mj-64 avatar mj-64 commented on July 18, 2024

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.

from tabwriter.

BurntSushi avatar BurntSushi commented on July 18, 2024

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.

from tabwriter.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.