Repository: sharkdp/bat (language: rust)

Issue:
bat as a library fails to compile with default settings
Hi, I intended to use `bat` as library for a simple program I had in mind so I generated a new Rust project with `cargo new` and added this snippet to my `Cargo.toml` as per the instructions on the Releases page:
```toml
[dependencies]
bat = { version = "0.15", default-features = false }
```
Now, at this point I haven't written any code yet so I ran `cargo build` just to check if everything's fine but I got a build error:
```rustc
error[E0433]: failed to resolve: use of undeclared type or module `regex_impl`
  --> /home/gian/.local/share/cargo/registry/src/github.com-1ecc6299db9ec823/syntect-4.1.1/src/parsing/regex.rs:13:27
   |
13 |     regex: AtomicLazyCell<regex_impl::Regex>,
   |                           ^^^^^^^^^^ use of undeclared type or module `regex_impl`

error[E0433]: failed to resolve: use of undeclared type or module `regex_impl`
  --> /home/gian/.local/share/cargo/registry/src/github.com-1ecc6299db9ec823/syntect-4.1.1/src/parsing/regex.rs:19:13
   |
19 |     region: regex_impl::Region,
   |             ^^^^^^^^^^ use of undeclared type or module `regex_impl`

error[E0433]: failed to resolve: use of undeclared type or module `regex_impl`
  --> /home/gian/.local/share/cargo/registry/src/github.com-1ecc6299db9ec823/syntect-4.1.1/src/parsing/regex.rs:36:9
   |
36 |         regex_impl::Regex::new(regex_str).err()
   |         ^^^^^^^^^^ use of undeclared type or module `regex_impl`

error[E0433]: failed to resolve: use of undeclared type or module `regex_impl`
  --> /home/gian/.local/share/cargo/registry/src/github.com-1ecc6299db9ec823/syntect-4.1.1/src/parsing/regex.rs:65:25
   |
65 |     fn regex(&self) -> &regex_impl::Regex {
   |                         ^^^^^^^^^^ use of undeclared type or module `regex_impl`

error[E0433]: failed to resolve: use of undeclared type or module `regex_impl`
  --> /home/gian/.local/share/cargo/registry/src/github.com-1ecc6299db9ec823/syntect-4.1.1/src/parsing/regex.rs:70:17
   |
70 |                 regex_impl::Regex::new(&self.regex_str).expect("regex string should be pre-tested");
   |                 ^^^^^^^^^^ use of undeclared type or module `regex_impl`

error[E0433]: failed to resolve: use of undeclared type or module `regex_impl`
   --> /home/gian/.local/share/cargo/registry/src/github.com-1ecc6299db9ec823/syntect-4.1.1/src/parsing/regex.rs:116:21
    |
116 |             region: regex_impl::new_region(),
    |                     ^^^^^^^^^^ use of undeclared type or module `regex_impl`

error[E0282]: type annotations needed
  --> /home/gian/.local/share/cargo/registry/src/github.com-1ecc6299db9ec823/syntect-4.1.1/src/parsing/regex.rs:30:20
   |
30 |             regex: AtomicLazyCell::new(),
   |                    ^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`

error[E0282]: type annotations needed
  --> /home/gian/.local/share/cargo/registry/src/github.com-1ecc6299db9ec823/syntect-4.1.1/src/parsing/regex.rs:81:20
   |
81 |             regex: AtomicLazyCell::new(),
   |                    ^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`

error: aborting due to 8 previous errors

Some errors have detailed explanations: E0282, E0433.
For more information about an error, try `rustc --explain E0282`.
error: could not compile `syntect`.
```
Confused about what was going on, I went to inspect `bat`'s `Cargo.toml` and changed my dependencies in my `Cargo.toml` to this instead:
```toml
[dependencies]
bat = { version = "0.15", default-features = false, features = ["regex-onig"] }
```
Now it compiles just fine and trying the Hello World example in `bat`'s documentation works too:
```rust
use bat::PrettyPrinter;

fn main() {
    PrettyPrinter::new()
        .input_from_bytes(b"<span style=\"color: #ff00cc\">Hello world!</span>\n")
        .language("html")
        .print()
        .unwrap();
}
```

<hr>

From the error message, I figured the issue was actually with the `syntect` crate and its `fancy-regex` engine so I created a brand-new project with this in my `Cargo.toml` in hopes I could reproduce the issue:
```toml
[dependencies]
syntect = { version = "4.1", default-features = false, features = ["default-fancy"] }
```
it appears to be working just fine. It compiles without issues so I don't know what makes `bat` fail to compile when `fancy-regex` is used.


Discussion / hints:
Both `regex-onig` and `regex-fancy` work fine. But you have to choose one.

I don't think this is a bug. But we should definitely work on the documentation. Maybe there is a way to set the default to one of these engines. This might collide with the bat application defaults, though.
I understand. Sorry for the noise caused by opening an issue then. I didn't know I was supposed to pick one of the two engines myself. I just pasted the snippet from the Releases page :smile: 
> Sorry for the noise caused by opening an issue then

No.. please do. I have updated the code in the release notes to include `regex-onig`

Fix patch:
diff --git a/Cargo.toml b/Cargo.toml
index 865d1f85ef..66a11adfc4 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -30,6 +30,8 @@ application = [
 ]
 git = ["git2"] # Support indicating git modifications
 paging = ["shell-words"] # Support applying a pager on the output
+
+# You need to use one of these if you depend on bat as a library:
 regex-onig = ["syntect/regex-onig"] # Use the "oniguruma" regex engine
 regex-fancy = ["syntect/regex-fancy"] # Use the rust-only "fancy-regex" engine
 
diff --git a/README.md b/README.md
index 4e953b1c04..8c89b7c068 100644
--- a/README.md
+++ b/README.md
@@ -163,6 +163,7 @@ bat main.cpp | xclip
 export MANPAGER="sh -c 'col -bx | bat -l man -p'"
 man 2 select
 ```
+(replace `bat` by `batcat` if you are on Debian or Ubuntu)
 
 It might also be necessary to set `MANROFFOPT="-c"` if you experience
 formatting problems.
@@ -481,7 +482,7 @@ You can add new (or change existing) file name patterns using the `--map-syntax`
 command line option. The option takes an argument of the form `pattern:syntax` where
 `pattern` is a glob pattern that is matched against the file name and
 the absolute file path. The `syntax` part is the full name of a supported language
-(use `bat --list-languages` for an overview)full .
+(use `bat --list-languages` for an overview).
 
 Note: You probably want to use this option as an entry in `bat`s configuration file instead
 of passing it on the command line (see below).
@@ -499,7 +500,7 @@ Example: To open all files called `.ignore` (exact match) with the "Git Ignore"
 Example: To open all `.conf` files in subfolders of `/etc/apache2` with the "Apache Conf"
 syntax, use (this mapping is already built in):
 ```bash
--map-syntax='/etc/apache2/**/*.conf:Apache Conf'
+--map-syntax='/etc/apache2/**/*.conf:Apache Conf'
 ```
 
 ### Using a different pager
@@ -691,6 +692,11 @@ bash assets/create.sh
 cargo install --path . --locked --force
 ```
 
+If you want to build an application that uses `bat`s pretty-printing
+features as a library, check out the [the API documentation](https://docs.rs/bat/).
+Note that you have to use either `regex-onig` or `regex-fancy` as a feature
+when you depend on `bat` as a library.
+
 ## Contributing
 
 Take a look at the [`CONTRIBUTING.md`](CONTRIBUTING.md) guide.
diff --git a/assets/manual/bat.1.in b/assets/manual/bat.1.in
index 65a17fe130..eaa44c4faa 100644
--- a/assets/manual/bat.1.in
+++ b/assets/manual/bat.1.in
@@ -40,7 +40,7 @@ language names and file extensions.
 .HP
 \fB\-H\fR, \fB\-\-highlight\-line\fR <N:M>...
 .IP
-Highlight the specified line ranges with a different background color For example:
+Highlight the specified line ranges with a different background color. For example:
 .RS
 .IP "\-\-highlight\-line 40"
 highlights line 40
@@ -52,6 +52,18 @@ highlights lines 1 to 40
 highlights lines 40 to the end of the file
 .RE
 .HP
+\fB\-\-file\-name\fR <name>...
+.IP
+Specify the name to display for a file. Useful when piping data to {{PROJECT_EXECUTABLE}} from STDIN when {{PROJECT_EXECUTABLE}} does not otherwise know the filename. Note that the provided file name is also used for syntax detection.
+.HP
+\fB\-d\fR, \fB\-\-diff\fR
+.IP
+Only show lines that have been added/removed/modified with respect to the Git index. Use '\-\-diff\-context=N' to control how much context you want to see.
+.HP
+\fB\-\-diff\-context\fR <N>...
+.IP
+Include N lines of context around added/removed/modified lines when using '\-\-diff'.
+.HP
 \fB\-\-tabs\fR <T>
 .IP
 Set the tab width to T spaces. Use a width of 0 to pass tabs through directly
@@ -111,6 +123,7 @@ Map a glob pattern to an existing syntax name. The glob pattern is matched on th
 path and the filename. For example, to highlight *.build files with the Python syntax,
 use -m '*.build:Python'. To highlight files named '.myignore' with the Git Ignore
 syntax, use -m '.myignore:Git Ignore'.
+Note that the right-hand side is the *name* of the syntax, not a file extension.
 .HP
 \fB\-\-theme\fR <theme>
 .IP
diff --git a/src/bin/bat/clap_app.rs b/src/bin/bat/clap_app.rs
index 87cb2a65e8..ea8efd89cc 100644
--- a/src/bin/bat/clap_app.rs
+++ b/src/bin/bat/clap_app.rs
@@ -328,7 +328,8 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
                     "Map a glob pattern to an existing syntax name. The glob pattern is matched \
                      on the full path and the filename. For example, to highlight *.build files \
                      with the Python syntax, use -m '*.build:Python'. To highlight files named \
-                     '.myignore' with the Git Ignore syntax, use -m '.myignore:Git Ignore'.",
+                     '.myignore' with the Git Ignore syntax, use -m '.myignore:Git Ignore'. Note \
+                     that the right-hand side is the *name* of the syntax, not a file extension.",
                 )
                 .takes_value(true),
         )


Test patch: