package rex import ( "context" "encoding/json" "strings" ) // ActionInfo describes an action that keys can be bound to. type ActionInfo struct { // Name is what bindings use, such as pane.split. Name string `json:"name"` // Kind is client (the client performs it), lua (the configuration file // defines it), or block (a block's creator provides it). Kind string `json:"kind,omitempty"` Title string `json:"title,omitempty"` Description string `json:"description,omitempty"` Category string `json:"category,omitempty"` Args json.RawMessage `json:"args,omitempty"` Repeats bool `json:"repeats,omitempty"` } // KeyBinding binds keys to an action. type KeyBinding struct { // Keys has one element per key pressed, such as ["ctrl+b", "c"]. Keys []string `json:"keys"` Action string `json:"action"` Args json.RawMessage `json:"args,omitempty"` Mode string `json:"mode,omitempty"` Source string `json:"source,omitempty"` } // KeymapLayer is a named set of bindings, such as a client's defaults. type KeymapLayer struct { Name string `json:"name"` Bindings []KeyBinding `json:"bindings"` } // KeymapArgs are client.keymap's arguments. type KeymapArgs struct { // Actions are the client's own actions. Actions []ActionInfo `json:"actions,omitempty"` // Defaults are the client's built-in bindings, beneath the // configuration file. Defaults []KeymapLayer `json:"defaults,omitempty"` } // KeymapMode is a key mode, as client.keymap reports it. type KeymapMode struct { Name string `json:"name"` Exclusive bool `json:"exclusive"` Blocked [][]string `json:"blocked"` } // Diagnostic is a problem with a keymap, to show people. type Diagnostic struct { Severity string `json:"severity"` Source string `json:"source"` Message string `json:"message"` Action string `json:"action"` Key string `json:"key"` Mode string `json:"mode"` } // KeymapResult is what client.keymap returns. type KeymapResult struct { // Bindings are resolved, in the order to try them. Bindings []KeyBinding `json:"bindings"` Diagnostics []Diagnostic `json:"diagnostics"` // Actions are the server's actions: the configuration file's, then each // block creator's. Actions []ActionInfo `json:"actions"` Modes []KeymapMode `json:"modes"` Config struct { Path string `json:"path"` Exists bool `json:"exists"` Generation int `json:"generation"` InstanceID string `json:"instance_id"` Hash string `json:"hash"` } `json:"config"` } // Keymap declares the client's actions and default bindings, and returns // what each key does once the configuration file and scripts are layered on // top. func (c *Conn) Keymap(ctx context.Context, args KeymapArgs) (KeymapResult, error) { var result KeymapResult _, err := c.Call(ctx, "client.keymap", args, &result) return result, err } // ClientAction is a client action that a configuration file action asked // this client to perform. type ClientAction struct { Action string `json:"action"` Args json.RawMessage `json:"args,omitempty"` } // ActionResult is what client.action returns. type ActionResult struct { Result json.RawMessage `json:"result"` // ClientActions are for this client to perform, in order. ClientActions []ClientAction `json:"client_actions"` } // Action runs a server action: one the configuration file defines, or a // block's action or method, on blockID or the session's focused block. func (c *Conn) Action(ctx context.Context, sessionID, blockID, action string, args json.RawMessage) (ActionResult, error) { payload := map[string]any{"action": action, "origin": "key"} if sessionID != "" { payload["session_id"] = sessionID } if blockID != "" { payload["block_id"] = blockID } if len(args) > 0 && string(args) != "null" { payload["args"] = args } var result ActionResult _, err := c.Call(ctx, "client.action", payload, &result) return result, err } // PerformRequest is a client_perform_requested event: another client asking // this one to perform one of its actions. type PerformRequest struct { CallID string `json:"call_id"` Action string `json:"action"` Args json.RawMessage `json:"args"` Origin string `json:"origin"` SessionID string `json:"session_id"` BlockID string `json:"block_id"` } // Reply answers a request the server passed on, such as a // client_perform_requested event. A nil err sends result. func (c *Conn) Reply(ctx context.Context, callID string, result any, err *Error) error { payload := map[string]any{"call_id": callID} if err != nil { payload["error"] = map[string]string{"code": err.Code, "message": err.Message} } else { payload["result"] = result } _, callErr := c.Call(ctx, "client.reply", payload, nil) return callErr } // Host is another Rex server, which people connect to by its label. type Host struct { ID string `json:"id"` Label string `json:"label"` // Endpoints reach the host, in the order to try them, in the forms // Candidates takes. Endpoints []string `json:"endpoints"` HostID string `json:"host_id"` } // Hosts returns the server's hosts: the other servers its user connects to // by label. func (c *Conn) Hosts(ctx context.Context) ([]Host, error) { var result struct { Hosts []Host `json:"hosts"` } _, err := c.Call(ctx, "hosts.list", nil, &result) return result.Hosts, err } // FindHost returns the host with a label, ignoring case, as the rex CLI // matches them. func FindHost(hosts []Host, label string) (Host, bool) { for _, h := range hosts { if strings.EqualFold(h.Label, label) { return h, true } } return Host{}, false }