Skip to content

Add ANSI escape sequence handling for compilation output#140

Open
KlevisImeri wants to merge 5 commits into
ej-shafran:mainfrom
KlevisImeri:feat/ansi-escape-sequence-handling
Open

Add ANSI escape sequence handling for compilation output#140
KlevisImeri wants to merge 5 commits into
ej-shafran:mainfrom
KlevisImeri:feat/ansi-escape-sequence-handling

Conversation

@KlevisImeri
Copy link
Copy Markdown

@KlevisImeri KlevisImeri commented May 17, 2026

Summary

Adds ANSI escape sequence handling to compilation output, inspired by Emacs' ansi-color.el and ansi-osc.el. A new ansi.lua module centralizes all processing: CSI stripping, OSC handling, and SGR color rendering via baleia.

Closes #135

The new config options added are:

  1. ansi_color_for_compilation:
    Controls how ANSI escape sequences are handled:
  • "render" (default): strip non-SGR CSI and OSC, render SGR colors via baleia.nvim
  • "filter": strip all CSI and OSC sequences, write plain text
  • "passthrough": no processing, pass through raw escape sequences
    When "render" is set but baleia.nvim is unavailable, falls back to "filter" with a warning.
  1. osc_handlers:
    A table mapping OSC command numbers to handler functions. Each handler receives the data portion of the OSC sequence and must return a replacement string that
    appears in the buffer. Return "" to strip. Default handlers strip all sequences except OSC 8 (hyperlinks), which renders the URI visible.

Behavior Changes

  • Change the runtime to LuaJIT "Lua.runtime.version": "LuaJIT", which is the one Neovim uses.
  • \e[K and other CSI sequences now stripped: The test in
    carriage_return_spec.lua that expected
    \e[K to appear in buffer output has been updated. Terminal cursor control sequences don't apply to Neovim buffers and are now correctly removed.
  • Incomplete escape sequences at chunk boundaries are buffered and reassembled when the next output chunk arrives. Any remaining partial is flushed when the
    process exits.

CI

  • make typecheck passes
  • make fmt passes
  • make test passes (all existing + 32 new)

- Add PLAN.md with implementation design for porting Emacs' ansi-color behavior
- Add lua/compile-mode/ansi.lua module for ANSI CSI and OSC escape sequence processing
- Addresses issue ej-shafran#135: non-SGR sequences (e.g. OSC 8 hyperlinks) now handled gracefully
- Add ansi.lua module centralizing all ANSI processing
- Three modes: passthrough, filter, render
- CSI non-SGR stripping, OSC stripping, SGR color rendering via baleia
- Partial escape sequence buffering for chunked output
- Configurable OSC handler table for side effects
- Lazy initialization on first buf_set_lines call
- Update config, docs (README + vimdoc), health check
- Fix PARTIAL_CSI_PATTERN missing $ anchor causing complete sequences
 to be incorrectly buffered as partials
- Fix process_osc to return handler return value instead of discarding
 it
- Add ansi.flush() to write remaining partial buffer on process exit
- Centralize modifiable toggle in buf_set_lines with buf_is_valid guard
- Add M.reset() for test isolation
- Call ansi.reset() in test_helpers.setup_tests
- Update osc_handlers to return "" (strip) or URI (OSC 8)
- Fix carriage_return_spec expectation for \e[K stripping
- Set Lua runtime to LuaJIT in .luarc-ci.json for \x escape support
- Add Makefile test/% and test-debug/% targets for individual test files
- Add 32 tests covering filter, passthrough, partial buffering,
 OSC handlers, fallback, and integration
- Update documentation: handler return value, partial buffering note,
 OSC 8 default behavior, moved osc_handlers to end of config
@KlevisImeri KlevisImeri marked this pull request as draft May 17, 2026 14:14
- Replace vim.regex/vim.fn.substitute with Lua string patterns and gsub
- Clean up pattern naming (drop _SUFFIX convention and L_ prefixes)
- Fix render mode negative index handling for baleia extmarks
- Expose strip functions (_strip_csi, _strip_non_sgr_csi, _strip_osc) for testing
- Add assert_output, assert_buffer, assert_render test helpers
- Add pattern correctness and render mode test suites
@KlevisImeri KlevisImeri marked this pull request as ready for review May 17, 2026 18:45
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.

[BUG] cargo run --color always prints weird characters

1 participant