sharkdp / hexyl

A command-line hex viewer
Apache License 2.0
8.92k stars 227 forks source link

Provide examples #81

Closed lovesegfault closed 4 years ago

lovesegfault commented 4 years ago

I wanted to use hexyl as a library to pretty print the memory of a VM I am working on, for error dumps. The memory is just [u8; 4096] so I imagine it should be pretty simple, but I couldn't find an example of how to use this as a library.

lovesegfault commented 4 years ago

On a secondary note, I was hoping to use hexyl as my Display implementation, but now I realize this can't be done since Printer doesn't allow you to specify a custom writer like Display expects.

sharkdp commented 4 years ago

but I couldn't find an example of how to use this as a library.

Because, so far, nobody volunteered to write the documentation. Note that hexyl was only recently turned into a library crate (#68). I never really designed it to be a library as well.

I imagine it should be pretty simple

Here you go: https://github.com/sharkdp/hexyl/blob/abc79396dfb50791d643e389c2b275bfcd323aa6/examples/simple.rs#L1-L20

but now I realize this can't be done since Printer doesn't allow you to specify a custom writer like Display expects.

Why not? Printer::new takes a &'a mut Writer with Writer: Write (i.e. it takes a &mut impl Write). When implementing Display::fmt, you have access to a Formatter, which itself implements Write.

sharkdp commented 4 years ago

Some kind of reaction/response would have been nice :-/

lovesegfault commented 4 years ago

Hi @sharkdp, I'm really sorry for not having gotten back to you, this escaped me in the GH notification chaos.

Thanks a lot for adding the lib usage example! Regarding the issues with implementing Display, I haven't touched the project I was working on in a while, but IIRC it was around the differences between core::fmt::Write and std::io::Write where Printer takes a different Writer than fmt::Display expects. You can write an adapter, but it's ugly.

c.f. https://github.com/lovesegfault/chirp/blob/master/src/memory.rs#L31-L43

sharkdp commented 4 years ago

Thank you for the update!

IIRC it was around the differences between core::fmt::Write and std::io::Write where Printer takes a different Writer than fmt::Display expects.

Oh - you are right. Printer takes a std::io::Write. Formatter implements std::fmt::Write. I had never noticed that there are two versions of this.

If you come back to your project at some point and think that there is something that we can do here, please let us know.