← Catalog

pi-shell-highlight

Shell command highlighting for pi's permission dialogs, used by pi-keeper: command names stand out and heredocs get their own language colors.

Extension

Install

Install with PiG

pig install npm:pi-shell-highlight

Install with Pi

pi install npm:pi-shell-highlight
Review before installing

Packages run code with your permissions and influence agent behavior. Catalog inclusion is not a security audit. Review the source before installing a third-party package.

Pi manifest JSON

The pi field of package.json.

{
  "extensions": [
    "./extension.ts"
  ]
}

README

Written by the package's publisher, shown as published in version 0.1.0 (README.md). PiG does not review it.

pi-shell-highlight

A pi extension that publishes a shell command highlighter for the process. It draws nothing itself: another package that shows a shell command asks for it through a process-global registry. pi-keeper's permission dialog is the consumer.

pi's own highlighter is a grammar: it cannot know that git, tar or rm are commands, and it paints a heredoc body as one string. This package adds:

  • the word in command position is colored (line start, after |, &&, ;, (, {, and after a prefix such as sudo, env, xargs, time, rtk, with its options skipped);
  • three weights: a precommand (sudo, env, rtk) recedes, the command stands out, and a runner (python, node, go, cargo, npm, make, …) reads apart from a plain tool such as grep;
  • options (-n, --json) get their own color;
  • a heredoc body is highlighted with the language its delimiter or opening command names, else the language it looks like, else shell (with command names colored).

Install

pi install npm:pi-shell-highlight    # every session; writes ~/.pi/agent/settings.json

pi update --extensions picks up new releases. On its own it draws nothing: install it with a consumer. pi-keeper picks it up by itself; /keeper → command-colors switches to another style (builtin-syntax is pi's own highlighter). From a checkout: pi -e ./pi-shell-highlight.

The contract

import { HIGHLIGHTER_KEY, type CommandHighlighter, type CommandStyle } from "pi-shell-highlight/highlight.ts";

const highlighter = (globalThis as Record<symbol, unknown>)[HIGHLIGHTER_KEY] as CommandHighlighter | undefined;
const lines = highlighter?.highlightCommand(command, "bash", style);
  • The key is Symbol.for("pi.shell-highlight.v1"); v1 is the contract version. The registry lives on globalThis because pi clears the module cache per child process.
  • highlightCommand(text, lang, style) returns one string per input line, same order, with ANSI codes, and never changes the visible characters.
  • style wraps spans: command, plain, and the optional variable, runner, precommand, option. Each returns a self-contained span so the caller keeps its theme. A missing optional slot falls back (runner and precommand to command, option to the grammar's color).
  • A consumer must validate the shape and fall back to pi's highlightCode when this package is not loaded. pi-keeper does that in extensions/shell-highlight-bridge.ts.

Heredoc languages

The delimiter names the language, then a part of it (PY_EOF → python), then the opening command (psql … <<EOF → sql). Otherwise the body is read for a shebang, a JSON shape, or lines only one language opens with; if none match, it is shell.

DelimiterLanguageDelimiterLanguage
PY, PY3, PYTHONpythonGOgo
SQLsqlRB, RUBYruby
JSONjsonRS, RUSTrust
YAML, YMLyamlPHPphp
SH, BASH, SHELLbashPL, PERLperl
JS, NODEjavascriptLUAlua
TStypescriptDOCKER, DOCKERFILEdockerfile
HTML, XML, CSShtml, xml, cssMD, INI, TF, HCLmarkdown, ini, hcl

Limits

  • MAX_HIGHLIGHT_LINES (400) caps each shell run and each heredoc body; the tail stays plain.
  • One heredoc opener per line: cat <<A <<B takes the first.
  • A prose heredoc (git commit -F - <<'EOF') is read as shell and gets the first word of each line colored. Use a prose delimiter such as <<MD to avoid that.
  • No disk access: no path or glob coloring, no unknown-command detection.

License

Apache-2.0, see LICENSE and NOTICE.