Giter Site home page Giter Site logo

Provide examples about hexyl HOT 5 CLOSED

sharkdp avatar sharkdp commented on August 25, 2024
Provide examples

from hexyl.

Comments (5)

sharkdp avatar sharkdp commented on August 25, 2024 1

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:

use std::io;
use hexyl::{BorderStyle, Printer};
fn main() {
let input = vec![
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44,
0x52, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x44, 0x08, 0x02, 0x00, 0x00, 0x00,
];
let stdout = io::stdout();
let mut handle = stdout.lock();
let show_color = true;
let use_squeezing = false;
let border_style = BorderStyle::Unicode;
let mut printer = Printer::new(&mut handle, show_color, border_style, use_squeezing);
printer.print_all(&input[..], None).unwrap();
}

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.

from hexyl.

sharkdp avatar sharkdp commented on August 25, 2024 1

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.

from hexyl.

lovesegfault avatar lovesegfault commented on August 25, 2024

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.

from hexyl.

sharkdp avatar sharkdp commented on August 25, 2024

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

from hexyl.

lovesegfault avatar lovesegfault commented on August 25, 2024

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

from hexyl.

Related Issues (20)

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.