Crate zsh_module

source ·
Expand description

Zsh Module

This is a high level crate that allows you to define your own zsh module.

Getting started

To get started, first, you need to create library, not an executable. Then, change your crate type to "cdylib" on your Cargo.toml:

[lib]
crate-type = ["cdylib"]

Boilerplate

On your lib.rs, you need to put a export_module! macro call, alongside a setup function (can be called whatever you want):

use zsh_module::{ Module, ModuleBuilder }

zsh_module::export_module!(setup);

fn setup() -> Result<Module, ()> {
   todo!()
}

Defining Actions

The main point part of crating a module is implementing Actions. Here’s an example module:

use zsh_module::{ Module, ModuleBuilder, Actions, Result }

zsh_module::export_module!(setup);

struct Greeter;

impl Actions for Greeter {
    fn boot(mut &self) -> Result<()> {
        println!("Hello, everyone!");
        Ok(())
    }
    fn cleanup(&mut self) -> Result<()> {
        println!("Bye, everyone!");
        Ok(())
    }
}

fn setup() -> Result<Module> {
    let module = ModuleBuilder::new(Greeter)
        .build();
    Ok(module)
}

Installing

When your module is ready, copy your shared library to your distribution’s zsh module folder and name it whatever you want, the only requirement is that it ends with your platforms’s dynamic loadable library extension.

On my machine, the zsh module folder is /usr/lib/zsh/<zsh-version>/zsh/.

If everything went fine, you can load it in zsh using the following command:

zmodload zsh/<module-name>

That is it!

Modules

Zsh native log functions. This module contains high level interfaces to the zsh log functions.

Macros

Prints out an error message, like println!
Exports a setup function to be called when the module needs to be set up.
Prints out a warning message, like println!
Prints out a warning message from a command, like println!

Structs

Properties of a zsh builtin command
A zsh module. You must build it using ModuleBuilder
Allows you to build a Module

Traits

This trait allows for defining behaviour to be enacted on parts of the zsh module lifecycle.

Type Definitions

This crate’s error type.
Result<T, Error>

Trait Aliases

This trait corresponds to the function signature of a zsh builtin command handler.