Giter Site home page Giter Site logo

prettyprint's Introduction

prettyprint

Documentation CI

Syntax highlighting library with batteries included.

โš ๏ธ For new projects, you might want to use bat instead, which can be used as a library now. prettyprint is in maintenance mode, which means it will not see any major updates anymore, but pull requests will still be reviewed and dependencies will be kept up-to-date for the time being. You might want to consider migrating away eventually, though.

Quick start

Screenshot

The above output was created with the following code:

let printer = PrettyPrinter::default()
    .language("rust")
    .build()?;

printer.file("fixtures/fib.rs")?;

Note that prettyprint is a builder and can be customized. For example, if you don't like the grid or the header, you can disable those:

let printer = PrettyPrinter::default()
    .header(false)
    .grid(false)
    .language("ruby")
    .build()?;

let example = r#"
def fib(n)        
    return 1 if n <= 1
    fib(n-1) + fib(n-2)
end
"#;
printer.string_with_header(example, "fib.rb")?;

"What!? It can also print strings, Matthias? That's insane."
It's true. You're welcome.

Installation

Add this to your Cargo.toml:

prettyprint = "*"

But why?

syntect is a great package for highlighting text.
When writing a command-line tool that prints text however, you might be looking for some additional functionality.

  • Line numbers
  • More built-in color-themes
  • Automatic pagination
  • Proper terminal handling
  • Showing non-printable characters
  • File headers
  • Windows support

prettyprint offers all of this in one crate (thanks to bat).

Known limitations

  • Doesn't run on no-std targets. I don't plan to support those.

Credits

prettyprint is simply a fork of bat, with some functionality stripped out and bundled up as a library. I built it, because I needed it for cargo-inspect. All credits go to the original authors.

prettyprint's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar drsensor avatar dtolnay avatar mitsuhiko avatar mre avatar zzau13 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

prettyprint's Issues

Wrapping

The internals have an OutputWrap struct but it's not exposed publicly. Is that intentional?

Refactor builder pattern

Hi, notice that the builder pattern is a bit daunting when I want to print 2 different things.

For example, I need to create 2 instances for printing 2 things (partial result + error messages):
let printer = PrettyPrinter::default()
    .header(false).grid(false)
    .paging_mode(PagingMode::Never)
    .language("erlang").build()?;
let eprinter = PrettyPrinter::default()
    .header(false).grid(false)
    .paging_mode(PagingMode::Error)
    .language("rust").build()?;

if !error {
    printer.string(partial_result);
} else {
    eprinter.string_with_header(error_messages, filename);
}

I'm not sure about ๐Ÿ‘† but it probably can cause memory spike ๐Ÿค” (because there is 2 instances)

I'm thinking to refactor it so that it can switch the configuration without doing instantiation.

Something akin to this:
let printer = PrettyPrinter::default()
    .header(false).grid(false)
    .paging_mode(PagingMode::Never)
    .language("erlang").build()?;

if !error {
    printer.string(partial_result);
} else {
    printer.configure().paging_mode(PagingMode::Error).language("rust"); // this will change the configuration
    printer.string_with_header(error_messages, filename);
} // ๐Ÿ‘ˆ maybe when out of scope, printer configuration will revert back
  // (which mean `PrettyPrint` will implement trait `Drop`)

However, is it possible to do it without introducing breaking changes?

Cannot compile on macOS

This crate can't compile on stock macOSยน due to inheriting a dependency on OpenSSL from the git2 crate. I expect you have no need for networking with git, so turning off the default features on the git2 crate will fix this.

ยนInstalling OpenSSL via Homebrew may work around this, but I don't use Homebrew so I can't check.

prettyprint should have option for light terminal

On macOS, the default terminal is black text on a white background (and since I have difficulty seeing red-on-black, I prefer to keep it that way). As a result, when cargo-expand uses prettyprint, certain syntax elements become invisible white-on-white text that renders the output unusable.

screen shot 2019-01-24 at 8 32 08 am

Options

  1. Somehow detect the terminal background color and choose a contrasting color for primary text. Doing this in a cross-platform way might be difficult.
  2. Add an option for callers to tell prettyprint to use a light theme vs a dark theme. This would require cargo-expand and others to expose a command-line flag of their own to choose light vs dark, but it would avoid reading anything from the terminal.

Add `examples` folder

There should be an examples folder for showing different features of the library.
This would help people get up to speed quickly and show off some of the more interesting features of the library such as the soon to be added stderr support (#9).

If anyone wants to contribute a usage example, feel free to send a pull request. ๐Ÿ˜Š

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.