Skip to content

Menu Configuration

The load function takes a dict of { "action": [], "menu": [] }.

import menuet
from menuet.builders.text import Render, TextMenuBuilder

model = menuet.Model()
load(
    {
        "menu": [
            {
                "label": "Sub-Menu",
                "menu": ["My App"],
                "group": "Separator",
            },
        ],
        "action": [
            {
                "id": "open-gui",
                "label": "Open GUI",
                "cb": "import myapp; myapp.open_gui()",
                "menu": ["My App", "Sub-Menu"],
            },
            {
                "id": "print-hello",
                "label":  "Print Hello",
                "cb": 'print("Hello")',
            },
        ],
    },
    model,
)

builder = TextMenuBuilder(model, root_menu="Example", render=Render.UTF8)
print(builder.build())
Example
├── My App
│   ├── Separator ───
│   └── Sub-Menu
│       └── Open GUI
└── Print Hello

Action Options

The [[action]] tables accepts the following options:

[[action]]                   # `action` is an array of action tables
id = "my-action"             # unique identifier [required]
label = "My Action"          # display name
menu = ["Menu", "Sub-Menu"]  # parent menus hierarchy
group = "My Group"           # group related menus and actions
cb = "print('Hello !')"      # action callback
desc = "Print Hello"         # tooltip
icon = "icons/my-icon.png"   # path to an icon

The [[menu]] tables accepts the following options:

[[menu]]                    # `menu` is an array of menu tables
label = "My Menu"           # display name [required]
desc = "My App Scripts"     # tooltip
menu = ["Parent Menu"]      # parent menus hierarchy
group = "My Group"          # group related menus and actions
icon = "icons/my-icon.png"  # path to an icon

Menus defined in [[action]] tables are created automatically. The only reason to configure [[menu]] explicitly is to set an icon, a group, or a desc.

icon Schemes

The icon option accepts a value in the format <path> or <scheme>:<value>. The following schemes are available:

  • path (default): takes a path to an icon file.

    icon = "icons/settings.png"
    
    icon = "path:icons/settings.png"
    
  • res: load a resource file. Value is in the form res:importable.module:file.ext.

    icon = "res:myapp.data:logo.svg" 
    

cb Schemes

The cb option accepts a value in the format <script> or <scheme>:<value>. The following schemes are available:

  • exec (default): takes a Python script.

    cb = "print('Hello !')"
    
    cb = "exec:import myapp; myapp.open_gui()"
    
    cb = """\
    from importlib.metadata import version
    print(version("myapp"))
    """
    
    cb = """exec:\
    import myapp
    myapp.open_gui()
    """
    
  • ep: loads an entry point. Value is in the form ep:importable.module:callable.

    cb = "res:myapp.my_module:open_gui"
    
    cb = "res:myapp:my_function"
    
  • copy: copy its value to the clipboard.

    cb = "copy:Lorem ipsum dolor sit amet"
    

    Warning

    The copy scheme requires the copykitten package, available as an extra:

    pip install menuet[copy]
    
  • url: takes a URL and open it in the default browser.

    cb = "url:https://codeberg.org/tahv/menuet"
    

JSON Schema

Menuet provides its own JSON Schema. If your editor supports TOML schema validation, it's recommended to set it up to enable validation diagnostics and auto-complete when editing a menu file:

Both tombi and taplo supports the #:schema directive to specify the schema to use for the document.

Add the comment directive at the beginning of the document, followed by a blank line.

menu.toml
#:schema https://tahv.codeberg.page/menuet/menuet.json

Sub Schema

Tombi can be configured with sub schema to apply a schema to a specific part of the TOML document.

tombi.toml
[[schemas]]
root = "tool.my-menu"
path = "https://tahv.codeberg.page/menuet/menuet.json"
include = ["pyproject.toml"]