{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://macterm.thdxg.dev/schemas/palette.schema.json",
  "title": "Macterm extension palette",
  "description": "A command-palette screen: one file for each palette, in the palettes/ folder of an extension in ~/.config/macterm/extensions/. A palette is a graph of named nodes. Each node has rows that you write (`items`), a listing (`list`, a command whose output becomes rows), or both. Every row enters another node or runs an action. A row can name a second action for ⌥↩ (`alt`). A row can export values. They travel down the stack as environment variables into every command below the row.",
  "type": "object",
  "additionalProperties": false,
  "required": ["name", "nodes"],
  "examples": [
    {
      "name": "SSH",
      "description": "Connect to a host from ~/.ssh/config",
      "requires": ["ssh"],
      "root": "hosts",
      "nodes": {
        "hosts": {
          "placeholder": "Search hosts...",
          "list": "awk 'tolower($1) == \"host\" { for (i = 2; i <= NF; i++) if ($i !~ /[*?!]/) print $i }' ~/.ssh/config",
          "export": { "HOST": "." },
          "enter": "host"
        },
        "host": {
          "items": [
            { "title": "Connect", "action": { "run": "ssh \"$HOST\"" }, "alt": { "run": "ssh \"$HOST\"", "in": "split" } },
            { "title": "Copy Name", "action": { "copy": "$HOST" } }
          ]
        }
      }
    }
  ],
  "properties": {
    "name": {
      "type": "string",
      "pattern": "\\S",
      "description": "The name of the palette: its row in the command palette, its pill, and its row in Settings."
    },
    "icon": {
      "type": "string",
      "deprecated": true,
      "description": "Not used. The row and the pill of the palette show the icon of its extension (icon in extension.yaml). A file that sets this key still reads."
    },
    "description": {
      "type": "string",
      "description": "One line under the row of the palette in the command palette, and on its card in Settings → Extensions."
    },
    "root": {
      "type": "string",
      "description": "The node that the palette opens on. The default is the node with the name root."
    },
    "when": {
      "$ref": "#/$defs/condition",
      "description": "Whether the palette can be used now. Macterm runs the check each time that the command palette opens, in the background. The row of the palette is usable until the check fails. When the check fails, the row is muted and shows the reason. When you open the palette with its keybind, you see the reason and not a listing."
    },
    "requires": {
      "type": "array",
      "description": "The programs that the commands of the palette need, by name, for example `[kubectl, jq]`. When a listing fails, the error names each of these programs that is not in your PATH.",
      "items": { "type": "string", "pattern": "^[A-Za-z0-9._+/][A-Za-z0-9._+/-]*$" }
    },
    "nodes": {
      "type": "object",
      "description": "Every screen of the palette, by name. A node has `items`, a `list`, or both. The items come first, then the rows of the listing.",
      "minProperties": 1,
      "additionalProperties": { "$ref": "#/$defs/node" }
    }
  },
  "$defs": {
    "condition": {
      "type": "object",
      "additionalProperties": false,
      "required": ["run"],
      "properties": {
        "run": {
          "type": "string",
          "pattern": "\\S",
          "description": "A command that exits with 0 when the palette or row can be used. Use POSIX sh, or a script with a `#!` first line. It has the same environment and directory as a listing. It has 10 seconds."
        },
        "unavailable": {
          "type": "string",
          "description": "The text that the muted row shows when the check fails. The default is \"Unavailable\"."
        }
      }
    },
    "exports": {
      "type": "object",
      "propertyNames": {
        "pattern": "^[A-Za-z_][A-Za-z0-9_]*$",
        "not": { "enum": ["MACTERM_PROJECT_DIR", "MACTERM_PROJECT_NAME", "MACTERM_PALETTE_COMMAND", "MACTERM_PALETTE_REQUIRES", "MACTERM_EXTENSION_DIR"] }
      },
      "additionalProperties": { "type": "string" }
    },
    "node": {
      "type": "object",
      "additionalProperties": false,
      "anyOf": [{ "required": ["items"] }, { "required": ["list"] }],
      "if": { "required": ["list"] },
      "then": { "oneOf": [{ "required": ["enter"] }, { "required": ["action"] }] },
      "dependentRequired": {
        "rows": ["list"], "title": ["list"], "subtitle": ["list"], "match": ["list"],
        "export": ["list"], "enter": ["list"], "action": ["list"], "alt": ["list"]
      },
      "properties": {
        "placeholder": {
          "type": "string",
          "description": "The placeholder of the search field on this screen. The default is \"Search <pill>...\"."
        },
        "items": {
          "type": "array",
          "description": "Rows that you write, in order. Macterm shows them before the rows of a listing. Each row enters a node or runs an action.",
          "items": { "$ref": "#/$defs/item" }
        },
        "list": {
          "type": "string",
          "description": "The command of a listing. It runs one time when the screen opens. Use POSIX sh with errexit (`sh -o errexit -c`), or the interpreter that a `#!` first line names. Macterm starts it through your login shell (non-interactive: it reads the login files, not .zshrc), so the command sees your PATH. It runs in the directory of the active project. Its environment has MACTERM_PROJECT_DIR, MACTERM_PROJECT_NAME and every value that was exported above this screen. Its output is JSON (an array, newline-delimited objects, or an object that has the array at `rows`) or plain lines, one row each. ⌘R runs it again."
        },
        "rows": {
          "type": "string",
          "description": "For a listing whose output is a JSON object: the path to the array of rows, for example `.items`."
        },
        "title": {
          "type": "string",
          "description": "The title of a listing row. A value that starts with `.` is a path into the row (`.` is the row itself, `.metadata.name` is a field, `.items[0]` is an index). Any other value is literal. The default is `.`."
        },
        "subtitle": {
          "type": "string",
          "description": "The second line of a listing row. It is a path or a literal."
        },
        "icon": {
          "type": "string",
          "description": "An SF Symbol for every row of this node, unless the row names its own. In a listing, it is a path or a literal."
        },
        "match": {
          "type": "array",
          "minItems": 1,
          "description": "The fields that the search looks in. Each one is a path or a literal. The default is the title and the subtitle.",
          "items": { "type": "string" }
        },
        "export": {
          "$ref": "#/$defs/exports",
          "description": "Environment variables that a listing row sets for everything below it. Each one is a path or a literal, for example `{ NAMESPACE: .metadata.name }`. Macterm never puts them into the text of another command. Read them as variables. A name must be a name that sh can read, and not a name that Macterm sets itself."
        },
        "enter": {
          "type": "string",
          "description": "For a listing: the node that every row opens, with the exports of the row added. The title of the row names the pill of the new screen."
        },
        "action": {
          "$ref": "#/$defs/action",
          "description": "For a listing: what every row does when you select it, instead of `enter`."
        },
        "alt": {
          "$ref": "#/$defs/alt",
          "description": "For a listing: what every row does on ⌥↩ or ⌥-click instead."
        }
      }
    },
    "item": {
      "type": "object",
      "additionalProperties": false,
      "required": ["title"],
      "oneOf": [{ "required": ["enter"] }, { "required": ["action"] }],
      "properties": {
        "title": { "type": "string" },
        "subtitle": { "type": "string" },
        "icon": { "type": "string", "description": "An SF Symbol for this row." },
        "export": {
          "$ref": "#/$defs/exports",
          "description": "Literal environment variables this row sets for everything below it."
        },
        "enter": { "type": "string", "description": "The node this row opens." },
        "action": { "$ref": "#/$defs/action" },
        "alt": { "$ref": "#/$defs/alt", "description": "What this row does on ⌥↩ or ⌥-click instead." },
        "when": {
          "$ref": "#/$defs/condition",
          "description": "Whether you can select this row now. Macterm runs the check each time that its screen opens, in the background. The row is usable until the check fails. When the check fails, the row is muted and shows the reason. Items that share one command run it one time."
        }
      }
    },
    "alt": {
      "description": "The second action of a row. ⌥↩ or ⌥-click runs it. It has exactly one of run (with or without cwd), cwd, copy or open, and an optional title. The subtitle of the row shows the title while ⌥ is down. By default, it shows what the action does: \"Run in a Split\", \"Open in a New Tab\", \"Copy\".",
      "allOf": [{ "$ref": "#/$defs/actionFields" }],
      "properties": { "title": { "type": "string" } },
      "unevaluatedProperties": false
    },
    "action": {
      "description": "Exactly one of run (with or without cwd), cwd, copy or open.",
      "allOf": [{ "$ref": "#/$defs/actionFields" }],
      "unevaluatedProperties": false
    },
    "actionFields": {
      "type": "object",
      "oneOf": [
        { "anyOf": [{ "required": ["run"] }, { "required": ["cwd"] }] },
        { "required": ["copy"] },
        { "required": ["open"] }
      ],
      "dependentSchemas": {
        "in": { "anyOf": [{ "required": ["run"] }, { "required": ["cwd"] }] }
      },
      "properties": {
        "run": {
          "type": "string",
          "description": "A command that runs in a new terminal before its shell starts. Macterm never types it at the shell prompt. The exported variables are in its environment. Use POSIX sh with errexit, or the interpreter that a `#!` first line names. When the command ends, the shell prompt is there. In a remote project, Macterm types the command at the shell prompt of the host, as you wrote it. The shell of the host runs it, and the exported variables are not set."
        },
        "in": {
          "type": "string",
          "enum": ["tab", "split"],
          "description": "Where `run` runs: a new tab of the active project (the default) or a split next to its focused pane."
        },
        "cwd": {
          "type": "string",
          "minLength": 1,
          "description": "The directory where the new terminal starts. Without run, the terminal is a shell in that directory. In a listing, it is a path into the row or a literal. A relative path is relative to the project directory. The directory must exist. In a remote project, Macterm does not open the terminal, because the directory is on this Mac."
        },
        "copy": {
          "type": "string",
          "description": "Text to copy to the clipboard. In a listing, it is a path into the row or a literal."
        },
        "open": {
          "type": "string",
          "description": "A URL or file path to open with its default app. In a listing, it is a path into the row or a literal."
        }
      }
    }
  }
}
