Open euank opened 2 years ago
Hey @euank, thanks for putting in the effort to file this issue.
This was partially a toy project to get a library crate on crates.io, and as a result the code wasn't particularly robust. All of the StringBuffer
code was made for the case I originally wanted to use this for, which was putting regular ASCII text from a timetable in pretty-ish Unicode tables. I wanted to fill the buffer with spaces to begin with so that blank spaces wouldn't need to be overwritten, and then effectively "fill in" the content. Methods like push_bytes_fixed_width
were made to work with table fields with a fixed width; rather than writing a bunch of spaces, it would just write the content and then skip the index forward to the next field. In retrospect, I should have justified the existence of StringBuffer
to avoid this kind of confusion.
When the text is centered, that functionality is used improperly (see line 296). Rather than writing only what is necessary, a new String
is created with the spaces already built-in, which means doing a copy/allocation unnecessarily. This should ideally be addressed at some point.
Your code doesn't use an equivalent of push_bytes_fixed_width
, and so the following example doesn't run properly.
euank/unicode_prettytable on ξ HEAD (ef7039b) is π¦ v0.3.1 via π¦ v1.56.0 took 16ms
at 10:22 am β― cargo run --example separate_header
Compiling unicode-prettytable v0.3.1 (/home/sridaran/Development/Rust/SelfPublished/unicode-prettytable/forks/euank/unicode_prettytable)
Finished dev [unoptimized + debuginfo] target(s) in 1.32s
Running `/home/sridaran/Development/Rust/SelfPublished/unicode-prettytable/forks/euank/target/debug/examples/separate_header`
ββββββββββββ€ββββββββ€ββββββββββββββββ€ββββββββββββββββ
β name β age β height (in) β weight (lb) β
ββββββββββββͺββββββββͺββββββββββββββββͺββββββββββββββββ‘
βDaveβ14β72β164β
ββββββββββββΌββββββββΌββββββββββββββββΌββββββββββββββββ€
βJoshuaβ24β65β141β
ββββββββββββΌββββββββΌββββββββββββββββΌββββββββββββββββ€
βStacieβ22β61β124β
ββββββββββββΌββββββββΌββββββββββββββββΌββββββββββββββββ€
βCatherineβ30β68β130β
ββββββββββββΌββββββββΌββββββββββββββββΌββββββββββββββββ€
βJackβ8β58β78β
ββββββββββββ΄ββββββββ΄ββββββββββββββββ΄ββββββββββββββββ
You could remedy this by creating a right-padded String when centered_text
is false (line 249 in Table), but in my opinion this is needlessly inefficient.
I later found more featureful crates like comfy-table and prettytable-rs, both of which (I assume) have support for Unicode. I switched over my motivating project from using this crate to comfy-table, which offered a lot more customizations than what I put in this crate. In its current state, this crate offers comparatively efficient table creation.
I think one other approach to fixing Unicode is to allow the underlying StringBuffer
to resize, which would mean changing the implementation to use Vec
methods instead of raw write
. Because there are significantly more customizable alternatives, I'd like to keep this crate as fast as possible so that it can serve some sort of purpose. If you have any questions, feel free to ask me.
Thanks for the detailed response, @srithon! Indeed, you're right that I misunderstood how padding was happening and got that wrong.
I also appreciate suggestion of alternative creates; I'll probably switch over to one of those.
That solves my immediate problem, but I guess it's worth figuring out if there's something else to do. I ended up here because this crate showed up more easily in whatever searches I did than comfy-table or prettytable.
I'll suggest that we have the following options:
There might be other reasonable options, but I feel like those are the logical next steps from here. Do you have a strong preference for any of those options?
Thanks for the points, @euank! I think the way to go is 1, and then if we ever get time to do so, address 2. It would be nice to have some benchmarks on how fast the crate actually is, but in the process of writing the benchmarks I saw that the required input of a Vec<Vec<T>>
rather than just an Iterator<Item=Iterator<Item=T>>
was a pain to work with. I definitely thought of this while writing the code originally, but for some reason didn't go through with it. 1 can certainly be done without the benchmarks, but they would definitely be nice to have.
When I get some time, I'll work on adding a README to the project root. Thanks for the feedback!
The following code panics currently:
The output with backtrace is:
Removing the util StringBuffer stuff was enough to fix it for me. Specifically, I fixed it over in this commit: https://github.com/euank/unicode-prettytable/commit/ef7039b4e1e19ab4da1bb7aed86721e974e77ef7
If you want I can make a PR with that; I wasn't sure if you might want a different fix or such.