Since this is a fairly simple plugin (just reading the output of a process and doing some commands and such based on its output), to create a plugin doing this, I’d almost certainly use Lua instead of an RPC plugin:
Lua color-changing script
local Job = require('plenary.job')
local timer = vim.loop.new_timer()
timer:start(1000, 1000, vim.schedule_wrap(function()
local output
local job = Job:new {
command = 'defaults',
args = {'read', '-g', 'AppleInterfaceStyle'},
on_stdout = function(_, data)
output = data
end
}
job:sync()
if output == 'Dark' then
vim.cmd [[ colorscheme gruvbox-material ]]
else
vim.cmd [[ colorscheme elflord ]]
end
end))
The above should be fairly self-explanatory, but just in case it isn’t, here are a few things to note. I’ve used vim.loop
to create a timer, which you can find the docs for here: luv/docs.md at master · luvit/luv · GitHub (see also :help vim.loop
). In addition, the above uses plenary.nvim for the nice job API, which afaik is just a wrapper over vim.loop
's process capabilities.
Running the above via :luafile %
will change your colorscheme to gruvbox-material or elflord (assuming you have those installed) depending on whether your system theme is light or dark (I tested it and seemed to work fine).
Note that you could add more to this, perhaps wrapping it in a function that allows you to pass the colorschemes, and that returns the timer so you can stop it; or maybe you could read from global variables to get the colorschemes, so you can change them on the fly; etc.
However, just because I’d personally do this in Lua because of how much easier it is to setup, it definitely can be done in an RPC plugin. I’ve written a quick Rust example to do almost the exact same thing as the above Lua script, just with the extra step of having to run :lua require'colorscheme_changer'.run()
to start it: GitHub - smolck/nvim-rpc-plugin-example: Quick colorscheme changing example
You asked some questions regarding RPC plugins/how they work, and hopefully poking around in that repo (specifically the lua/
directory) will give you the information you want. I also have a spotify plugin I created/am working on that is another, perhaps more instructive, example which enables you to play Spotify tracks via Telescope and also gives a couple other commands. It’s a WIP, will hopefully improve soon, but shows the basic idea of how to setup an RPC plugin.
See also GitHub - tjdevries/rofl.nvim: Rust On the FLY completion for neovim, which is where I got most of the Lua code to start a Rust plugin over RPC, and the examples in nvim-rs’s repo, if you’re interested in the Rust side of things.
Note that one of the downsides of using, say, a Rust plugin is the compilation step. The user has to go to the installed plugin’s directory and run cargo build --release
in the case of all of the plugins I linked, which while it works, isn’t ideal (this also means they have to have the Rust toolchain installed, another downside).
As for best practices, I’m not sure there really are any. It’s kind of up to you to organize things as you like. Perhaps there are some though that I’m not aware of/not thinking of.