-
Notifications
You must be signed in to change notification settings - Fork 326
add captureSpans and getSpanLines for styled terminal output capture #574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+237
−1
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b35b422
add captureSpans and getSpanLines for styled terminal output capture
remorses d280a03
remove unused VTermData import
remorses b340c0f
format
remorses 3d253be
refactor: use RGBA class and rgbToHex from lib instead of inline func…
remorses 1dd866c
refactor: use existing types instead of VTerm types, add RGBA.equals()
remorses 1136691
style: format
remorses 31303e0
Merge branch 'main' into capture-spans-upstream
kommander File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import { describe, test, expect, beforeEach, afterEach } from "bun:test" | ||
| import { createTestRenderer, type TestRenderer } from "./test-renderer" | ||
| import { TextRenderable } from "../renderables/Text" | ||
| import { BoxRenderable } from "../renderables/Box" | ||
| import { TextAttributes, type CapturedFrame } from "../types" | ||
| import { RGBA } from "../lib" | ||
|
|
||
| describe("captureSpans", () => { | ||
| let renderer: TestRenderer | ||
| let renderOnce: () => Promise<void> | ||
| let captureSpans: () => CapturedFrame | ||
|
|
||
| beforeEach(async () => { | ||
| const setup = await createTestRenderer({ width: 40, height: 10 }) | ||
| renderer = setup.renderer | ||
| renderOnce = setup.renderOnce | ||
| captureSpans = setup.captureSpans | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| renderer.destroy() | ||
| }) | ||
|
|
||
| test("returns correct dimensions and line count", async () => { | ||
| await renderOnce() | ||
| const data = captureSpans() | ||
|
|
||
| expect(data.cols).toBe(40) | ||
| expect(data.rows).toBe(10) | ||
| expect(data.lines.length).toBe(10) | ||
| }) | ||
|
|
||
| test("captures text content in spans", async () => { | ||
| const text = new TextRenderable(renderer, { content: "Hello World" }) | ||
| renderer.root.add(text) | ||
| await renderOnce() | ||
|
|
||
| const data = captureSpans() | ||
| const firstLine = data.lines[0] | ||
| const textContent = firstLine.spans.map((s) => s.text).join("") | ||
|
|
||
| expect(textContent).toContain("Hello World") | ||
| }) | ||
|
|
||
| test("groups consecutive cells with same styling into single span", async () => { | ||
| const text = new TextRenderable(renderer, { content: "AAAA" }) | ||
| renderer.root.add(text) | ||
| await renderOnce() | ||
|
|
||
| const data = captureSpans() | ||
| const firstLine = data.lines[0] | ||
| const aaaSpan = firstLine.spans.find((s) => s.text.includes("AAAA")) | ||
|
|
||
| expect(aaaSpan).toBeDefined() | ||
| expect(aaaSpan!.width).toBeGreaterThanOrEqual(4) | ||
| }) | ||
|
|
||
| test("captures foreground color", async () => { | ||
| const text = new TextRenderable(renderer, { | ||
| content: "Red Text", | ||
| fg: RGBA.fromHex("#ff0000"), | ||
| }) | ||
| renderer.root.add(text) | ||
| await renderOnce() | ||
|
|
||
| const data = captureSpans() | ||
| const firstLine = data.lines[0] | ||
| const redSpan = firstLine.spans.find((s) => s.text.includes("Red")) | ||
|
|
||
| expect(redSpan).toBeDefined() | ||
| expect(redSpan!.fg.r).toBe(1) | ||
| expect(redSpan!.fg.g).toBe(0) | ||
| expect(redSpan!.fg.b).toBe(0) | ||
| }) | ||
|
|
||
| test("captures background color", async () => { | ||
| const box = new BoxRenderable(renderer, { | ||
| width: 10, | ||
| height: 3, | ||
| backgroundColor: RGBA.fromHex("#00ff00"), | ||
| }) | ||
| renderer.root.add(box) | ||
| await renderOnce() | ||
|
|
||
| const data = captureSpans() | ||
| const secondLine = data.lines[1] | ||
| const greenSpan = secondLine.spans.find((s) => s.bg.g === 1 && s.bg.r === 0 && s.bg.b === 0) | ||
|
|
||
| expect(greenSpan).toBeDefined() | ||
| }) | ||
|
|
||
| test("returns alpha 0 for transparent colors", async () => { | ||
| await renderOnce() | ||
|
|
||
| const data = captureSpans() | ||
| const firstLine = data.lines[0] | ||
| const transparentSpan = firstLine.spans.find((s) => s.bg.a === 0) | ||
|
|
||
| expect(transparentSpan).toBeDefined() | ||
| }) | ||
|
|
||
| test("captures text attributes", async () => { | ||
| const text = new TextRenderable(renderer, { | ||
| content: "Styled", | ||
| attributes: TextAttributes.BOLD | TextAttributes.ITALIC | TextAttributes.UNDERLINE | TextAttributes.DIM, | ||
| }) | ||
| renderer.root.add(text) | ||
| await renderOnce() | ||
|
|
||
| const data = captureSpans() | ||
| const firstLine = data.lines[0] | ||
| const styledSpan = firstLine.spans.find((s) => s.text.includes("Styled")) | ||
|
|
||
| expect(styledSpan).toBeDefined() | ||
| expect(styledSpan!.attributes & TextAttributes.BOLD).toBeTruthy() | ||
| expect(styledSpan!.attributes & TextAttributes.ITALIC).toBeTruthy() | ||
| expect(styledSpan!.attributes & TextAttributes.UNDERLINE).toBeTruthy() | ||
| expect(styledSpan!.attributes & TextAttributes.DIM).toBeTruthy() | ||
| }) | ||
|
|
||
| test("includes cursor position", async () => { | ||
| await renderOnce() | ||
| const data = captureSpans() | ||
|
|
||
| expect(data.cursor).toEqual([expect.any(Number), expect.any(Number)]) | ||
| }) | ||
|
|
||
| test("splits spans when styling changes", async () => { | ||
| const text1 = new TextRenderable(renderer, { | ||
| content: "AAA", | ||
| fg: RGBA.fromHex("#ff0000"), | ||
| }) | ||
| const text2 = new TextRenderable(renderer, { | ||
| content: "BBB", | ||
| fg: RGBA.fromHex("#00ff00"), | ||
| }) | ||
| renderer.root.add(text1) | ||
| renderer.root.add(text2) | ||
| await renderOnce() | ||
|
|
||
| const data = captureSpans() | ||
| const allSpans = data.lines.flatMap((l) => l.spans) | ||
|
|
||
| expect(allSpans.some((s) => s.fg.r === 1 && s.fg.g === 0)).toBe(true) | ||
| expect(allSpans.some((s) => s.fg.g === 1 && s.fg.r === 0)).toBe(true) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably needs some epsilon for comparing floats moving forward, but fine for now.