Skip to content

Conversation

@UdeshyaDhungana
Copy link
Contributor

@UdeshyaDhungana UdeshyaDhungana commented Nov 28, 2025

  • Add feature for Grep/Highlighting extension in emulated grep

Note

Adds --color highlighting (always/never/auto with TTY) to emulated grep and introduces VT-based VT/ANSI assertions, stages, and fixtures to validate highlighting behavior.

  • Emulated Grep:
    • Add --color support (always|never|auto) with TTY-aware behavior via EmulationOptions.
    • Highlight matches in stdin results; keep -o behavior unchanged.
  • Assertions & VT:
    • New HighlightingAssertion with VT-based comparison of text and ANSI styles (bold red only), plus detailed mismatch logs.
    • Implement screen-state comparator and validators; utility helpers for ANSI/color names and messages.
    • Introduce virtual_terminal package (VT emulator, screen state, CRLF translation).
  • Tests & Stages:
    • Add highlighting test stages bm2, eq0, wg2, jk4, na5 with fixtures and failure scenarios; integrate into stages_test.go and Makefile target test_highlighting_with_grep.
    • Extend grep tests to cover color modes and TTY; update test-case runners to use new emulation options.
  • Course & Utils:
    • Add "Highlighting" extension and related stages to course definition.
    • Add HighlightString and ColorMode utilities.
  • Deps & Env:
    • Add Charmbracelet ultraviolet, x/vt, ANSI-related deps; update tester-utils and others.
    • Local Dockerfile installs Python and minor tweaks.
  • Misc:
    • Slight error-message tweak in OrderedLinesAssertion.

Written by Cursor Bugbot for commit 777260c. This will update automatically on new commits. Configure here.

@UdeshyaDhungana UdeshyaDhungana changed the title Make emulated grep support --color arg Make emulated grep support --color argument Nov 28, 2025
pattern: "\\d",
input: "a1b",
colorMode: "always",
expected: Result{ExitCode: 0, Stdout: []byte("a" + utils.HighlightString("1") + "b")},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to avoid logic in assertions - hardcode the expected bytes here. That way we're protected against accidental changes to the logic inside utils.HighlightString.

UdeshyaDhungana and others added 25 commits December 11, 2025 20:05
Add multiple lines stage in highlighting extension (3/3)
…xtures

Record fixtures for highlighting assertion (2/3)
if r.hasCursor() {
// If the cursor is on the line, we need to preserve the spaces before the cursor
contentsBeforeCursor := rawCellContents[:r.cursorCellIndex]
contentsAfterCursor := strings.TrimRight(rawCellContents[r.cursorCellIndex:], " ")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Cell index used as byte offset for string slicing

The getTextContents() function uses r.cursorCellIndex as a byte offset when slicing rawCellContents, but cursorCellIndex is a cell/column index, not a byte position. Since rawCellContents is built by concatenating cell.Content values (which can contain multi-byte Unicode characters), using the cell index as a byte offset produces incorrect slices or invalid UTF-8 strings when processing text with non-ASCII characters.

Fix in Cursor Fix in Web


for _, match := range matchedStrings {
matchIdx := strings.Index(remaining, match)
beforeMatch := remaining[:matchIdx]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Missing bounds check causes panic when match not found

The highlightMatches function uses strings.Index(remaining, match) to find match positions but doesn't handle the case where it returns -1. If matchIdx is -1, the expression remaining[:matchIdx] becomes remaining[:-1] which is invalid in Go and causes a panic. While the matchedStrings come from regex matches that should exist in the line, there's no defensive check for edge cases like unexpected matcher behavior or overlapping matches.

Fix in Cursor Fix in Web

@UdeshyaDhungana UdeshyaDhungana merged commit 1fef97c into main Dec 15, 2025
2 checks passed
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.

3 participants