This page describes some common tasks and snippets that can be used for the plugin development. Snippets are written in Rust, but the concepts should be transferable to other languages as well. All examples are based on the rust-plugin-example.
Non selectable plugins with permissions
If a plugin (like a statusbar) should be non selectable, zellij-tile
provides the function set_selectable()
. When this function is directly called on plugin
initialization, a user won’t be able to grant plugin permissions since the plugin cannot be
selected. Therefore the plugin should call set_selectable(false)
after receiving the PermissionRequestResult
.
The following code demonstrates the feature. Notice, that the plugin must subscribe to the
PermissionRequestResult
to receive it in the load()
function. Then the event must be handled
in the update()
function.
|
|
Queuing events until PermissionsRequestResult is received
Hence Zellij communicates via events with the plugin, a plugin needs to ensure that dependencies between these
events, that are relevant for the logic of the plugin, are handled correctly. For example, when a plugin is started
a second time, it will receive a few events (like ModeUpdate
) before the PermissionRequestResult
event is received.
If a plugin calls a function as soon as the ModeUpdate
event is received, that requires permissions, the function
call would fail. Therefore the plugin needs to hold back events until the PermissionRequestResult
is received.
The following code demonstrates how events can be stored into a queue, until the PermissionRequestResult
is received.
A Vec<Event>
is used in the State
struct for storing the events. Event handling is extracted into a separate function,
such that it can be called in several places in the update()
function. If PermissionRequestResult
is not received,
events will be pushed into the vector. As soon as the PermissionRequestResult
is received, the vector will be emptied
and all events will be processed sequentially.
|
|
Advanced logging & Tracing
Tracing is a form of logging, which could help to trace calls through certain functions and files of the code.
There is a tracing
and a tracing-subscriber
, which can be utilized to implement tracing within Zellij plugins.
First add these two crates with cargo add tracing tracing-subscriber
.
Next add a function, that initializes the tracing.
|
|
Call this function at the beginning of the load()
function of your plugin. It will initialize the tracing crate and
configure it, such that it will write into a log file, where the plugin is started. Then you can use the tracing crate, similarly to
the log crate.
|
|
Additionally, functions can be instrumented with tracing. For more details, please visit the documentation of the tracing crate at https://docs.rs/tracing/latest/tracing/.
|
|