Macro format_function_plugin

Source
macro_rules! format_function_plugin {
    ($name:expr, |$arg:ident| $body:block) => { ... };
}
Expand description

Defines a new format function.

This macro takes two arguments: The name of the function (as a null-terminated byte string), and the function body itself. The function recives a &CStr. as an argument, and should return a type that implements AsRef<CStr>, such as CStr or CString.

This function will then be usable in tmux format strings by surrounding it with a pair of colons. For instance, if you define a format function that reverses it’s input named reverse, you can use it as #{:reverse:session_name} to display the builtin tmux session_name variable backwards.

For example:

use tmux_plugin::format_function_plugin;
use std::ffi::CString;

format_function_plugin!(b"trim\0", |arg| {
    match arg.to_str() {
      Ok(string) => {
        CString::new(string.trim().to_owned())
            .expect("Does not contain null bytes, as the source was a valid CString")
      },
      Err(_) => CString::new("Invalid UTF-8 in input").unwrap(),
    }
});