Skip to content

Conversation

@acheronfail
Copy link
Contributor

This PR allows crates using bat as a library to configure the show_nonprintable setting via the PrettyPrinter API.

Note that it is possible to use bat as a lib and configure this, but the workaround requires using bat's "inner" API which is discouraged by bat's documentation.

Current workaround:

let mut config = bat::config::Config::default();
config.show_nonprintable = true;
let assets = bat::assets::HighlightingAssets::from_binary();
let ctrl = bat::controller::Controller::new(&config, &assets);
let inp = bat::input::Input::from(bat::Input::from_bytes(&output));
ctrl.run(vec![inp]).unwrap();

Once this PR is merged, consumers will be able to do this:

bat::PrettyPrinter::new()
    .input_from_bytes(&output)
    .show_nonprintable(true)
    .print()
    .expect("Failed to print to STDOUT");

Something else noteworthy: if using bat as a library, if binary content is detected then the PrettyPrinter will print:

[bat warning]: Binary content from READER will not be printed to the terminal (but will be present if the output of 'bat' is piped). You can use 'bat -A' to show the binary file contents.

I'm not sure if this has been discussed previously, but I imagine it could cause some surprise for crates which depend on bat since the -A flag obviously isn't available, etc.

@Enselic
Copy link
Collaborator

Enselic commented Apr 2, 2022

Thank you! Looks good to me. Would you mind adding an entry about it to CHANGELOG.md please?

@acheronfail
Copy link
Contributor Author

No worries at all! And I've added an entry to the changelog. 👍

Co-authored-by: Martin Nordholts <[email protected]>
Copy link
Owner

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@sharkdp
Copy link
Owner

sharkdp commented Apr 2, 2022

Something else noteworthy: if using bat as a library, if binary content is detected then the PrettyPrinter will print:

[bat warning]: Binary content from READER will not be printed to the terminal (but will be present if the output of 'bat' is piped). You can use 'bat -A' to show the binary file contents.

I don't think this has been discussed, no. I agree that this should be improved. The "content from READER" part isn't great either 😄. It would be good if you could open a new ticket for this. Thanks!

@sharkdp sharkdp merged commit 33a9849 into sharkdp:master Apr 2, 2022
@acheronfail
Copy link
Contributor Author

No worries, I've done so here: #2146

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants