canvas

package
v0.0.0-...-d25b266 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package canvas provides IPC communication for TUI applications. It allows AI assistants to query and control TUI state via Unix sockets.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultSocketDir

func DefaultSocketDir() string

DefaultSocketDir returns the default directory for canvas sockets

func SocketPath

func SocketPath(id string) string

SocketPath returns the socket path for a canvas ID

func Wrap

func Wrap(canvasID string, model tea.Model) tea.Model

Types

type BubbleTeaAdapter

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

func (*BubbleTeaAdapter) CanvasState

func (a *BubbleTeaAdapter) CanvasState() StatePayload

func (*BubbleTeaAdapter) CanvasView

func (a *BubbleTeaAdapter) CanvasView() string

func (*BubbleTeaAdapter) HandleCanvasInput

func (a *BubbleTeaAdapter) HandleCanvasInput(text string) error

func (*BubbleTeaAdapter) HandleCanvasKey

func (a *BubbleTeaAdapter) HandleCanvasKey(key string, r rune) error

func (*BubbleTeaAdapter) Init

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

func (*BubbleTeaAdapter) Update

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

func (*BubbleTeaAdapter) View

func (a *BubbleTeaAdapter) View() string

type Client

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

Client connects to a canvas server to query/control it

func NewClient

func NewClient(id string) *Client

NewClient creates a client for the given canvas ID

func NewClientWithSocket

func NewClientWithSocket(socket string) *Client

NewClientWithSocket creates a client with a custom socket path

func (*Client) Close

func (c *Client) Close() error

Close requests the canvas to close

func (*Client) GetState

func (c *Client) GetState() (*StatePayload, error)

GetState queries the canvas for its current state

func (*Client) GetView

func (c *Client) GetView() (string, error)

GetView queries the canvas for its rendered view

func (*Client) Ping

func (c *Client) Ping() bool

Ping checks if the canvas is responsive

func (*Client) SendInput

func (c *Client) SendInput(text string) error

SendInput sends text input to the canvas

func (*Client) SendKey

func (c *Client) SendKey(key string) error

SendKey sends a key press to the canvas

type ErrorPayload

type ErrorPayload struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

ErrorPayload contains error information

type InputHandler

type InputHandler interface {
	// HandleCanvasInput processes text input sent via IPC
	HandleCanvasInput(text string) error
}

InputHandler is implemented by TUI models to receive text input

type InputPayload

type InputPayload struct {
	Text string `json:"text"`
}

InputPayload contains text input

type KeyHandler

type KeyHandler interface {
	// HandleCanvasKey processes a key sent via IPC
	HandleCanvasKey(key string, r rune) error
}

KeyHandler is implemented by TUI models to receive key events

type KeyPayload

type KeyPayload struct {
	Key  string `json:"key"`            // e.g., "enter", "tab", "ctrl+c"
	Rune rune   `json:"rune,omitempty"` // for character input
}

KeyPayload contains a key to send

type Message

type Message struct {
	Type    MessageType     `json:"type"`
	Payload json.RawMessage `json:"payload,omitempty"`
}

Message is the base IPC message structure

func NewMessage

func NewMessage(t MessageType, payload any) (*Message, error)

NewMessage creates a new message with the given type and payload

func (*Message) ParsePayload

func (m *Message) ParsePayload(v any) error

ParsePayload unmarshals the payload into the given type

type MessageType

type MessageType string

MessageType identifies the type of IPC message

const (
	// Queries (AI → TUI)
	MsgGetState  MessageType = "get_state"
	MsgGetView   MessageType = "get_view"
	MsgSendKey   MessageType = "send_key"
	MsgSendInput MessageType = "send_input"
	MsgClose     MessageType = "close"

	// Responses (TUI → AI)
	MsgState MessageType = "state"
	MsgView  MessageType = "view"
	MsgAck   MessageType = "ack"
	MsgError MessageType = "error"

	// Events (TUI → AI, async)
	MsgReady     MessageType = "ready"
	MsgUpdated   MessageType = "updated"
	MsgSelected  MessageType = "selected"
	MsgCancelled MessageType = "cancelled"
)

type Server

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

Server handles IPC communication for a TUI

func NewServer

func NewServer(id string) (*Server, error)

NewServer creates a new IPC server for the given canvas ID

func (*Server) ID

func (s *Server) ID() string

ID returns the canvas ID

func (*Server) OnClose

func (s *Server) OnClose(fn func())

OnClose sets a callback for when a close message is received

func (*Server) SendEvent

func (s *Server) SendEvent(msgType MessageType, payload any) error

SendEvent sends an async event to any connected client

func (*Server) SetModel

func (s *Server) SetModel(model any)

SetModel sets the TUI model for state queries

func (*Server) SocketPath

func (s *Server) SocketPath() string

SocketPath returns the path to the Unix socket

func (*Server) Start

func (s *Server) Start()

Start begins accepting connections

func (*Server) Stop

func (s *Server) Stop()

Stop closes the server

type StatePayload

type StatePayload struct {
	// Custom fields from the TUI's model
	Custom map[string]any `json:"custom,omitempty"`

	// Standard fields
	Width   int    `json:"width,omitempty"`
	Height  int    `json:"height,omitempty"`
	Focused bool   `json:"focused,omitempty"`
	Mode    string `json:"mode,omitempty"`
	Input   string `json:"input,omitempty"`
	Cursor  int    `json:"cursor,omitempty"`
}

StatePayload contains the TUI's current state

type StateProvider

type StateProvider interface {
	// CanvasState returns the current state for IPC queries
	CanvasState() StatePayload
}

StateProvider is implemented by TUI models to expose their state

type ViewPayload

type ViewPayload struct {
	Content string `json:"content"`
	ANSI    bool   `json:"ansi"` // true if content contains ANSI codes
}

ViewPayload contains the rendered view

type ViewProvider

type ViewProvider interface {
	// CanvasView returns the current rendered view
	CanvasView() string
}

ViewProvider is implemented by TUI models to expose their rendered view

Jump to

Keyboard shortcuts

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