Repository: dtolnay/cargo-expand (language: rust)

Issue:
Is it possible to use a pure rust lib rather than onig? 
Is it possible to use a pure rust lib rather than onig? I'm mostly asking this question so that the answer is documented. When it's not pure rust people can have trouble compiling with gcc on windows on gnu toolchain - it requires more setup and configuration of a windows machine than just installing rustup.

As I said I'm sure there's some good reasons behind using onig versus depending on some parts of ripgrep, just would be nice to know so that I can stop wondering.... :-)


Discussion / hints:
This crate doesn't use onig, it is pulled in through syntect. I am sad about it too. https://github.com/trishume/syntect/issues/12
Seems like soonish there might be a fancy-regex feature: https://github.com/trishume/syntect/pull/270

<sub>Sent with <a href="http://githawk.com">GitHawk</a></sub>
The `fancy-regex` mode was just merged and released as [v4.0.0](https://github.com/trishume/syntect/pull/270)! You may want to try using it to fix this issue, although it does halve performance so you may only want to use it on platforms where it's needed.

Still hoping that onig's build will eventually improve but for now `fancy-regex` is a good option.
Even as a feature that’s helpful. Ideally a bit of the readme can just say for the max-rust version do ‘X’.

<sub>Sent with <a href="http://githawk.com">GitHawk</a></sub>
I've already added such a section to the Readme :)
Thanks - I was thinking of the readme for cargo-expand as that’s where people will look for it initially.

<sub>Sent with <a href="http://githawk.com">GitHawk</a></sub>

Fix patch:
diff --git a/Cargo.lock b/Cargo.lock
index 2e3ea7e..e4b83be 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -71,28 +71,6 @@ version = "1.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
 
-[[package]]
-name = "backtrace"
-version = "0.3.46"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b1e692897359247cc6bb902933361652380af0f1b7651ae5c5013407f30e109e"
-dependencies = [
- "backtrace-sys",
- "cfg-if",
- "libc",
- "rustc-demangle",
-]
-
-[[package]]
-name = "backtrace-sys"
-version = "0.1.36"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "78848718ee1255a2485d1309ad9cdecfc2e7d0362dd11c6829364c6b35ae1bc7"
-dependencies = [
- "cc",
- "libc",
-]
-
 [[package]]
 name = "base64"
 version = "0.10.1"
@@ -108,6 +86,26 @@ version = "0.11.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7"
 
+[[package]]
+name = "bat"
+version = "0.14.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d4f3f3e642dcbd5cba51128d99b54ac75872fd3c417bff1ed1a8d08a0c7bac0d"
+dependencies = [
+ "ansi_colours",
+ "ansi_term 0.12.1",
+ "console",
+ "content_inspector",
+ "encoding",
+ "error-chain",
+ "globset",
+ "semver",
+ "serde",
+ "serde_yaml",
+ "syntect",
+ "unicode-width",
+]
+
 [[package]]
 name = "bincode"
 version = "1.2.1"
@@ -150,6 +148,15 @@ dependencies = [
  "constant_time_eq",
 ]
 
+[[package]]
+name = "bstr"
+version = "0.2.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2889e6d50f394968c8bf4240dc3f2a7eb4680844d27308f798229ac9d4725f41"
+dependencies = [
+ "memchr",
+]
+
 [[package]]
 name = "byteorder"
 version = "1.3.4"
@@ -161,7 +168,7 @@ name = "cargo-expand"
 version = "0.5.0"
 dependencies = [
  "atty",
- "prettyprint",
+ "bat",
  "proc-macro2",
  "quote",
  "serde",
@@ -205,7 +212,7 @@ dependencies = [
  "ansi_term 0.11.0",
  "atty",
  "bitflags",
- "strsim 0.8.0",
+ "strsim",
  "textwrap",
  "unicode-width",
  "vec_map",
@@ -274,76 +281,6 @@ dependencies = [
  "lazy_static",
 ]
 
-[[package]]
-name = "darling"
-version = "0.10.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0d706e75d87e35569db781a9b5e2416cff1236a47ed380831f959382ccd5f858"
-dependencies = [
- "darling_core",
- "darling_macro",
-]
-
-[[package]]
-name = "darling_core"
-version = "0.10.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b"
-dependencies = [
- "fnv",
- "ident_case",
- "proc-macro2",
- "quote",
- "strsim 0.9.3",
- "syn",
-]
-
-[[package]]
-name = "darling_macro"
-version = "0.10.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72"
-dependencies = [
- "darling_core",
- "quote",
- "syn",
-]
-
-[[package]]
-name = "derive_builder"
-version = "0.9.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a2658621297f2cf68762a6f7dc0bb7e1ff2cfd6583daef8ee0fed6f7ec468ec0"
-dependencies = [
- "darling",
- "derive_builder_core",
- "proc-macro2",
- "quote",
- "syn",
-]
-
-[[package]]
-name = "derive_builder_core"
-version = "0.9.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2791ea3e372c8495c0bc2033991d76b512cd799d07491fbd6890124db9458bef"
-dependencies = [
- "darling",
- "proc-macro2",
- "quote",
- "syn",
-]
-
-[[package]]
-name = "directories"
-version = "2.0.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "551a778172a450d7fc12e629ca3b0428d00f6afa9a43da1b630d54604e97371c"
-dependencies = [
- "cfg-if",
- "dirs-sys",
-]
-
 [[package]]
 name = "dirs"
 version = "1.0.5"
@@ -356,16 +293,10 @@ dependencies = [
 ]
 
 [[package]]
-name = "dirs-sys"
-version = "0.3.4"
+name = "dtoa"
+version = "0.4.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "afa0b23de8fd801745c471deffa6e12d248f962c9fd4b4c33787b055599bde7b"
-dependencies = [
- "cfg-if",
- "libc",
- "redox_users",
- "winapi",
-]
+checksum = "4358a9e11b9a09cf52383b451b49a169e8d797b68aa02301ff586d70d9661ea3"
 
 [[package]]
 name = "encode_unicode"
@@ -443,7 +374,6 @@ version = "0.12.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "d371106cc88ffdfb1eabd7111e432da544f16f3e2d7bf1dfe8bf575f1df045cd"
 dependencies = [
- "backtrace",
  "version_check",
 ]
 
@@ -486,6 +416,19 @@ dependencies = [
  "wasi",
 ]
 
+[[package]]
+name = "globset"
+version = "0.4.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7ad1da430bd7281dde2576f44c84cc3f0f7b475e7202cd503042dff01a8c8120"
+dependencies = [
+ "aho-corasick",
+ "bstr",
+ "fnv",
+ "log",
+ "regex",
+]
+
 [[package]]
 name = "heck"
 version = "0.3.1"
@@ -504,12 +447,6 @@ dependencies = [
  "libc",
 ]
 
-[[package]]
-name = "ident_case"
-version = "1.0.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
-
 [[package]]
 name = "indexmap"
 version = "1.3.2"
@@ -552,6 +489,21 @@ dependencies = [
  "safemem",
 ]
 
+[[package]]
+name = "linked-hash-map"
+version = "0.5.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ae91b68aebc4ddb91978b11a1b02ddd8602a05ec19002801c5666000e05e0f83"
+
+[[package]]
+name = "log"
+version = "0.4.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
+dependencies = [
+ "cfg-if",
+]
+
 [[package]]
 name = "memchr"
 version = "2.3.3"
@@ -606,27 +558,6 @@ version = "0.2.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b"
 
-[[package]]
-name = "prettyprint"
-version = "0.8.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6d9764c1ec675e49ff70c4f76f0df91875d57220c4e7c8587d659684e91dd0ac"
-dependencies = [
- "ansi_colours",
- "ansi_term 0.12.1",
- "atty",
- "clap",
- "console",
- "content_inspector",
- "derive_builder",
- "directories",
- "encoding",
- "error-chain",
- "lazy_static",
- "shell-words",
- "syntect",
-]
-
 [[package]]
 name = "proc-macro-error"
 version = "1.0.2"
@@ -768,12 +699,6 @@ dependencies = [
  "crossbeam-utils",
 ]
 
-[[package]]
-name = "rustc-demangle"
-version = "0.1.16"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783"
-
 [[package]]
 name = "ryu"
 version = "1.0.4"
@@ -842,10 +767,16 @@ dependencies = [
 ]
 
 [[package]]
-name = "shell-words"
-version = "0.1.0"
+name = "serde_yaml"
+version = "0.8.11"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "39acde55a154c4cd3ae048ac78cc21c25f3a0145e44111b523279113dce0d94a"
+checksum = "691b17f19fc1ec9d94ec0b5864859290dff279dbd7b03f017afda54eb36c3c35"
+dependencies = [
+ "dtoa",
+ "linked-hash-map",
+ "serde",
+ "yaml-rust",
+]
 
 [[package]]
 name = "strsim"
@@ -853,12 +784,6 @@ version = "0.8.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
 
-[[package]]
-name = "strsim"
-version = "0.9.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c"
-
 [[package]]
 name = "structopt"
 version = "0.3.14"
@@ -933,6 +858,7 @@ dependencies = [
  "serde_derive",
  "serde_json",
  "walkdir",
+ "yaml-rust",
 ]
 
 [[package]]
@@ -1090,3 +1016,12 @@ name = "xml-rs"
 version = "0.8.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "2bb76e5c421bbbeb8924c60c030331b345555024d56261dae8f3e786ed817c23"
+
+[[package]]
+name = "yaml-rust"
+version = "0.4.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "65923dd1784f44da1d2c3dbbc5e822045628c590ba72123e1c73d3c230c4434d"
+dependencies = [
+ "linked-hash-map",
+]
diff --git a/Cargo.toml b/Cargo.toml
index dbd55b7..9ee662e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -23,8 +23,8 @@ termcolor = "1.0"
 toml = "0.5"
 toolchain_find = "0.1"
 
-[dependencies.prettyprint]
-version = "0.8.1"
+[dependencies.bat]
+version = "0.14"
 default-features = false
 features = ["regex-fancy"]
 
diff --git a/src/error.rs b/src/error.rs
index 88b18a6..ca250ce 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -1,11 +1,8 @@
 use std::fmt::{self, Display};
 use std::io;
 
-use prettyprint::PrettyPrintError;
-
 pub enum Error {
     Io(io::Error),
-    Print(PrettyPrintError),
     Toml(toml::ser::Error),
 }
 
@@ -17,12 +14,6 @@ impl From<io::Error> for Error {
     }
 }
 
-impl From<PrettyPrintError> for Error {
-    fn from(error: PrettyPrintError) -> Self {
-        Error::Print(error)
-    }
-}
-
 impl From<toml::ser::Error> for Error {
     fn from(error: toml::ser::Error) -> Self {
         Error::Toml(error)
@@ -35,7 +26,6 @@ impl Display for Error {
 
         match self {
             Io(e) => e.fmt(formatter),
-            Print(e) => e.fmt(formatter),
             Toml(e) => e.fmt(formatter),
         }
     }
diff --git a/src/main.rs b/src/main.rs
index 231323f..cb0af84 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -14,7 +14,8 @@ use std::process::{self, Command, Stdio};
 use std::str::FromStr;
 
 use atty::Stream::{Stderr, Stdout};
-use prettyprint::{PagingMode, PrettyPrinter};
+use bat::assets::HighlightingAssets;
+use bat::PrettyPrinter;
 use quote::quote;
 use structopt::StructOpt;
 use termcolor::{Color::Green, ColorChoice, ColorSpec, StandardStream, WriteColor};
@@ -98,12 +99,7 @@ fn cargo_expand() -> Result<i32> {
     let config = config::deserialize();
 
     if args.themes {
-        for theme in PrettyPrinter::default()
-            .build()
-            .unwrap()
-            .get_themes()
-            .keys()
-        {
+        for theme in HighlightingAssets::from_binary().themes() {
             let _ = writeln!(io::stdout(), "{}", theme);
         }
         return Ok(0);
@@ -206,26 +202,22 @@ fn cargo_expand() -> Result<i32> {
     };
     let _ = writeln!(io::stderr());
     if do_color {
-        if content.ends_with('\n') {
-            // Pretty printer seems to print an extra trailing newline.
-            content.truncate(content.len() - 1);
-        }
-
-        let mut builder = PrettyPrinter::default();
-        builder.header(false);
-        builder.grid(false);
-        builder.line_numbers(false);
-        builder.language("rust");
-        builder.paging_mode(PagingMode::Never);
-
+        let mut pretty_printer = PrettyPrinter::new();
+        pretty_printer
+            .input_from_bytes(content.as_bytes())
+            .language("rust")
+            .tab_width(Some(4))
+            .true_color(false)
+            .header(false)
+            .line_numbers(false)
+            .grid(false)
+            .vcs_modification_markers(false);
         if let Some(theme) = theme {
-            builder.theme(theme);
+            pretty_printer.theme(theme);
         }
 
-        let printer = builder.build().unwrap();
-
         // Ignore any errors.
-        let _ = printer.string(content);
+        let _ = pretty_printer.print();
     } else {
         let _ = write!(io::stdout(), "{}", content);
     }


Test patch: