tui

package
v0.271.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 42 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 tui holds the `ofga mappings` editors as components. Each one edits a *mapping.Document in place and talks to whatever hosts it only through the messages below. The workspace hosts them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DirItems

func DirItems(dir string, exts []string) ([]uilist.Item, error)

DirItems lists what is worth opening in dir: the parent, the subdirectories, and the files whose extension is in exts.

Dot-entries are left out. .git alone holds more directories than most of the trees a browser will walk, and burying model.fga under them would make the listing worse at the one thing it is for. A file kept inside one is still reachable by typing its path, which is the other half of the browser.

Names come off the disk, so they are sanitized for display; Item.ID keeps the real path.

func FailReason

func FailReason(o mapping.TestOutcome) string

FailReason is the first reason a test failed: an evaluation error first, since it is why everything else is missing, then the first tuple or filter missing, unexpected or not covered.

func Glyph

func Glyph(ic, text string) string

Glyph prefixes text with an icon and a space, or leaves it alone when the icon rung has none: under icons = off a decorative icon is empty, and a bare leading space would read as an indent.

func MappedCount

func MappedCount() int

MappedCount reports how many catalog events ship a ready-made mapping. Derived rather than written down: the two places that quoted it by hand had drifted apart, and the wrong number taught the user that an event mapping to nothing meant something was missing from their own setup.

func ProblemMark

func ProblemMark(p mapping.Problem) string

ProblemMark is the one place a severity becomes a character. A model warning is not a failure — the mapping saves — but it is not a clean bill either.

func SetSample

func SetSample(r *mapping.Rule, label string, event map[string]any)

SetSample attaches event to r as its sample and, when the event names its type, auto-fills the trigger from it.

func ShowRule

func ShowRule(st *Stack, i int)

ShowRule opens rule i's hub over the rules hub at the root of st, as enter on its row would: for a host that has just created the rule. Whatever was open over the rules hub closes.

Types

type App

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

App is the program: the start screen, then the workspace.

func NewApp

func NewApp(ctx context.Context, opts Options) (*App, error)

NewApp starts on the start screen, or straight in the workspace when opts.Path names a file. A path that does not exist opens a new mapping bound to it, as the start screen's New blank does. An error means that file could not be read, or could not be created where it was named.

func (*App) Init

func (a *App) Init() tea.Cmd

func (*App) Update

func (a *App) Update(msg tea.Msg) (tea.Model, tea.Cmd)

Update gives every message to the screen that is up. The workspace's command goes back untouched: tea.ExecProcess (the editor) and tea.Quit only work as the program receives them.

func (*App) View

func (a *App) View() tea.View

type ApplyTarget

type ApplyTarget struct {
	StoreID string
	Profile string
	// Name looks the store's name up. Best effort: "" when it cannot.
	Name   func(ctx context.Context) string
	Reader mapping.TupleReader
	Writer mapping.TupleWriter
	// Hints explains an apply failure the way the CLI does (clierr.Friendly,
	// the condition-conflict hint). It may return nothing.
	Hints func(err error) []string
	// Resume is the CLI command that resumes applying path's events from event
	// from. eventsPath is "" when the events did not come from one file; the
	// command then says "EVENTS_FILE". path is the workspace's file, which
	// Options.Path does not know when the start screen opened it.
	Resume func(path, eventsPath string, from int) string
}

ApplyTarget is the active profile's store.

type Focus

type Focus struct {
	Rule   int
	InIter bool
}

Focus is what the preview follows: the rule a component edits, and whether it edits inside that rule's iterator, where expressions address the item.

type Jump

type Jump struct {
	Rule    int
	Section string
}

Jump asks the host to show rule Rule in its Rules pane with the editor Section open ("" opens the rule itself). The Tests pane's e and the Check rows close with it. Section is one of "trigger", "action", "variables", "iterator", "filters", "tuple" or "".

type Options

type Options struct {
	// Path opens this file directly, skipping the start screen ("" shows it).
	Path string
	// Dir is where the start screen looks for mapping files.
	Dir string
	// RecentFile is mappings-recent.json; "" keeps no recent list.
	RecentFile string
	// Store names the configured store for the model dialog, e.g.
	// "profile prod, store 01ARZ3NDEKTSV4RRFFQ69G5FAV"; "" when none is set,
	// and then the store option is not offered.
	Store string
	// LoadStoreModel reads the store's model: --model-id or the profile's
	// model when set, else the latest. Nil when Store is "".
	LoadStoreModel func(ctx context.Context) (*openfga.AuthorizationModel, error)
	// Version is shown in the sidebar's brand line.
	Version string
	// Apply is the store the Apply pane writes to; nil when no store is set,
	// or when one is set but cannot be used.
	Apply *ApplyTarget
	// ApplyErr is why the store that is set cannot be used, such as a
	// malformed store ID; "" when Apply is set or no store is.
	ApplyErr string
}

Options configures `ofga mappings`.

type Outcome

type Outcome struct {
	// Changed reports that a component edited the document.
	Changed bool
	// Toasts are the lines components asked for, in order.
	Toasts []Toast
	// Requests are what components asked of the host, in order, that leave the
	// stack as it is.
	Requests []any
	// Closed reports that the root asked to close. The stack never pops its
	// root: whoever opened the stack decides what closing means.
	Closed bool
	// Result is what the root closed with.
	Result any
}

Outcome is what one Update settled that the host has to act on.

type Picked

type Picked struct {
	Label string
	Event map[string]any
	// Catalog is the catalog event chosen, set only by the add-rule fork's
	// pick, which shows the event's recipe before anything is committed.
	// Nil for a paste or a file, and for every pick on an existing rule.
	Catalog *auth0.Event
}

Picked is what an event pick closes with.

type Preview

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

Preview is the pane beside the editor: the file being built or the sample's payload, over what the sample evaluates to. A host draws it beside its stack, not on it, so its keys reach it whatever editor is on top.

func NewPreview

func NewPreview(s *Session, title string) *Preview

NewPreview builds the pane. title heads the file section.

func (*Preview) Keys

func (c *Preview) Keys() []string

Keys offers the switch, when there is a section to switch to.

func (*Preview) Refresh

func (c *Preview) Refresh(at Focus)

Refresh re-evaluates the document against the sample of the rule at points at. Evaluating a mapping-sized document costs microseconds and is capped by mapping.EvalTimeout, so a host calls it after every edit.

func (*Preview) Result

func (c *Preview) Result() mapping.Preview

Result is the evaluation the pane is showing.

func (*Preview) Switch

func (c *Preview) Switch() (string, bool)

Switch is what ctrl+t leads to, in the words the section header uses: the file from the payload, the payload from the file, nothing without a sample.

func (*Preview) Update

func (c *Preview) Update(msg tea.Msg) (component, tea.Cmd)

Update takes the pane's own keys: ctrl+t switches section, pgup/pgdn page the one on show. Everything else is ignored.

func (*Preview) View

func (c *Preview) View(w, rows int) string

View draws one long section, then the evaluation pinned under it.

type RuleAdded

type RuleAdded struct{ Index int }

RuleAdded is what a rule fork closes with: the index of the first rule it appended (a recipe can append several).

type RulesRequest

type RulesRequest int

RulesRequest is what the rules hub asks of its host: the actions that leave the editor. The hub closes with one and stays where it is, for the host to come back to.

const (
	// RequestAdd asks for a new rule, which the host's add-rule fork creates.
	RequestAdd RulesRequest = iota
	// RequestModel asks for the model source.
	RequestModel
	// RequestSave asks for the save dialog.
	RequestSave
	// RequestQuit asks to leave: outright with no rules, or by way of saving.
	RequestQuit
	// RequestTests asks for the tests, which the host shows over the hub.
	RequestTests
)

type Session

type Session struct {
	Ctx   context.Context
	Doc   *mapping.Document
	Index *mapping.ModelIndex
	// ReadOnly makes the components refuse the keys that edit the document and
	// drop them from Keys.
	ReadOnly bool
	// contains filtered or unexported fields
}

Session is what every component on a stack shares: the document they edit in place, the model they check against, the context evaluation runs under, and the messages posted that the stack has not settled yet.

func NewSession

func NewSession(ctx context.Context, doc *mapping.Document, ix *mapping.ModelIndex) *Session

NewSession starts the state the components on one host share.

type Stack

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

Stack is a host's handle on a pile of components: the top one has the keyboard, and the ones under it are where closing goes back to.

func OpenRules

func OpenRules(s *Session) *Stack

OpenRules opens the rules hub, the root of the workspace's rule editing.

func OpenTests

func OpenTests(s *Session, at Focus) *Stack

OpenTests opens the test list on its own, for a host that shows the tests apart from the rules hub. at is the rule the preview follows.

func (*Stack) Depth

func (st *Stack) Depth() int

Depth is how many components are open.

func (*Stack) Dialog

func (st *Stack) Dialog() bool

Dialog reports whether the top component is a dialog.

func (*Stack) Filtering

func (st *Stack) Filtering() bool

Filtering reports whether the top component's list filter is taking input.

func (*Stack) Focus

func (st *Stack) Focus() (Focus, bool)

Focus is the rule the top component edits, when it edits one.

func (*Stack) Heading

func (st *Stack) Heading() (title, subtitle string)

Heading is the top component's title and subtitle.

func (*Stack) Help

func (st *Stack) Help() (title, body string, ok bool)

Help is the top component's ? overlay, when it has one.

func (*Stack) Init

func (st *Stack) Init() tea.Cmd

Init starts the root component's work, such as focusing a text area. A root may open its first child here (see ruleFork), so what it posted is settled too. A root's Init may only open children: anything else it posts (a change, a toast, a request) is dropped with the Outcome.

func (*Stack) Keys

func (st *Stack) Keys() []string

Keys is the top component's hints.

func (*Stack) Resume

func (st *Stack) Resume() (tea.Cmd, Outcome)

Resume tells the top component it is on top again after the host showed something over the whole stack, so it re-reads the document. It settles and returns like Update.

func (*Stack) Titles

func (st *Stack) Titles() []string

Titles names every open component, bottom first, for a breadcrumb.

func (*Stack) Typing

func (st *Stack) Typing() bool

Typing reports whether the top component takes text: a form, a paste box, a typed path, or a list whose filter is being set. The host's single-key bindings must reach it instead.

func (*Stack) Update

func (st *Stack) Update(msg tea.Msg) (tea.Cmd, Outcome)

Update hands msg to the top component, then settles everything posted while it ran — including what handling those posts posts in turn — before returning, so the next key always reaches the component now on top.

func (*Stack) View

func (st *Stack) View(width, height int) string

View renders the top component.

type TestResults

type TestResults struct {
	Compiled bool
	Outcomes map[string]mapping.TestOutcome
}

TestResults is how the tests a save writes fare against the document as it is now: each outcome by test name. Compiled is false when the mapping does not compile, and then nothing ran.

func RunSavedTests

func RunSavedTests(ctx context.Context, d *mapping.Document) TestResults

RunSavedTests runs the tests a save writes the way `ofga mappings test` runs the saved file: rendered, loaded and run through mapper, so a verdict here is the verdict there. A mapping-sized run is small and in memory, so the screens run it on every change, each evaluation capped at mapping.EvalTimeout like the preview's rather than at the CLI's longer timeout.

type Toast

type Toast struct {
	Text string
	Err  bool
}

Toast is a line a component asked the host to show.

Jump to

Keyboard shortcuts

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