Comments (8)
Could you please provide a reproduction? Please specify inputs, actual outputs and expected outputs.
from tabwriter.
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.
That does not seem expected to me. Patches are welcome.
from tabwriter.
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.
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.
from tabwriter.
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.
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)
- Formatting broken when using ansi colours HOT 8
- move binary to src HOT 6
- Relicense under dual MIT/Apache-2.0
- approaching 1.0 HOT 4
- Allow Center and Right Alignment of Texts HOT 4
- Broken link to CSV toolkit example in README.md
- MemWriter has been deprecated HOT 2
- Example in README doesn't work HOT 1
- Awkwardness with std::fmt::Display? HOT 2
- padding(0) gives one space of padding HOT 2
- Possible to disable alignment for certain lines? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tabwriter.