Skip to content

Conversation

sharkdp
Copy link
Owner

@sharkdp sharkdp commented Apr 22, 2020

This is a major update of "bat as a library". I have implemented a new PrettyPrinter struct which is a builder-like wrapper to configure and control the pretty-printing process. I tried to make it similar to prettyprint::PrettyPrinter by @mre, but it will not be a drop-in replacement, unfortunately.

Major new functionality: the bat library can now accept arbitrary Readers as input. This also includes input from a (byte) string.

Here are a few examples of how the library can be used (see examples folder for more):

simple.rs

/// A simple program that prints its own source code using the bat library
use bat::PrettyPrinter;

fn main() {
    PrettyPrinter::new().input_file(file!()).print().unwrap();
}

image

cat.rs

/// A very simple colorized `cat` clone, using `bat` as a library.
/// See `src/bin/bat` for the full `bat` application.
use bat::PrettyPrinter;

fn main() {
    PrettyPrinter::new()
        .header(true)
        .grid(true)
        .line_numbers(true)
        .input_files(std::env::args_os().skip(1))
        .print()
        .unwrap();
}

image

yaml.rs

/// A program that serializes a Rust structure to YAML and pretty-prints the result
use bat::PrettyPrinter;
use serde::Serialize;

#[derive(Serialize)]
struct Person {
    name: String,
    height: f64,
    adult: bool,
    children: Vec<Person>,
}

fn main() {
    let person = Person {
        name: String::from("Anne Mustermann"),
        height: 1.76f64,
        adult: true,
        children: vec![Person {
            name: String::from("Max Mustermann"),
            height: 1.32f64,
            adult: false,
            children: vec![],
        }],
    };

    let bytes = serde_yaml::to_vec(&person).unwrap();
    PrettyPrinter::new()
        .language("yaml")
        .line_numbers(true)
        .input_from_bytes(&bytes)
        .print()
        .unwrap();
}

image

advanced.rs

/// A program that prints its own source code using the bat library
use bat::{PagingMode, PrettyPrinter, WrappingMode};

fn main() {
    PrettyPrinter::new()
        .header(true)
        .grid(true)
        .line_numbers(true)
        .use_italics(true)
        // The following line will be highlighted in the output:
        .highlight(line!() as usize)
        .theme("1337")
        .wrapping_mode(WrappingMode::Character)
        .paging_mode(PagingMode::QuitIfOneScreen)
        .input_file(file!())
        .print()
        .unwrap();
}

image

closes #884
closes #937
closes #729
closes #730

@sharkdp sharkdp changed the title PrettyPrint builder, Custom Reader input, bugfixes, refactoring PrettyPrint builder, custom 'Read' input, bugfixes, refactoring Apr 22, 2020
@sharkdp sharkdp changed the title PrettyPrint builder, custom 'Read' input, bugfixes, refactoring PrettyPrint wrapper, custom 'Read' input, bugfixes, major refactoring Apr 22, 2020
@sharkdp
Copy link
Owner Author

sharkdp commented Apr 22, 2020

@dandavison Would an API like this work for delta?

@sharkdp sharkdp added this to the v0.14 milestone Apr 22, 2020
@sharkdp sharkdp merged commit 5d71056 into master Apr 22, 2020
@sharkdp sharkdp deleted the prettyprint-builder branch April 22, 2020 21:55
@dandavison
Copy link
Contributor

@dandavison Would an API like this work for delta?

This looks great! However, regarding delta, the only things it uses from bat are the pager spawning code (output::OutputType), the HighlightingAssets, and the to_ansi_color function (see comment for details of delta's usage of bat). I don't think delta could use a pretty printer from bat since what delta has to do is fairly involved (gathering up removed and added lines separately from a diff hunk, running a string alignment algorithm to detect the within-line highlight regions, superimposing foreground color extents from syntect with its own background color extents, etc).

@sharkdp
Copy link
Owner Author

sharkdp commented Apr 22, 2020

I see. Than you for the clarification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants