Skip to content

Add sight console: Stata's console with highlighting, completion and colors - #335

Merged
jbearak merged 3 commits into
mainfrom
t3/plan-sight-console
Oct 30, 2026
Merged

jbearak merged 3 commits into
mainfrom
t3/plan-sight-console

Conversation

@jbearak

@jbearak jbearak commented Oct 30, 2026

Copy link
Copy Markdown
Owner

Console Stata is bare: no syntax highlighting, no completion, no history, plain output. Raven's raven console fixed that for R. This PR adds the Stata equivalent, sight console. It's simpler than Raven's: Stata can't be embedded, so there's no debugger. Sight runs the Stata console binary in a pseudo-terminal and wraps it, like a smarter rlwrap stata-mp.

What you get:

  • Highlighting as you type, from Sight's lexer.
  • Tab completion from Sight's completion provider, run over the lines you entered this session. It completes commands, options, locals, globals, generated variables, and paths. Lines that errored are left out.
  • History across sessions, in a private file. Lines starting with a space aren't saved.
  • Colored output (best-effort): the message before r(NNN); and --Break-- in red, note:/warning: in yellow, and do-file echo lines highlighted.

Design points:

  • A line goes to Stata only on Enter, whether or not a prompt was recognized. That way _request() input and typing ahead behave as in plain console Stata.
  • A recognized prompt (exact match after 50 ms of quiet) adds highlighting and completion, and loads the next staged pasted line. A staged line never runs without Enter.
  • Bun's PTY is not the child's controlling terminal (TPGID 0), so Ctrl+C and resizes signal Stata directly (SIGINT, SIGWINCH).
  • Stata 18's console puts its terminal in raw mode and echoes input itself. Sight keeps its highlighted copy of each line and drops Stata's plain echo, but only an exact copy right after the submission (allowing for the \r\n> Stata inserts when it wraps a long line). Anything else is shown unchanged.
  • The console needs Bun's PTY, which the native binary has. Under Node (npm), interactive use exits with a message, and piped input passes straight through to Stata.

It's marked experimental. VS Code integration is left for a follow-up.

Proof:

  • Ran the compiled native binary against StataMP 18 on macOS (a separate Mac). Lines show once and highlighted, long lines included. Errors and --Break-- are red.
  • Tab completion on that run: `my → `myname', summ → summarize, de → detail.
  • Ctrl+C stops sleep 10000. --more-- takes one key (space continues, q breaks). A resize changed c(linesize) from 79 to 49.
  • _request, foreach blocks, Mata and Python prompts, typing ahead, staged paste, and Ctrl+D to quit all worked. The history file has mode 600.
  • The real-Stata run caught the double echo, which a raw mode in the fake Stata (tests/fixtures/console/fake-stata.ts) now reproduces.
  • Unit and session tests, plus real-PTY integration tests that drive the CLI against the fake (including SIGTERM reaping and Node passthrough).
  • bun run typecheck, bun run lint and bun run check:line-endings pass. bun test ./tests passes except help-topic-coverage, which needs a full local Stata install and fails the same way on main on the build machine.
  • Linux is tested only against the fake.

Reviewed with sol-loop: 3 rounds with gpt-6.1-sol. Sol verdict: approved

Created with Claude Opus 5.5 in Claude Code (T3 Code); plan reviewed by Claude Sonnet and GPT-6.1 Terra.

`sight console` runs the Stata console binary (stata-mp, stata-se, ...)
in a pseudo-terminal and wraps it, in the spirit of Raven's console:
syntax highlighting as you type (Sight's lexer), Tab completion (Sight's
completion provider over the lines entered this session), persistent
history, and best-effort output colors (errors before `r(NNN);` and
`--Break--` red, notes yellow, do-file echo lines highlighted).

Input follows the rlwrap model: a line goes to Stata only on Enter,
whether or not a prompt was recognized, so `_request()` input and
typeahead work as in plain console Stata. A recognized prompt only
adds highlighting, completion, and loading of the next staged pasted
line, which never runs without Enter.

Bun's PTY is not the child's controlling terminal, so Ctrl+C and
resizes signal Stata directly (SIGINT, SIGWINCH) rather than relying on
the line discipline. Echo is turned off through the PTY's termios; if
Stata turns it back on, the console shows each line once.

The PTY needs Bun (the native binary); under Node, interactive use
exits with a message and piped input passes straight through to Stata.
Tests run the real CLI in a PTY against a fake Stata fixture.
Record `end` entered at a Mata or Python prompt so the transcript's
embedded block closes, and keep a running command's failure tracking
when the user types ahead. History no longer changes the mode of an
existing directory.
Stata 18's console puts its terminal in raw mode and echoes input
itself, so every line showed twice. When Stata echoes (raw mode or
echo on), drop exactly one copy of the submitted line from the start
of its output, allowing for the wrap marker Stata inserts in long
lines; anything else is shown unchanged. Lines typed while Stata is
busy are left for Stata to echo when it reads them. Also reset the
cursor-key mode Stata turns on, and add a raw mode to the fake Stata.

Verified against StataMP 18 on macOS.
@coderabbitai

coderabbitai Bot commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

Next included review available in 57 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available. Your 74 included PR review attempts over the past 7 days set your current allowance at 1 review per hour.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Team
  • Run ID: 93ce23d6-db45-4655-b63c-a19df5d0735e
Commits

Reviewing files that changed from the base of the PR and between 1d73376 and 2f9be79.

Files selected for processing: 25
  • README.md
  • docs/cli.md
  • docs/console.md
  • src/cli.ts
  • src/console/args.ts
  • src/console/completer.ts
  • src/console/highlighter.ts
  • src/console/history.ts
  • src/console/index.ts
  • src/console/interactive.ts
  • src/console/keys.ts
  • src/console/line-editor.ts
  • src/console/output-colorizer.ts
  • src/console/prompt-detector.ts
  • src/console/pty.ts
  • src/console/render.ts
  • src/console/session.ts
  • src/console/stata-locator.ts
  • tests/fixtures/console/fake-stata.ts
  • tests/integration/console/console-pty.test.ts
  • tests/integration/console/screen.ts
  • tests/unit/console/editing.test.ts
  • tests/unit/console/session.test.ts
  • tests/unit/console/setup.test.ts
  • tests/unit/console/text-processing.test.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@jbearak
jbearak merged commit f5cbc46 into main Oct 30, 2026
13 checks passed
@jbearak
jbearak deleted the t3/plan-sight-console branch October 30, 2026 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant