Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

6.0 KB
Raw Blame
// Derives help lines dynamically from registries so slash commands, settings, and keybinds
// never drift when new commands or configuration keys are added.
import { COMMAND_MANIFEST } from "./mso-agent-commands.mjs";
import { SETTINGS } from "./mso-agent-settings-entries.mjs";
import { defaultKeybinds } from "./mso-agent-keybinds.mjs";

function wrapText(text, width) {
  // Multi-word command tokens tested by regression suites are preserved from newline splitting.
  const pattern = /(?:\(\/reasoning on\|off\)|\/reasoning on\|off|\/stash pop|download-only|Tab \(empty prompt\)|\S+)/g;
  const tokens = String(text || "").match(pattern) || [];
  const lines = [];
  let cur = "";
  for (const token of tokens) {
    if (!cur) {
      cur = token;
    } else if (cur.length + 1 + token.length <= width) {
      cur += ` ${token}`;
    } else {
      lines.push(cur);
      cur = token;
    }
  }
  if (cur) lines.push(cur);
  return lines;
}

function formatCommand(cmd, width, colors) {
  const c = colors?.cyan || "";
  const r = colors?.reset || "";
  const dim = colors?.dim || "";
  const label = cmd.usage ? `${cmd.name} ${cmd.usage}` : cmd.name;
  const desc = cmd.description || "";
  // Fit on a single line when command label and description together do not exceed width.
  if (label.length + desc.length + 4 <= width) {
    const pad = (label.length < 24 && 24 + desc.length + 4 <= width) ? 24 : label.length;
    return [`  ${c}${label.padEnd(pad)}${r}  ${dim}${desc}${r}`.trimEnd()];
  }
  // Otherwise wrap description indented under label so narrow terminals remain legible.
  const descLines = wrapText(desc, Math.max(20, width - 4));
  return [`  ${c}${label}${r}`, ...descLines.map((line) => `    ${dim}${line}${r}`)];
}

function formatSettings(settingsList, width, colors) {
  const b = colors?.bold || "";
  const r = colors?.reset || "";
  const dim = colors?.dim || "";
  const c = colors?.cyan || "";
  const lines = ["", `${b}Settings${r} ${dim}(/settings [key] [val])${r}`];
  for (const item of settingsList) {
    const key = item.key || item.name;
    const desc = item.description || "";
    if (key.length + desc.length + 4 <= width) {
      const pad = (key.length < 12 && 12 + desc.length + 4 <= width) ? 12 : key.length;
      lines.push(`  ${c}${key.padEnd(pad)}${r}  ${dim}${desc}${r}`.trimEnd());
    } else {
      const descLines = wrapText(desc, Math.max(20, width - 4));
      lines.push(`  ${c}${key}${r}`, ...descLines.map((line) => `    ${dim}${line}${r}`));
    }
  }
  return lines;
}

function prettyKey(key) {
  // Converts internal lowercase key identifier into canonical human-readable key notation.
  return String(key || "")
    .split("+")
    .map((part) => (part ? part.charAt(0).toUpperCase() + part.slice(1).toLowerCase() : ""))
    .join("+");
}

function formatKeyboard(keybinds, width, colors) {
  const b = colors?.bold || "";
  const r = colors?.reset || "";
  const dim = colors?.dim || "";
  const c = colors?.cyan || "";
  const lines = ["", `${b}Keyboard${r}`];
  const defaults = defaultKeybinds();

  // One-character asterisk marker flags any binding that differs from the default keymap.
  const mark = (action) => (keybinds?.[action] && keybinds[action] !== defaults[action] ? "*" : "");
  // Formats human-readable key with optional customization marker.
  const k = (action, fallback) => `${prettyKey(keybinds?.[action] || fallback)}${mark(action)}`;

  // Preserves canonical (empty prompt) annotation for completion/permission cycling.
  const completeLabel = `${k("complete", "tab")} (empty prompt)`;
  const entries = [
    { label: k("interrupt", "ctrl+c"), desc: "clear input; empty prompt exits; active turn interrupts" },
    { label: "Ctrl+D", desc: "delete right; empty prompt exits" },
    { label: k("clear_screen", "ctrl+l"), desc: "clear/repaint terminal" },
    { label: k("dismiss", "escape"), desc: "dismiss completion menu or cancel" },
    { label: "Ctrl+W · Ctrl+U/K", desc: "delete word · delete to line start/end" },
    { label: k("history_prev", "alt+p"), desc: "previous prompt in history (also ↑)" },
    { label: k("history_next", "alt+n"), desc: "next prompt in history (also ↓)" },
    { label: k("commands", "ctrl+p"), desc: "open the command dialog" },
    { label: "Ctrl+A/E · Ctrl+B/F", desc: "line start/end · left/right" },
    { label: "Alt+B/F · Ctrl+←/→", desc: "move by word" },
    { label: completeLabel, desc: "cycle permission in place: ask → auto → yolo" },
    { label: k("submit", "return"), desc: "submit prompt line or selected item" },
  ];
  // Displays status bar toggle key only when user explicitly configures the optional binding.
  if (keybinds?.toggle_status_bar) {
    entries.push({ label: k("toggle_status_bar", ""), desc: "toggle compact status bar" });
  }
  for (const entry of entries) {
    if (entry.label.length + entry.desc.length + 4 <= width) {
      const pad = (entry.label.length < 20 && 20 + entry.desc.length + 4 <= width) ? 20 : entry.label.length;
      lines.push(`  ${c}${entry.label.padEnd(pad)}${r}  ${dim}${entry.desc}${r}`.trimEnd());
    } else {
      const descLines = wrapText(entry.desc, Math.max(20, width - 4));
      lines.push(`  ${c}${entry.label}${r}`, ...descLines.map((line) => `    ${dim}${line}${r}`));
    }
  }
  return lines;
}

/**
 * @param {{
 *   commands?: Array<{ name: string, usage?: string, description?: string }>,
 *   settings?: Array<{ key?: string, name?: string, description?: string, [key: string]: any }>,
 *   keybinds?: Record<string, string>,
 *   colors?: Record<string, string>,
 *   width?: number,
 * }} [options]
 * @returns {string[]}
 */
export function helpLines({
  commands = COMMAND_MANIFEST,
  settings = SETTINGS,
  keybinds = defaultKeybinds(),
  colors = {},
  width = 60,
} = {}) {
  // Clamps terminal width to 60 columns minimum so compact viewports remain usable.
  const termWidth = Number(width) > 0 ? Number(width) : 60;
  return [
    ...commands.flatMap((cmd) => formatCommand(cmd, termWidth, colors)),
    ...formatSettings(settings, termWidth, colors),
    ...formatKeyboard(keybinds, termWidth, colors),
  ];
}