-
-
Notifications
You must be signed in to change notification settings - Fork 1
✨ Add glyph-cell text backgrounds #93
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
Open
rauhryan
wants to merge
5
commits into
main
Choose a base branch
from
rr/text-bg-support-76
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
740bef4
✨ Add glyph-cell text backgrounds
rauhryan d206613
chore: update the render-spec
rauhryan 1547c89
Add a very usage of text bg in the examples
rauhryan 6a9933c
Adds a demo that looks like a code diff to show all the different col…
rauhryan dd496b4
adds test to verify the bg color is reset have the directive
rauhryan 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| /** | ||
| * Diff demo — renders a code-review style diff with inline text backgrounds. | ||
| * | ||
| * Shows how text-level `bg` can highlight only changed glyph cells without | ||
| * filling the entire row or layout box. | ||
| */ | ||
|
|
||
| import { Buffer } from "node:buffer"; | ||
| import process from "node:process"; | ||
| import { | ||
| close, | ||
| createTerm, | ||
| CSI, | ||
| fixed, | ||
| grow, | ||
| type Op, | ||
| open, | ||
| rgba, | ||
| text, | ||
| } from "../../mod.ts"; | ||
| import { validated } from "../../validate.ts"; | ||
|
|
||
| const encode = (s: string) => new TextEncoder().encode(s); | ||
| const write = (b: Uint8Array) => process.stdout.write(Buffer.from(b)); | ||
|
|
||
| const BG = rgba(31, 45, 34); | ||
| const FG = rgba(220, 220, 210); | ||
| const MUTED = rgba(140, 145, 140); | ||
| const PATH = rgba(139, 210, 210); | ||
| const DELETE = rgba(255, 105, 105); | ||
| const DELETE_BG = rgba(184, 92, 85); | ||
| const ADD = rgba(190, 210, 100); | ||
| const ADD_BG = rgba(190, 210, 100); | ||
| const INLINE_FG = rgba(31, 45, 34); | ||
|
|
||
| const rows: Row[] = [ | ||
| { | ||
| kind: "title", | ||
| segments: [ | ||
| { value: "edit ", color: FG }, | ||
| { value: "examples/inline-regions/index.ts", color: PATH }, | ||
| ], | ||
| }, | ||
| { kind: "blank" }, | ||
| code(" ", MUTED, [{ value: "...", color: MUTED }]), | ||
| code(" 35", MUTED, [ | ||
| { | ||
| value: | ||
| "const write = (b: Uint8Array) => process.stdout.write(Buffer.from(b));", | ||
| }, | ||
| ]), | ||
| code(" 36", MUTED, [{ value: "" }]), | ||
| code(" 37", MUTED, [{ value: "const WHITE = rgba(255, 255, 255);" }]), | ||
| code(" 38", MUTED, [{ value: "const GREEN = rgba(80, 250, 123);" }]), | ||
| code("- 39", DELETE, [ | ||
| { value: "const " }, | ||
| mark("AGREEN", DELETE_BG), | ||
| { value: " = rgba(" }, | ||
| mark("80", DELETE_BG), | ||
| { value: ", " }, | ||
| mark("250", DELETE_BG), | ||
| { value: ", " }, | ||
| mark("123, 10", DELETE_BG), | ||
| { value: ");" }, | ||
| ]), | ||
| code("+ 39", ADD, [ | ||
| { value: "const " }, | ||
| mark("GREEN_BG", ADD_BG), | ||
| { value: " = rgba(" }, | ||
| mark("20", ADD_BG), | ||
| { value: ", " }, | ||
| mark("70", ADD_BG), | ||
| { value: ", " }, | ||
| mark("38", ADD_BG), | ||
| { value: ");" }, | ||
| ]), | ||
| code(" 40", MUTED, [{ value: "const GRAY = rgba(100, 100, 100);" }]), | ||
| code(" 41", MUTED, [{ value: "const CYAN = rgba(139, 233, 253);" }]), | ||
| code(" 42", MUTED, [{ value: "" }]), | ||
| code(" 43", MUTED, [{ value: "const RED = rgba(255, 0, 0);" }]), | ||
| code(" ", MUTED, [{ value: "...", color: MUTED }]), | ||
| code(" 136", MUTED, [{ value: " height: fixed(1)," }]), | ||
| code(" 137", MUTED, [{ value: ' direction: "ltr",' }]), | ||
| code(" 138", MUTED, [{ value: " }," }]), | ||
| code(" 139", MUTED, [{ value: " })," }]), | ||
| code("-140", DELETE, [ | ||
| { value: ' text(" ✓ Frobnicated ", { color: WHITE, bg: ' }, | ||
| mark("AGREEN", DELETE_BG), | ||
| { value: " })," }, | ||
| ]), | ||
| code("+140", ADD, [ | ||
| { value: ' text(" ✓ Frobnicated ", { color: WHITE, bg: ' }, | ||
| mark("GREEN_BG", ADD_BG), | ||
| { value: " })," }, | ||
| ]), | ||
| code(" 141", MUTED, [{ value: " close()," }]), | ||
| code(" 142", MUTED, [{ value: " ];" }]), | ||
| code(" 143", MUTED, [{ value: " }" }]), | ||
| code(" 144", MUTED, [{ value: " let progress = i / (barFrames - 1);" }]), | ||
| code(" ", MUTED, [{ value: "...", color: MUTED }]), | ||
| ]; | ||
|
|
||
| let { columns } = terminalSize(); | ||
| let height = rows.length + 2; | ||
| let term = validated(await createTerm({ width: columns, height })); | ||
| let result = term.render(renderDiff(columns, height), { mode: "line" }); | ||
| write(new Uint8Array(result.output)); | ||
| write(CSI("0m")); | ||
| write(encode("\n")); | ||
|
|
||
| interface Segment { | ||
| value: string; | ||
| color?: number; | ||
| bg?: number; | ||
| } | ||
|
|
||
| interface Row { | ||
| kind: "blank" | "code" | "title"; | ||
| gutter?: string; | ||
| color?: number; | ||
| segments?: Segment[]; | ||
| } | ||
|
|
||
| function code(gutter: string, color: number, segments: Segment[]): Row { | ||
| return { kind: "code", gutter, color, segments }; | ||
| } | ||
|
|
||
| function mark(value: string, bg: number): Segment { | ||
| return { value, color: INLINE_FG, bg }; | ||
| } | ||
|
|
||
| function renderDiff(width: number, height: number): Op[] { | ||
| let ops: Op[] = [ | ||
| open("root", { | ||
| layout: { | ||
| width: fixed(width), | ||
| height: fixed(height), | ||
| direction: "ttb", | ||
| padding: { left: 1, top: 1 }, | ||
| }, | ||
| bg: BG, | ||
| }), | ||
| ]; | ||
|
|
||
| rows.forEach((row, index) => { | ||
| ops.push( | ||
| open(`row-${index}`, { | ||
| layout: { width: grow(), height: fixed(1), direction: "ltr" }, | ||
| }), | ||
| ); | ||
|
|
||
| if (row.kind === "blank") { | ||
| ops.push(close()); | ||
| return; | ||
| } | ||
|
|
||
| if (row.kind === "title") { | ||
| for (let segment of row.segments ?? []) { | ||
| ops.push(text(segment.value, { color: segment.color })); | ||
| } | ||
| ops.push(close()); | ||
| return; | ||
| } | ||
|
|
||
| ops.push(text(`${row.gutter ?? ""} `, { color: row.color })); | ||
| for (let segment of row.segments ?? []) { | ||
| ops.push( | ||
| text(segment.value, { | ||
| color: segment.color ?? row.color, | ||
| bg: segment.bg, | ||
| }), | ||
| ); | ||
| } | ||
| ops.push(close()); | ||
| }); | ||
|
|
||
| ops.push(close()); | ||
| return ops; | ||
| } | ||
|
|
||
| function terminalSize(): { columns: number; rows: number } { | ||
| return process.stdout.isTTY | ||
| ? { | ||
| columns: process.stdout.columns ?? 100, | ||
| rows: process.stdout.rows ?? 24, | ||
| } | ||
| : { columns: 100, rows: 24 }; | ||
| } |
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
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,74 @@ | ||
| import { beforeEach, describe, expect, it } from "./suite.ts"; | ||
| import { createTerm, type Term } from "../term.ts"; | ||
| import { close, grow, open, rgba, text } from "../ops.ts"; | ||
|
|
||
| const decode = (b: Uint8Array) => new TextDecoder().decode(b); | ||
|
|
||
| type TextBgColor = { | ||
| value: number; | ||
| sgr: string; | ||
| }; | ||
|
|
||
| function randomTextBgColor(): TextBgColor { | ||
| let r = 0; | ||
| let g = 0; | ||
| let b = 0; | ||
|
|
||
| do { | ||
| r = Math.floor(Math.random() * 256); | ||
| g = Math.floor(Math.random() * 256); | ||
| b = Math.floor(Math.random() * 256); | ||
| } while ( | ||
| (r === 255 && g === 0 && b === 0) || | ||
| (r === 0 && g === 255 && b === 0) || | ||
| (r === 0 && g === 0 && b === 255) | ||
| ); | ||
|
|
||
| return { | ||
| value: rgba(r, g, b), | ||
| sgr: `\x1b[48;2;${r};${g};${b}`, | ||
| }; | ||
| } | ||
|
|
||
| describe("text background color", () => { | ||
| let term: Term; | ||
|
|
||
| beforeEach(async () => { | ||
| term = await createTerm({ width: 20, height: 1 }); | ||
| }); | ||
|
|
||
| it("fills glyph cells with the requested text-level bg", () => { | ||
| let bg = randomTextBgColor(); | ||
| let ansi = decode( | ||
| term.render([ | ||
| open("root", { layout: { width: grow(), height: grow() } }), | ||
| text("Hi", { bg: bg.value }), | ||
| close(), | ||
| ]).output, | ||
| ); | ||
|
|
||
| let beforeH = ansi.slice(0, ansi.indexOf("H")); | ||
| expect(beforeH).toContain(bg.sgr); | ||
| }); | ||
|
|
||
| it("resets the background before writing trailing cells", () => { | ||
| let bg = randomTextBgColor(); | ||
| let ansi = decode( | ||
| term.render([ | ||
| open("root", { layout: { width: grow(), height: grow() } }), | ||
| text("Hi", { bg: bg.value }), | ||
| close(), | ||
| ]).output, | ||
| ); | ||
|
|
||
| let beforeH = ansi.slice(0, ansi.indexOf("H")); | ||
| expect(beforeH).toContain(bg.sgr); | ||
|
|
||
| let hi = ansi.indexOf("Hi"); | ||
| expect(hi).toBeGreaterThanOrEqual(0); | ||
|
|
||
| let afterHi = ansi.slice(hi + 2); | ||
| expect(afterHi).not.toContain(bg.sgr); | ||
| expect(afterHi.startsWith("\x1b[0m ")).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
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.
we're consolidating on
test/color.test.tsfor foreground/background behavior. feel free to rename this