plannotator-tui 0.14.0

Annotate Markdown in the terminal - standalone, and as a Herdr plugin.
-- Rex Annotate: review documents, code changes and your agent's replies in plannotator-tui,
-- and send the feedback back to the agent as its next message.
--
-- Written by `plannotator-tui rex install`; run that again after upgrading plannotator-tui.
-- Load it from ~/.config/rex/init.lua, then run `rex config reload`:
--
--   require("plannotator.annotate").setup{}
--
-- Options, all optional:
--   placement = "tab" | "split" | "popup"
--                                   where reviews open: a full-size tab of their own, a split
--                                   beside the pane, or a popup over it; default: [rex]
--                                   placement in plannotator-tui's config, else a tab
--   keys = { annotate_gate = "ctrl+cmd+a", annotate_last = false }
--                                   change, add or drop keys; keys = false binds none. A key
--                                   given here to any action takes it from a default. A key
--                                   you rex.bind yourself wins when it comes after setup{}.
--   bin = "/path/to/plannotator-tui"

local M = {}

-- The plannotator-tui that wrote this file. Rex runs actions with a bare PATH, so the path is
-- absolute.
M.bin = @BIN@

-- Keys bound unless setup says otherwise. Rex for Mac binds none of them by default.
M.keys = {
  annotate_last = "shift+cmd+o",
  annotate_newest = "alt+shift+cmd+o",
  annotate_open = "ctrl+cmd+o",
  annotate_review_pick = "ctrl+cmd+p",
}

-- Each action reviews the focused pane: the agent in it, its folder, its repository's changes,
-- or its output.
M.actions = {
  { name = "annotate_last", title = "Annotate: Agent's Last Reply", args = { "last" },
    description = "Review the agent's recent replies and send feedback back." },
  { name = "annotate_newest", title = "Annotate: Agent's Newest Reply", args = { "last", "--newest" },
    description = "Review the agent's newest reply, without the picker." },
  { name = "annotate_open", title = "Annotate: Documents Here", args = { "open" },
    description = "Review the Markdown files in the pane's folder and send feedback to its agent." },
  { name = "annotate_gate", title = "Annotate: Documents Here, with Approve", args = { "open", "--gate" },
    description = "Review the pane's folder with an Approve button, and send the decision to its agent." },
  { name = "annotate_terminal", title = "Annotate: Pane Output", args = { "terminal" },
    description = "Review the pane's recent output and send feedback to its agent." },
  { name = "annotate_find", title = "Annotate: Newest Markdown File Named Here", args = { "find" },
    description = "Review the newest Markdown file the pane's output names." },
  { name = "annotate_review", title = "Annotate: Review Changes", args = { "review" },
    description = "Review the uncommitted changes in the pane's git repository and send feedback to its agent." },
  { name = "annotate_review_pick", title = "Annotate: Pick Changes to Review", args = { "review", "--pick" },
    description = "Pick what to review in the pane's git repository (uncommitted, unstaged, staged, a recent commit) and send feedback to its agent." },
}

local function launch(bin, placement, review, ctx)
  if ctx.block_id == nil then error("Annotate: no pane to review", 0) end
  local argv = { bin, "rex" }
  for _, arg in ipairs(review) do argv[#argv + 1] = arg end
  argv[#argv + 1] = "--block"
  argv[#argv + 1] = ctx.block_id
  if ctx.session_id ~= nil then
    argv[#argv + 1] = "--session"
    argv[#argv + 1] = ctx.session_id
  end
  if placement ~= nil then
    argv[#argv + 1] = "--placement"
    argv[#argv + 1] = placement
  end
  -- plannotator-tui opens the review pane and exits; the review runs in that pane.
  local r, err = rex.exec(argv, { timeout = 20 })
  if r == nil then error("Annotate: cannot run " .. bin .. ": " .. tostring(err), 0) end
  if not r.ok then
    local message = (r.stderr or ""):gsub("^%s+", ""):gsub("%s+$", "")
    if message == "" then message = "Annotate: plannotator-tui exited with status " .. tostring(r.code) end
    error(message, 0)
  end
end

function M.setup(opts)
  opts = opts or {}
  local bin = opts.bin or M.bin
  -- plannotator-tui checks the placement when a review opens.
  local placement = opts.placement

  for _, action in ipairs(M.actions) do
    local review = action.args
    rex.action{
      name = action.name,
      title = action.title,
      description = action.description,
      category = "Annotate",
      keywords = "plannotator review annotate feedback agent",
      run = function(ctx) launch(bin, placement, review, ctx) end,
    }
  end

  if opts.keys == false then return end
  local mine = opts.keys or {}
  -- A key the setup gives any action is theirs: a default on the same key steps aside.
  local taken = {}
  for _, key in pairs(mine) do
    if key then taken[M.key_id(key)] = true end
  end
  local function sorted(t)
    local names = {}
    for name in pairs(t) do names[#names + 1] = name end
    table.sort(names)
    return names
  end
  -- Defaults first, then the setup's own, each in name order, so the same setup binds the
  -- same way every time. `rex keymap` reports a key bound to an action that doesn't exist.
  for _, name in ipairs(sorted(M.keys)) do
    local key = M.keys[name]
    if mine[name] == nil and not taken[M.key_id(key)] then rex.bind(key, name) end
  end
  for _, name in ipairs(sorted(mine)) do
    local key = mine[name]
    if key then rex.bind(key, name) end
  end
end

-- A key as one string whatever its spelling: `Cmd+Ctrl+P` and `ctrl+cmd+p` are one key.
function M.key_id(key)
  local parts = {}
  for part in key:lower():gsub("%s+", ""):gmatch("[^+]+") do parts[#parts + 1] = part end
  local last = table.remove(parts)
  table.sort(parts)
  parts[#parts + 1] = last
  return table.concat(parts, "+")
end

return M