Documentation
¶
Overview ¶
Package engine runs Kite applications. An Engine connects a document to a terminal backend, processes input and scheduled work, and lays out and paints the document and its overlays.
Create an engine with New, attach the UI with Engine.Mount, and start the event loop with Engine.Run.
Index ¶
- Constants
- Variables
- func WithProfiler(enabled bool) func(*Options)
- type ClipboardProxy
- func (c *ClipboardProxy) ListFormats() *promise.Promise[[]string]
- func (c *ClipboardProxy) Read(mime string) *promise.Promise[[]byte]
- func (c *ClipboardProxy) ReadText() *promise.Promise[string]
- func (c *ClipboardProxy) Write(mime string, data []byte) *promise.Promise[struct{}]
- func (c *ClipboardProxy) WriteText(text string) *promise.Promise[struct{}]
- type Clock
- type CursorController
- type Engine
- func (e *Engine) AddEventListener(target any, typ event.EventType, fn event.Listener, opts ...event.Option) event.Subscription
- func (e *Engine) Bell()
- func (e *Engine) Caps() backend.Caps
- func (e *Engine) Clear()
- func (e *Engine) ColorScheme() style.ColorScheme
- func (e *Engine) Cursor() *CursorController
- func (e *Engine) DebugXRay() bool
- func (e *Engine) Document() dom.Document
- func (e *Engine) Done() <-chan struct{}
- func (e *Engine) Dump(path string) error
- func (e *Engine) EnsureFreshLayout()
- func (e *Engine) ExecProcess(ctx context.Context, cmd *exec.Cmd) (err error)
- func (e *Engine) FocusManager() *focus.Manager
- func (e *Engine) FocusedTarget() event.EventTarget
- func (e *Engine) Frame()
- func (e *Engine) HasPendingTasks() bool
- func (e *Engine) HitTest(x, y int) event.EventTarget
- func (e *Engine) InsertAbove(node dom.Node)
- func (e *Engine) IsInline() bool
- func (e *Engine) Mount(root dom.Element)
- func (e *Engine) MoveCaret(dir dom.Direction) bool
- func (e *Engine) NavigateFocus(dir dom.Direction) bool
- func (e *Engine) OnFrameRendered(fn func())
- func (e *Engine) OnStop(fn func())
- func (e *Engine) PaintEngine() *paint.PaintEngine
- func (e *Engine) Post(fn func())
- func (e *Engine) PostMacro(fn func())
- func (e *Engine) ProcessRawEvent(raw backend.RawEvent)
- func (e *Engine) RenderObject(n dom.Node) render.Object
- func (e *Engine) RenderView() *render.RenderView
- func (e *Engine) RequestAnimationFrame(fn func(time.Time)) func()
- func (e *Engine) RequestFrame()
- func (e *Engine) RequestFrameAt(t time.Time)
- func (e *Engine) Run(ctx context.Context) error
- func (e *Engine) Scheduler() terminal.Scheduler
- func (e *Engine) SetColorScheme(scheme style.ColorScheme)
- func (e *Engine) SetDebugXRay(enabled bool)
- func (e *Engine) SetInterval(fn func(), interval time.Duration) *Timer
- func (e *Engine) SetMouseMode(mode MouseMode)
- func (e *Engine) SetProgramResume(resume *terminal.ProgramResume)
- func (e *Engine) SetProgramStatus(status *terminal.ProgramStatus)
- func (e *Engine) SetProgressBar(state backend.ProgressBarState, percentage int)
- func (e *Engine) SetTimeout(fn func(), delay time.Duration) *Timer
- func (e *Engine) SetTitle(s string)
- func (e *Engine) StartProfiling()
- func (e *Engine) Stop()
- func (e *Engine) StopProfiling() *trace.Tracer
- func (e *Engine) Tracer() *trace.Tracer
- type MouseMode
- type Options
- type Pipeline
- type ProfilingPipeline
- type StandardPipeline
- type TerminalProxy
- func (tp *TerminalProxy) Bell()
- func (tp *TerminalProxy) Clipboard() terminal.Clipboard
- func (tp *TerminalProxy) Cursor() terminal.Cursor
- func (tp *TerminalProxy) ExecProcess(ctx context.Context, cmd *exec.Cmd) error
- func (tp *TerminalProxy) IsClosed() bool
- func (tp *TerminalProxy) OnClose(fn func())
- func (tp *TerminalProxy) Scheduler() terminal.Scheduler
- func (tp *TerminalProxy) SetProgramResume(resume *terminal.ProgramResume)
- func (tp *TerminalProxy) SetProgramStatus(status *terminal.ProgramStatus)
- func (tp *TerminalProxy) SetProgressBar(state terminal.ProgressBarState, percentage int)
- func (tp *TerminalProxy) SetTitle(title string)
- func (tp *TerminalProxy) Window() terminal.Window
- type Timer
Constants ¶
const DefaultMacroTaskBudget = 1024
DefaultMacroTaskBudget is the maximum number of macrotasks drained per frame.
const DefaultMacroTaskDuration = 4 * time.Millisecond
DefaultMacroTaskDuration is the wall-clock budget for the macrotask drain phase per frame.
const DefaultWorkers = 4
DefaultWorkers is the number of worker goroutines in the pool when not configured explicitly.
const MinFrameInterval = 8 * time.Millisecond
MinFrameInterval is the minimum elapsed time between two consecutive frames. Frame requests within this window coalesce into a single frame.
const UnlimitedMacroTaskDuration time.Duration = -1
UnlimitedMacroTaskDuration disables the wall-clock budget during macrotask draining (draining remains bounded by MacroTaskBudget). Intended for deterministic headless tests.
Variables ¶
var ErrExecBusy = errors.New("terminal already has an active subprocess")
ErrExecBusy is returned by ExecProcess when another subprocess already owns the terminal.
var ErrExecUnsupported = errors.New("terminal backend does not support process suspension")
ErrExecUnsupported is returned by ExecProcess when the active backend does not implement backend.Suspender.
Functions ¶
func WithProfiler ¶
WithProfiler enables or disables the engine's built-in profiler.
Types ¶
type ClipboardProxy ¶
type ClipboardProxy struct {
// contains filtered or unexported fields
}
func (*ClipboardProxy) ListFormats ¶
func (c *ClipboardProxy) ListFormats() *promise.Promise[[]string]
type Clock ¶
type Clock interface {
// Now returns the current time.
Now() time.Time
// After returns a channel that receives the current time after duration d
// has elapsed. The channel is never closed.
After(d time.Duration) <-chan time.Time
}
Clock is an injectable time source used by the engine for frame scheduling. The real implementation delegates to the standard library; tests supply a fake implementation to exercise time-dependent behaviour deterministically.
Clock implementations must be safe for concurrent use.
type CursorController ¶
type CursorController struct {
// contains filtered or unexported fields
}
CursorController controls the next frame's hardware cursor on the main goroutine.
func (*CursorController) Hide ¶
func (c *CursorController) Hide()
Hide suppresses the hardware cursor for this frame.
func (*CursorController) SetPos ¶
func (c *CursorController) SetPos(x, y int)
SetPos updates the terminal-cell position without changing visibility.
func (*CursorController) SetPosition ¶
func (c *CursorController) SetPosition(x, y int)
SetPosition places and shows the hardware cursor for this frame.
func (*CursorController) SetShape ¶
func (c *CursorController) SetShape(shape style.CursorShape)
SetShape selects the hardware cursor shape for this frame.
func (*CursorController) Show ¶
func (c *CursorController) Show(visible bool)
Show overrides cursor visibility for this frame.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine orchestrates the five-phase frame pipeline (Tasks → Style → Layout → Paint → Sync), the worker pool, and the macrotask / microtask queues.
Engine must be used from a single main goroutine for all render-tree operations; the worker pool runs concurrent goroutines for user-submitted task only.
func New ¶
New creates an Engine configured with the given backend and options.
Call Engine.Run to start the event loop (blocking), or call Engine.Frame directly for testing.
func (*Engine) AddEventListener ¶
func (e *Engine) AddEventListener(target any, typ event.EventType, fn event.Listener, opts ...event.Option) event.Subscription
AddEventListener registers fn as a listener for event of type typ on target. It returns a Subscription that can be used to remove the listener.
func (*Engine) Bell ¶
func (e *Engine) Bell()
Bell emits a BEL character. It is a no-op when Caps.Bell is false.
Bell must be called from the main goroutine.
func (*Engine) Clear ¶
func (e *Engine) Clear()
Clear clears the terminal and scrollback without unmounting the document, and schedules a repaint of the active UI.
func (*Engine) ColorScheme ¶
func (e *Engine) ColorScheme() style.ColorScheme
func (*Engine) Cursor ¶
func (e *Engine) Cursor() *CursorController
Cursor returns the CursorController that widgets use to drive cursor state.
func (*Engine) DebugXRay ¶
DebugXRay reports whether the visual layout debugging overlay is enabled.
func (*Engine) Done ¶
func (e *Engine) Done() <-chan struct{}
Done returns a channel that is closed when the engine stops.
func (*Engine) EnsureFreshLayout ¶
func (e *Engine) EnsureFreshLayout()
EnsureFreshLayout forces an immediate Sync and Layout pass if the DOM or Render tree is dirty. This guarantees that subsequent reads of layout data (like element bounds or cursor positions) are accurate. This is analogous to forced synchronous layout in web browsers.
func (*Engine) ExecProcess ¶
ExecProcess suspends the terminal backend, runs cmd to completion (inheriting unset stdio streams from the backend), and resumes rendering with a full repaint. Safe to call from event handlers or background goroutines.
func (*Engine) FocusManager ¶
FocusManager returns the engine's focus.Manager so that tests and application code can drive focus programmatically (e.g. simulate a mousedown-to-focus or query the currently focused node).
func (*Engine) FocusedTarget ¶
func (e *Engine) FocusedTarget() event.EventTarget
FocusedTarget returns the currently focused event target, or nil. It satisfies the event.FocusReader interface.
func (*Engine) Frame ¶
func (e *Engine) Frame()
Frame executes one complete frame pipeline:
- Sync Phase.
- Task Phase (macrotasks + microtasks).
- Style phase.
- Layout phase.
- Paint phase.
- Commit.
Frame must be called from the main goroutine.
func (*Engine) HasPendingTasks ¶
HasPendingTasks reports whether the engine's scheduler has any queued microtasks or macrotasks waiting to be drained.
func (*Engine) HitTest ¶
func (e *Engine) HitTest(x, y int) event.EventTarget
HitTest walks the render tree at point (x, y) and returns the topmost event target at that position. It tests overlays (topmost-first) before falling through to the main tree. Returns nil when no target is hit.
func (*Engine) InsertAbove ¶
InsertAbove renders an arbitrary dom.Node (detached from the main document) into a *backend.Buffer and inserts it above the next active frame.
func (*Engine) MoveCaret ¶
MoveCaret moves the global selection caret in the specified direction. If the caret hits the edge of the focused element, it shifts focus spatially. It returns true if a transition occurred.
func (*Engine) NavigateFocus ¶
NavigateFocus shifts DOM focus in the specified direction within the active focus scope. It returns true if focus successfully shifted to a new element.
func (*Engine) OnFrameRendered ¶
func (e *Engine) OnFrameRendered(fn func())
OnFrameRendered registers a hook to be called after every frame is committed.
func (*Engine) OnStop ¶
func (e *Engine) OnStop(fn func())
OnStop registers a hook to be called when the engine stops. If the engine is already stopped, fn is invoked immediately.
func (*Engine) PaintEngine ¶
func (e *Engine) PaintEngine() *paint.PaintEngine
PaintEngine returns the internal paint engine.
func (*Engine) Post ¶
func (e *Engine) Post(fn func())
Post schedules fn as a microtask. Microtasks run on the main thread and are drained (until the queue is empty) during each frame, between macrotask iterations and at the end of the task phase.
Post is safe to call from any goroutine.
func (*Engine) PostMacro ¶
func (e *Engine) PostMacro(fn func())
PostMacro schedules fn as a macrotask. Macrotasks are drained once per frame, subject to the configured count and duration budgets.
PostMacro is safe to call from any goroutine.
func (*Engine) ProcessRawEvent ¶
ProcessRawEvent converts a raw backend event into a sequence of DOM events and dispatches them through the tree. It is used primarily for testing and for custom event-loop implementations.
func (*Engine) RenderView ¶
func (e *Engine) RenderView() *render.RenderView
RenderView returns the root render object owned by this engine.
func (*Engine) RequestAnimationFrame ¶
func (*Engine) RequestFrame ¶
func (e *Engine) RequestFrame()
RequestFrame schedules a frame wake-up as soon as possible (after at least MinFrameInterval has elapsed since the last frame). Consecutive calls within MinFrameInterval coalesce.
RequestFrame is safe to call from any goroutine.
func (*Engine) RequestFrameAt ¶
RequestFrameAt schedules a frame wake-up at time t. If t is before time.Now() the frame is scheduled immediately.
RequestFrameAt must be called from the main goroutine.
func (*Engine) Run ¶
Run starts the engine's main event loop. It blocks until Stop is called, ctx is canceled, or the backend signals exit. A callback panic propagates after the engine stops and restores the terminal.
func (*Engine) SetColorScheme ¶
func (e *Engine) SetColorScheme(scheme style.ColorScheme)
func (*Engine) SetDebugXRay ¶
SetDebugXRay toggles the visual layout debugging overlay.
func (*Engine) SetInterval ¶
SetInterval schedules fn to run on the main thread repeatedly at the specified interval. It is safe to call from any goroutine.
func (*Engine) SetMouseMode ¶
SetMouseMode configures the mouse-event level. The backend toggles the relevant terminal protocol; the engine carries the policy.
SetMouseMode must be called from the main goroutine.
func (*Engine) SetProgramResume ¶
func (e *Engine) SetProgramResume(resume *terminal.ProgramResume)
SetProgramResume reports the command to resume this process using OSC 7577. It is a no-op if Caps.ProgramResume is false.
SetProgramResume must be called from the main goroutine.
func (*Engine) SetProgramStatus ¶
func (e *Engine) SetProgramStatus(status *terminal.ProgramStatus)
SetProgramStatus reports the program status to the terminal using OSC 7501. It is a no-op if Caps.ProgramStatus is false.
SetProgramStatus must be called from the main goroutine.
func (*Engine) SetProgressBar ¶
func (e *Engine) SetProgressBar(state backend.ProgressBarState, percentage int)
SetProgressBar updates the terminal window tab's native progress bar. It is a no-op if Caps.ProgressBar is false.
SetProgressBar must be called from the main goroutine.
func (*Engine) SetTimeout ¶
SetTimeout schedules fn to run on the main thread after the specified delay. It is safe to call from any goroutine.
func (*Engine) SetTitle ¶
SetTitle sets the terminal window title. It is a no-op when Caps.Title is false.
SetTitle must be called from the main goroutine.
func (*Engine) StartProfiling ¶
func (e *Engine) StartProfiling()
StartProfiling dynamically enables profiling by wrapping the engine's active pipeline with ProfilingPipeline (if not already wrapped) and starting a new tracer.
func (*Engine) Stop ¶
func (e *Engine) Stop()
Stop shuts down the scheduler and restores the backend. Repeated calls are safe.
func (*Engine) StopProfiling ¶
StopProfiling dynamically disables profiling by reverting the engine's active pipeline to the standard (unwrapped) pipeline and returning the accumulated tracer. If profiling was not active, it returns nil.
type MouseMode ¶
type MouseMode uint8
MouseMode configures what level of mouse event the backend enables.
const ( // MouseModeOff disables all mouse event reporting. MouseModeOff MouseMode = iota // MouseModeClick enables button-press event only. MouseModeClick // MouseModeDrag enables drag event (motion while button held, default). MouseModeDrag // MouseModeTrack enables full motion tracking including hover event. MouseModeTrack )
type Options ¶
type Options struct {
// Clipboard overrides the backend's default OSC 52 clipboard when non-nil.
Clipboard terminal.Clipboard
// NumWorkers is the size of the worker pool (default DefaultWorkers).
NumWorkers int
// MacroTaskBudget caps macrotasks per frame by count (default DefaultMacroTaskBudget).
MacroTaskBudget int
// MacroTaskDuration caps macrotasks per frame by wall clock (default DefaultMacroTaskDuration).
// Set to UnlimitedMacroTaskDuration (or any negative duration) to disable the wall-clock cap.
MacroTaskDuration time.Duration
// Clock is the injectable time source. Defaults to RealClock().
Clock Clock
// ShutdownTimeout bounds how long Stop waits for pending jobs before
// forcing exit. Default is 5 seconds.
ShutdownTimeout time.Duration
// Profiler enables the high-level phase profiling and deep-tree tracing.
Profiler bool
// Inline enables inline mode (relative cursor, dynamic height, no alt-screen).
Inline bool
}
Options configures an Engine.
type Pipeline ¶
type Pipeline interface {
Sync(e *Engine)
Tasks(e *Engine)
Style(e *Engine)
Layout(e *Engine) bool // returns layoutRan
Paint(e *Engine, layoutRan bool)
}
Pipeline defines the core phases of the Kite rendering engine. Abstracting these into an interface allows for decorators like a Profiler to wrap the standard execution loop without polluting the core logic.
type ProfilingPipeline ¶
type ProfilingPipeline struct {
// contains filtered or unexported fields
}
ProfilingPipeline wraps a Pipeline and records phase durations using a trace.Tracer.
func (*ProfilingPipeline) Layout ¶
func (p *ProfilingPipeline) Layout(e *Engine) bool
func (*ProfilingPipeline) Paint ¶
func (p *ProfilingPipeline) Paint(e *Engine, layoutRan bool)
func (*ProfilingPipeline) Style ¶
func (p *ProfilingPipeline) Style(e *Engine)
func (*ProfilingPipeline) Sync ¶
func (p *ProfilingPipeline) Sync(e *Engine)
func (*ProfilingPipeline) Tasks ¶
func (p *ProfilingPipeline) Tasks(e *Engine)
type StandardPipeline ¶
type StandardPipeline struct {
// contains filtered or unexported fields
}
StandardPipeline is the default implementation of the Kite rendering pipeline.
func (*StandardPipeline) Layout ¶
func (p *StandardPipeline) Layout(e *Engine) bool
func (*StandardPipeline) Paint ¶
func (p *StandardPipeline) Paint(e *Engine, layoutRan bool)
func (*StandardPipeline) Style ¶
func (p *StandardPipeline) Style(e *Engine)
func (*StandardPipeline) Sync ¶
func (p *StandardPipeline) Sync(e *Engine)
func (*StandardPipeline) Tasks ¶
func (p *StandardPipeline) Tasks(e *Engine)
type TerminalProxy ¶
type TerminalProxy struct {
// contains filtered or unexported fields
}
TerminalProxy exposes the engine's host services to its document.
func (*TerminalProxy) Bell ¶
func (tp *TerminalProxy) Bell()
func (*TerminalProxy) Clipboard ¶
func (tp *TerminalProxy) Clipboard() terminal.Clipboard
Clipboard returns the provider selected during engine construction.
func (*TerminalProxy) Cursor ¶
func (tp *TerminalProxy) Cursor() terminal.Cursor
Cursor returns the engine's per-frame hardware cursor controller.
func (*TerminalProxy) ExecProcess ¶
ExecProcess hands off the terminal to cmd until it exits, then resumes rendering.
func (*TerminalProxy) IsClosed ¶
func (tp *TerminalProxy) IsClosed() bool
func (*TerminalProxy) OnClose ¶
func (tp *TerminalProxy) OnClose(fn func())
func (*TerminalProxy) Scheduler ¶
func (tp *TerminalProxy) Scheduler() terminal.Scheduler
func (*TerminalProxy) SetProgramResume ¶
func (tp *TerminalProxy) SetProgramResume(resume *terminal.ProgramResume)
func (*TerminalProxy) SetProgramStatus ¶
func (tp *TerminalProxy) SetProgramStatus(status *terminal.ProgramStatus)
func (*TerminalProxy) SetProgressBar ¶
func (tp *TerminalProxy) SetProgressBar(state terminal.ProgressBarState, percentage int)
func (*TerminalProxy) SetTitle ¶
func (tp *TerminalProxy) SetTitle(title string)
func (*TerminalProxy) Window ¶
func (tp *TerminalProxy) Window() terminal.Window
Window provides frame scheduling on the engine's rendering loop.