-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proof of concept: Lua plugins #2203
Conversation
Very interesting! I think you are onto something very powerful and useful here. Some high level remarks/questions:
|
I had similar thoughts. I like the idea in general, and have some concerns which I'd like us to discuss before we get too deep:
|
Hm. I was thinking that plugins would be similar to syntaxes or themes. There could be builtin plugins. And in addition, there could be user plugins in
So the main reason why I went for a deep integration with a scripting language as opposed to a "binary interface" is this: I believe that we would quickly want more interaction between
Good point. I didn't think of that. My feeling is that
I hope that Lua is indeed easy to learn and maybe even easier to review. But it's still a very good and valid argument.
Hm, yes. I might be overly sensitive to performance topics. But with all of the great improvements concerning startup time in the recent past, it would be a real shame if we would fire up a python interpreter for each plugin. That would add ~ 20 ms for every plugin.
Another good question. I'd go with including the full source code for a start.
Absolutely. I think we could and should avoid the temporary files completely. The plugins would need some way to directly write to
The
Absolutely. However, instead of passing everything that could be remotely interesting to the plugin, I think it would be cool if the plugins access this information somehow by explicitly asking for it (see the "deeper integration" topic above). The way I thought this would work is that we would expose certain functions via the Rust-Lua FFI. |
@Enselic @keith-hall Do you think this is worth exploring further? Honest opinion please :-) |
My 100% honest opinion is that I don't want to increase the maintenance burden of this project with Lua scripts, or anything else that is not Rust :) I do think that a good plugin-infrastructure has the potential to decrease the long-term maintenance burden of this project, which would be great, but I don't think it should be Lua based. Sorry for being somewhat discouraging, but I interpreted your request for honesty as a sincere request, so I wanted to be honest :) |
Yes, of course. Thank you.
I don't think that adding plugins will ever decrease the maintenance burden. Plugins will - hopefully - allow us to improve the functionality of the project. But maintenance-wise, they will almost certainly increase the effort. If easier maintenance is the only goal of this project, we should not accept any new features (which is also a valid approach). And certainly not a plugin-architecture that allows for a manifold of new features (and bugs). So I'm not sure if it's fair to evaluate this purely from a maintenance-related perspective. First and foremost, I would like to evaluate this from a user-perspective:
Coming back to the maintenance concern: would it help if we move all plugins to separate repositories (similar to how it works for Sublime syntaxes) and let them be maintained independently, by the respective authors? Instead of directly integrating them in our project, we could simply provide a list/registry of plugins somewhere... and users would simply install them by |
What you say make a lot of sense. What it boils down to for me personally is that I don't want to read or write Lua to solve problems in bat. I sloppily framed this as "increased maintenance". But I know very well that no one expects me to read or write Lua, so I would say my concern should not have too much weight here. It was just some spontaneous feedback. Merging this will not prevent me from continue to contribute to other parts of bat, so if that's what we end up doing, that's fine. Thank you for reaching out for feedback and for elaborating on your position. |
Agreed. Is there a way to avoid starting the plugin host on each
I personally worry about then having no control over the "base" set of plugins, which could make it hard to triage bug reports. Maybe having the diagnostic list all the installed plugins, but that could ad a lot of noise. Those concerns aside, I like the idea in general, it definitely helps solve a lot of user requests. But at the same time, I feel those could also be solved with something external to So my "final verdict" : I'm undecided, but I'm happy to go with the flow. Adding plugins will no doubt be an interesting challenge / experience if we go for it. |
I thought about that as well. But that would probably make plugins (a bit?) less powerful. Because the functionality of when to call the plugin would have to be done in the core [bat-plugin]
name = "uncompress"
description = "…"
filetype_filter = "*.gz" That could work quite well for some plugins. But the "directories" and "curl" plugins above, for example, would not fit this pattern. And immediately call for an extension of the config format.
Agreed. We will probably receive lots of falsely targeted bug reports in this repo if we go down that route, similar to bugs in the syntaxes.
True. But requires users to know about two tools. And gives them less control over the specific options passed to
Ok, thank you both for voicing your concerns. I suggest we close this topic for now. Focus on |
Just a note: If this ever gets implemented, the plugin should be able to return multiple file paths for one, such that one can view the contents of files in a compressed directory for example, this would be amazing! I already maintain a script that does lots of this kind of preprocessing, but the pagination often ends up suboptimal because |
This is a proof-of-concept implementation of something that we discussed amongst the maintainers recently: a plugin mechanism for bat. The idea of this PR is to get the discussion started. And to answer a few questions. Most importantly: do we really want/need this?
This whole idea came up because there are a lot of feature requests that keep popping up on
bat
s issue tracker that we previously always declined, mostly because we wanted to keepbat
focused on its core functionality. And because of the additional maintenance overhead:bat <directory>
support (bat the folders #427, "ls" functionality when called with a directory #971, Use bat to view directory #1156, Provide useful output whenbat
is given a directory instead of a file #1940)On the other hand, most of these functionalities are actually quite useful! A lot of workarounds have been suggested. Lots of people wrote their own wrapper functions. And with bat-extras, we even have a dedicated project working on extending
bat
s functionality. The "problem" with all of these solutions is that they are not directly included inbat
, so you need to remember the name of the wrapper scripts. And you need to install them separately.Here, we try to follow a different approach: instead of forcing users to wrap
bat
, we enable users to add functionality to bat by providing a plugin mechanism. We could still maintain those plugins here in this repository. But (1) we could easily argue that those plugins are not part ofbat
s core functionality, especially if all of them would be opt-in (2) it would be easier to maintain this functionality... or to throw it out again. And (3), users could easily customize those plugins or write their own.In this PoC, plugins can be enabled by adding lines like the following to your
bat
config file:Plugins are written in Lua. For now, there is just one type of plugin: a "preprocess" plugin that can modify the input to
bat
. These plugins have to provide apreprocess
function that will be called for every input path tobat
. The plugins then have the option to return a different path instead. This can be used to hand back paths to temporary files with the output of the preprocessing. For example,directories.lua
looks like this:Obviously, this is not a great plugin architecture. It's extremely restrictive. And the interface between
bat
and the plugins is very limited. I could easily imagine a lot of different things that we would like to expose to the plugins. Or functionality that we would like to provide (e.g. to add new command-line options tobat
). But it's somewhat surprising that this simple interface is (up to a few limitations) powerful enough to handle four of the five feature requests listed above:Showcase
gzip support:
CURL support:
bat <directory>
supportPreview binary files
Executable size, Benchmarks
I chose Lua because I heard that it is cheap to embed it into existing applications. And also fast. I have never programmed in Lua until today, so I would have probably have preferred another scripting language personally. But I don't think we want to ship a Python interpreter with
bat
.But obviously, this still comes with some overhead in terms of binary size:
Concerning startup time: the overhead here seems to be pretty much negligible. With three plugins activated and executed (but in the trivial "don't touch this path" mode) and 22 input files passed to
bat
(i.e. 3 x 22 = 66 Lua function invocations), we get a slowdown of less than a millisecond:./bat-master src/*.rs
./bat-2203 src/*.rs