|
| 1 | +.. _plugin_system: |
| 2 | + |
| 3 | +============= |
| 4 | +Plugin System |
| 5 | +============= |
| 6 | + |
| 7 | +The plugin system allows users to customize and extend different aspects of |
| 8 | +tmuxp without the need to change tmuxp itself. |
| 9 | + |
| 10 | + |
| 11 | +Using a Plugin |
| 12 | +-------------- |
| 13 | + |
| 14 | +To use a plugin, install it in your local python environment and add it to |
| 15 | +your tmuxp configuration file. |
| 16 | + |
| 17 | +Example Configurations |
| 18 | +^^^^^^^^^^^^^^^^^^^^^^ |
| 19 | +YAML |
| 20 | +~~~~ |
| 21 | + |
| 22 | +.. literalinclude:: ../examples/plugin-system.yaml |
| 23 | + :language: yaml |
| 24 | + |
| 25 | +JSON |
| 26 | +~~~~ |
| 27 | + |
| 28 | +.. literalinclude:: ../examples/plugin-system.json |
| 29 | + :language: json |
| 30 | + |
| 31 | +.. _poetry: https://python-poetry.org/ |
| 32 | + |
| 33 | + |
| 34 | +Developing a Plugin |
| 35 | +------------------- |
| 36 | + |
| 37 | +tmuxp expects all plugins to be class within a python submodule named |
| 38 | +``plugin`` that is within a python module that is installed in the local |
| 39 | +python environment. A plugin interface is provided by tmuxp to inherit. |
| 40 | + |
| 41 | +`poetry`_ is the chosen python package manager for tmuxp. It is highly |
| 42 | +suggested to use it when developing plugins; however, ``pip`` will work |
| 43 | +just as well. Only one of the configuration files is needed for the packaging |
| 44 | +tool that the package developer desides to use. |
| 45 | + |
| 46 | +.. code-block:: bash |
| 47 | +
|
| 48 | + python_module |
| 49 | + ├── tmuxp_plugin_my_plugin_module |
| 50 | + │ ├── __init__.py |
| 51 | + │ └── plugin.py |
| 52 | + ├── pyproject.toml # Poetry's module configuration file |
| 53 | + └── setup.py # pip's module configuration file |
| 54 | +
|
| 55 | +
|
| 56 | +When publishing plugins to pypi, tmuxp advocates for standardized naming: |
| 57 | +``tmuxp-plugin-{your-plugin-name}`` to allow for easier searching. To create a |
| 58 | +module configuration file with poetry, run ``poetry init`` in the module |
| 59 | +directory. The resulting file looks something like this: |
| 60 | + |
| 61 | +.. code-block:: toml |
| 62 | +
|
| 63 | + [tool.poetry] |
| 64 | + name = "tmuxp-plugin-my-tmuxp-plugin" |
| 65 | + version = "0.0.2" |
| 66 | + description = "An example tmuxp plugin." |
| 67 | + authors = ["Author Name <author.name@<domain>.com>"] |
| 68 | +
|
| 69 | + [tool.poetry.dependencies] |
| 70 | + python = "~2.7 || ^3.5" |
| 71 | + tmuxp = "^1.6.0" |
| 72 | +
|
| 73 | + [tool.poetry.dev-dependencies] |
| 74 | +
|
| 75 | + [build-system] |
| 76 | + requires = ["poetry>=0.12"] |
| 77 | + build-backend = "poetry.masonry.api" |
| 78 | +
|
| 79 | +
|
| 80 | +The `plugin.py` file could contain something like the following: |
| 81 | + |
| 82 | +.. code-block:: python |
| 83 | + |
| 84 | + from tmuxp.plugin import TmuxpPlugin |
| 85 | + import datetime |
| 86 | +
|
| 87 | + class MyTmuxpPlugin(TmuxpPlugin): |
| 88 | + def __init__(self): |
| 89 | + """ |
| 90 | + Initialize my custom plugin. |
| 91 | + """ |
| 92 | + # Optional version dependency configuration. See Plugin API docs |
| 93 | + # for all supported config parameters |
| 94 | + config = { |
| 95 | + 'tmuxp_min_version' = '1.6.2' |
| 96 | + } |
| 97 | +
|
| 98 | + TmuxpPlugin.__init__( |
| 99 | + self, |
| 100 | + plugin_name='tmuxp-plugin-my-tmuxp-plugin', |
| 101 | + **config |
| 102 | + ) |
| 103 | +
|
| 104 | + def before_workspace_builder(self, session): |
| 105 | + session.rename_session('my-new-session-name') |
| 106 | +
|
| 107 | + def reattach(self, session): |
| 108 | + now = datetime.datetime.now().strftime('%Y-%m-%d') |
| 109 | + session.rename_session('session_{}'.format(now)) |
| 110 | +
|
| 111 | +
|
| 112 | +Once this plugin is installed in the local python environment, it can be used |
| 113 | +in a configuration file like the following: |
| 114 | + |
| 115 | +.. code-block:: yaml |
| 116 | +
|
| 117 | + session_name: plugin example |
| 118 | + plugins: |
| 119 | + - my_plugin_module.plugin.MyTmuxpPlugin |
| 120 | + # ... the rest of your config |
| 121 | +
|
| 122 | +
|
| 123 | +Plugin API |
| 124 | +---------- |
| 125 | + |
| 126 | +.. automethod:: tmuxp.plugin.TmuxpPlugin.before_workspace_builder |
| 127 | +.. automethod:: tmuxp.plugin.TmuxpPlugin.on_window_create |
| 128 | +.. automethod:: tmuxp.plugin.TmuxpPlugin.after_window_finished |
| 129 | +.. automethod:: tmuxp.plugin.TmuxpPlugin.before_script |
| 130 | +.. automethod:: tmuxp.plugin.TmuxpPlugin.reattach |
0 commit comments