sharkdp / bat

A cat(1) clone with wings.
Apache License 2.0
49.35k stars 1.25k forks source link

Have library use also find locally defined language #2441

Open carueda opened 1 year ago

carueda commented 1 year ago

I've been a happy user of the bat command line program for a good while (thanks!), including its use with a custom language ("TethysL") for which I defined a sublime spec according to the readme. In particular, bat --list-languages includes an entry for such language, and rendering has been all great and as expected.

Now, I just started experimenting with bat in library mode and was hoping that such use would also "discover" my local configuration for the custom language. However, I get unknown syntax: 'TethysL'. More details below.

Perhaps library use only considers syntaxes embedded in it by default and I missing a way to instruct the library to also look into the location displayed by "$(bat --config-dir)/syntaxes")? Thanks!


Info related to using bat as program:

$ bat --version
bat 0.22.1
$ head  "$(bat --config-dir)/syntaxes/sublime-tethysl-syntax/tethysl.sublime-syntax"
%YAML 1.2
---
name: TethysL
file_extensions:
  - tl
  - tethysl
first_line_match: ^#!\s*/.*\btethysl(\d(\.\d)?)?\b
scope: source.tethysl

variables:
$ bat --list-languages | rg TethysL
TethysL:tl,tethysl

Info related to using bat as library:

[dependencies]
bat = { version = "0.22.1", default-features = false, features = ["regex-onig"] }

When running:

    let src = ...;
    PrettyPrinter::new()
        .language("TethysL")
        .input_from_bytes(src.as_bytes())
        .print()
        .unwrap();

I get

[bat error]: unknown syntax: 'TethysL'
carueda commented 1 year ago

Per a quick inspection, it does seem like library use (as exercised via bat::PrettyPrinter) only considers the embedded syntaxes:

keith-hall commented 1 year ago

Sorry for the late reply here, I'm no expert on bat internals :)

Per a quick inspection, it does seem like library use (as exercised via bat::PrettyPrinter) only considers the embedded syntaxes:

* https://github.com/sharkdp/bat/blob/3d7817d662446ed03a95dfb404770a4ff8df135c/src/pretty_printer.rs#L54-L57

* https://github.com/sharkdp/bat/blob/3d7817d662446ed03a95dfb404770a4ff8df135c/src/pretty_printer.rs#L248-L252

I actually interpret this comment to mean, that the assets.get_syntaxes() call will never fail because at a minimum, it always uses the assets from the binary. So there will always be at least one syntax, whether or not the "cache" folder is used.

But yes, it does seem like an unexpected omission that PrettyPrinter will only ever use the assets from the binary. This confused me, but after some investigation, it seems (unless I'm mistaken) bat the command line application isn't using PrettyPrinter. Which kinda explains it :) I think that all that needs to be fixed is to add to the bat PrettyPrinter public API a way to instantiate it with passing a cache directory. But presumably as the command line side isn't using this struct, then other apps relying on bat as a library could achieve the same thing without it too...

carueda commented 1 year ago

Thanks for the note. Right. In my project I found a way to instruct the library to use my locally configured language. So, strictly speaking this is possible already. However, I understand that PrettyPrinter is intended to be the API, but this use case is not covered, and that's why I decided to enter this ticket. Thanks.