Skip to content

Add coloured JSON output with --pretty and Ctrl+T - #171

Open
WietseWind wants to merge 1 commit into
websockets:masterfrom
WietseWind:feature/json-pretty-print
Open

Add coloured JSON output with --pretty and Ctrl+T#171
WietseWind wants to merge 1 commit into
websockets:masterfrom
WietseWind:feature/json-pretty-print

Conversation

@WietseWind

@WietseWind WietseWind commented Sep 9, 2026

Copy link
Copy Markdown

Large JSON replies are hard to read on one line. Add optional indentation and colours.

Changes

  • Add --pretty to start with indented, coloured JSON output. Without this
    flag, formatting starts off.
  • Press Ctrl+T to turn formatting off or on for new messages. Show the current
    state. Keep the draft and cursor intact.
  • Use --no-color for indentation only. Do not write colour reset codes when
    colours are disabled.
  • Support connect and listen mode. Keep sent messages, non-JSON messages,
    binary messages, and piped output unchanged.

Demo

JSON formatting and Ctrl+T toggle

Full demo: 13 seconds, 1 MB GIF.

Security overview

  • No npm packages added, removed, or updated. Existing commander, ws,
    https-proxy-agent, and read remain in use.
  • Reuse Node's readline, tty, and event handling. Add the built-in stream
    module for Ctrl+T input handling. Tests also use built-in util.
    These modules ship with Node.js; no extra package downloads or install scripts.
  • Use JSON.parse to validate messages, then format the original tokens with
    string operations and ANSI colours. Do not execute message text.
    Keep large numbers, duplicate keys, and string escapes intact.
    Formatting adds no file writes or network requests.

Validation

  • All 20 tests pass on Node.js 18.20.8 and 24.11.1.
  • Test the standalone packed package in a macOS terminal: startup flag,
    Ctrl+T, exact numbers, cursor position, and ordinary arrow recall.
  • Tests are excluded from the npm package.

@WietseWind
WietseWind force-pushed the feature/json-pretty-print branch from 88b6484 to fd1b4af Compare September 9, 2026 11:24
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