mods

package
v2.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2026 License: MIT Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mods runs plugin mods (docs/mods.md) inside the terminal console.

Every mod gets a goja runtime of its own, which is its sandbox: the runtime has no require, file system, timers or network, only the $ object. A goja runtime is not goroutine-safe, so each one is owned by a loop goroutine and everything that touches it — a handler call, a promise settling, a button press — is a job queued on that loop. The chain of one event crosses runtimes through Go: next(e) in one mod resolves on another goroutine and comes back to the caller's loop as a job.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Denied

func Denied(result any) (string, bool)

Denied reads a {deny: "reason"} answer.

func Signature

func Signature(list []Mod) string

Signature identifies a mod list, so a poller can tell whether anything changed without comparing the scripts.

Types

type API

type API interface {
	JSON(method, path string, body any) ([]byte, error)
}

API is the part of console.Client the mods' $.api uses. It is called as the person running the console.

type Command

type Command struct {
	Mod   string // Mod.Key()
	Name  string // the mod's display name
	ID    string
	Title string
}

Command is a palette command a mod registered with $.command.register.

type Element

type Element struct {
	Type      string // Box, Text, Badge, Button, Link
	Text      string // Text, Badge
	Tone      string // dim, accent, warn, danger, ok
	Bold      bool
	Mono      bool
	Label     string // Button, Link
	Href      string // Link
	Hotkey    string // Button
	Direction string // Box: row or column
	Gap       int
	Children  []Element
	// contains filtered or unexported fields
}

Element is a mod element ($.ui.resolve) as the console draws it. Props a terminal cannot show are dropped.

func Buttons

func Buttons(elems []Element) []Element

Buttons lists the buttons in elems, depth first.

func (Element) Press

func (e Element) Press()

Press runs the button's onPress in its mod, without waiting for it.

func (Element) Pressable

func (e Element) Pressable() bool

Pressable reports whether the element is a button with an onPress.

type Host

type Host struct {
	// contains filtered or unexported fields
}

Host owns the runtimes of the loaded mods, in install order.

func NewHost

func NewHost(opts Options) *Host

NewHost starts an empty host; Load gives it mods.

func (*Host) Close

func (h *Host) Close()

Close stops every runtime.

func (*Host) ClosePane

func (h *Host) ClosePane(p Pane)

ClosePane closes a pane as Esc does.

func (*Host) Commands

func (h *Host) Commands() []Command

Commands lists the palette commands of the running mods, in mod order.

func (*Host) Dispatch

func (h *Host) Dispatch(ctx context.Context, event string, e map[string]any, def func(map[string]any) (any, error)) (any, error)

Dispatch runs event through every mod's handlers, in mod order, ending in def (Lectern's own behaviour; nil does nothing). It returns what the first handler returned, exported to plain Go values.

func (*Host) Handles

func (h *Host) Handles(event string) bool

Handles reports whether any running mod has a handler for event, so the console can skip work nobody listens to.

func (*Host) Load

func (h *Host) Load(list []Mod) bool

Load replaces the running mods with list. An unchanged mod keeps its runtime, state in memory, commands and panes; a new one is started and gets app.start. It reports whether anything changed.

func (*Host) Loaded

func (h *Host) Loaded() int

Loaded reports how many mods are running.

func (*Host) Logs

func (h *Host) Logs(key string) []string

Logs returns what a mod passed to $.log, newest last.

func (*Host) Notices

func (h *Host) Notices() <-chan Notice

Notices delivers toasts and redraw requests. Sends never block: a console that falls behind loses redraw hints, not its own events.

func (*Host) OpenPanes

func (h *Host) OpenPanes() []Pane

OpenPanes lists the panes the mods opened, the most recent last.

func (*Host) Problems

func (h *Host) Problems() []string

Problems lists what keeps mods from running: load errors, paused mods, and the most recent handler error of each mod.

func (*Host) Render

func (h *Host) Render(component string, props map[string]any) (hidden bool, elems []Element)

Render asks the mods how to draw component. It waits at most the render budget (100 ms); handlers still running then are drawn without.

func (*Host) RenderMany

func (h *Host) RenderMany(component string, props []map[string]any) []Rendered

RenderMany renders several instances of one component (every session row) under one shared budget, so a slow mod costs the console 100 ms per redraw, not 100 ms per row.

func (*Host) RenderPane

func (h *Host) RenderPane(p Pane) []Element

RenderPane draws an open pane: the ui.render "pane" result's append.

func (*Host) RunCommand

func (h *Host) RunCommand(ctx context.Context, id string, args map[string]any) (any, error)

RunCommand runs a mod command through command.run; the end of the chain calls the command's own run($, args).

func (*Host) SetViewport

func (h *Host) SetViewport(width, height int)

SetViewport records the terminal size ui.render reports.

func (*Host) Status

func (h *Host) Status() []string

Status is every mod's $.ui.status text, in mod order.

type Mod

type Mod struct {
	Plugin string `json:"plugin"`
	ID     string `json:"id"`
	Name   string `json:"name"`
	API    string `json:"api,omitempty"`
	Hash   string `json:"hash"`
	Script string `json:"script,omitempty"`
	Error  string `json:"error,omitempty"`
}

Mod is one entry of GET /api/plugins/mods?surface=cli. Script is already the classic script pluginpkg.ModScript makes of the module.

func (Mod) Key

func (m Mod) Key() string

Key names a mod across reloads: plugin/id.

type Notice

type Notice struct {
	Kind NoticeKind
	Mod  string // the mod's display name
	Text string
}

Notice is something the console should react to between its own events.

type NoticeKind

type NoticeKind int

NoticeKind says what changed.

const (
	// NoticeRender: something a mod draws may have changed; render again.
	NoticeRender NoticeKind = iota
	// NoticeToast: Text is a notification from Mod.
	NoticeToast
	// NoticePanes: a pane was opened or closed.
	NoticePanes
)

type Options

type Options struct {
	API API
	// StateDir keeps each mod's $.state as JSON; empty keeps it in memory.
	StateDir string
	// Version is reported to app.start.
	Version string
}

Options configure a Host.

type Pane

type Pane struct {
	Mod   string
	ID    string
	Title string
}

Pane is a pane opened with $.ui.open.

type Rendered

type Rendered struct {
	Hidden bool
	Append []Element
}

Rendered is one ui.render result.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL