template

package
v0.0.0-...-d0cc00f Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatchesKeyBinding

func MatchesKeyBinding(event *tcell.EventKey, binding config.KeyBinding) bool

MatchesKeyBinding returns true if the given key event matches the binding. The binding.Key string supports formats like "Escape", "Enter", "Ctrl+Q", "Ctrl+C", "F1", "Alt+a".

func ParseAlignment

func ParseAlignment(align string) int

ParseAlignment converts alignment strings to tview alignment constants

Types

type BoundView

type BoundView struct {
	Refresh func() string // returns evaluated template string
	SetText func(string)  // applies the string to the view
}

BoundView refreshes a view when its state key changes; used for deferred refresh from SetInputCapture.

type ColorHelper

type ColorHelper struct{}

ColorHelper provides color parsing utilities

func (*ColorHelper) Parse

func (c *ColorHelper) Parse(name string) tcell.Color

Parse converts color names to tcell.Color

type Context

type Context struct {
	App    *tview.Application
	Pages  *tview.Pages
	Colors *ColorHelper
	// contains filtered or unexported fields
}

Context provides the execution context for templates

func NewContext

func NewContext(app *tview.Application, pages *tview.Pages) *Context

NewContext creates a new template context

func (*Context) GetState

func (c *Context) GetState(key string) (interface{}, bool)

GetState returns the current value for a state key.

func (*Context) HasDirtyKeys

func (c *Context) HasDirtyKeys() bool

HasDirtyKeys returns true if any state key has been marked dirty (e.g. by SetStateDirect).

func (*Context) OnStateChange

func (c *Context) OnStateChange(key string, fn func(interface{}))

OnStateChange subscribes to state changes for the given key. The callback runs on the main goroutine when SetState is called for that key.

func (*Context) RefreshDirtyBoundViews

func (c *Context) RefreshDirtyBoundViews()

RefreshDirtyBoundViews evaluates and updates all bound views for dirty keys, then runs OnStateChange callbacks for those keys. Must be run on the main goroutine (e.g. via QueueUpdateDraw from a background refresh goroutine).

func (*Context) RegisterBoundView

func (c *Context) RegisterBoundView(key string, bv BoundView)

RegisterBoundView registers a view that displays state for key. It will be refreshed when RefreshDirtyBoundViews runs (on next key event), not from inside handlers.

func (*Context) RegisterFormCancel

func (c *Context) RegisterFormCancel(name string, callback func())

RegisterFormCancel registers a form's cancel callback by name so runFormCancel(formName) can invoke it (e.g. from a button).

func (*Context) RegisterFormSubmit

func (c *Context) RegisterFormSubmit(name string, callback func())

RegisterFormSubmit registers a form's submit callback by name so runFormSubmit(formName) can invoke it (e.g. from a button).

func (*Context) RunCallback

func (c *Context) RunCallback(templateStr string)

RunCallback executes a template expression (e.g. "switchToPage \"main\"") and runs the resulting callback. No-op if executor is not set or execution fails.

func (*Context) RunFormCancel

func (c *Context) RunFormCancel(name string)

RunFormCancel runs the cancel callback registered for the given form name. No-op if name is unknown.

func (*Context) RunFormSubmit

func (c *Context) RunFormSubmit(name string)

RunFormSubmit runs the submit callback registered for the given form name. No-op if name is unknown.

func (*Context) SetExecutor

func (c *Context) SetExecutor(e *Executor)

SetExecutor sets the template executor so RunCallback can execute template expressions (e.g. from modal onDone). Called by the app builder after creating the executor.

func (*Context) SetState

func (c *Context) SetState(key string, value interface{})

SetState updates the view model state and notifies subscribers. Safe to call from goroutines; notifications run on main via App.QueueUpdateDraw.

func (*Context) SetStateDirect

func (c *Context) SetStateDirect(key string, value interface{})

SetStateDirect updates state and marks the key dirty. Bound views are refreshed later from RefreshDirtyBoundViews (e.g. in SetInputCapture) to avoid deadlock.

type Executor

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

Executor handles template execution

func NewExecutor

func NewExecutor(ctx *Context, registry *FunctionRegistry) *Executor

NewExecutor creates a new template executor

func (*Executor) EvaluateToString

func (e *Executor) EvaluateToString(templateStr string) (string, error)

EvaluateToString evaluates a template string containing {{ bindState key }} (and other evaluators) and returns the rendered string. Example: "Notification: {{ bindState notification }}" -> "Notification: Hello" when state "notification" is "Hello"

func (*Executor) ExecuteCallback

func (e *Executor) ExecuteCallback(templateStr string) (func(), error)

ExecuteCallback parses and executes a template expression to create a callback function

func (*Executor) ExtractBindStateKeys

func (e *Executor) ExtractBindStateKeys(templateStr string) []string

ExtractBindStateKeys returns all state keys referenced by bindState in the template string. Used to subscribe to state changes for re-evaluation.

type FunctionRegistry

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

FunctionRegistry manages registered template functions

func NewFunctionRegistry

func NewFunctionRegistry() *FunctionRegistry

NewFunctionRegistry creates a new function registry with built-in functions

func (*FunctionRegistry) Get

func (r *FunctionRegistry) Get(name string) (*TemplateFunction, bool)

Get retrieves a template function by name

func (*FunctionRegistry) GetEvaluator

func (r *FunctionRegistry) GetEvaluator(name string) (*TemplateEvaluator, bool)

GetEvaluator retrieves an evaluator by name

func (*FunctionRegistry) Register

func (r *FunctionRegistry) Register(name string, minArgs int, maxArgs *int, validator func(*Context, []string) error, handler interface{}) error

Register adds a new template function to the registry

func (*FunctionRegistry) RegisterEvaluator

func (r *FunctionRegistry) RegisterEvaluator(name string, minArgs, maxArgs int, handler func(*Context, []string) string) error

RegisterEvaluator adds a value-returning template function (e.g. bindState)

type TemplateEvaluator

type TemplateEvaluator struct {
	Name    string
	MinArgs int
	MaxArgs int // evaluators use fixed arg count
	Handler func(*Context, []string) string
}

TemplateEvaluator defines a value-returning template function (e.g. bindState)

type TemplateFunction

type TemplateFunction struct {
	Name      string
	MinArgs   int
	MaxArgs   *int // nil means unlimited (variadic)
	Validator func(*Context, []string) error
	Handler   interface{} // Function that executes the template logic
}

TemplateFunction defines a registered template function

Jump to

Keyboard shortcuts

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