-
Notifications
You must be signed in to change notification settings - Fork 237
Preliminary image support #386
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
Draft
acoliver
wants to merge
11
commits into
sst:main
Choose a base branch
from
acoliver:feature/terminal-images
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.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
552c771
Add preliminary terminal image support
acoliver 3317e4c
Merge branch 'main' into feature/terminal-images
acoliver 52061c3
Fix react workspace dependency
acoliver c8ebb3c
Fix nested scrollbox clipping by intersecting scissor rects
acoliver 1b05443
Merge remote-tracking branch 'acoliver/feature/clip-fix' into feature…
acoliver 0e123c2
Merge branch 'main' into feature/terminal-images
acoliver fabb670
Merge origin/main into feature/terminal-images
acoliver 2dbbcca
Fix renderer image formatting and types
acoliver f40c04b
Pin Bun to 1.2.2 for CI stability and switch image loading to jimp
acoliver 2e9ca16
Use latest Bun with serial test concurrency for CI
acoliver 23f0ef9
Fix Jimp import for TS declarations
acoliver 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 |
|---|---|---|
|
|
@@ -227,6 +227,18 @@ | |
|
|
||
| "@opentui/core": ["@opentui/core@workspace:packages/core"], | ||
|
|
||
| "@opentui/core-darwin-arm64": ["@opentui/[email protected]", "", { "os": "darwin", "cpu": "arm64" }, "sha512-zlOdbzzFwNMqBqiWCvzDC/VMs1Gsjgp7Wogoh3bZYYxYyjpCVGcEjP38hY7Db36HezYhPiUnlgg9hK9Tby7hHQ=="], | ||
|
|
||
| "@opentui/core-darwin-x64": ["@opentui/[email protected]", "", { "os": "darwin", "cpu": "x64" }, "sha512-GBLbF4rvDToZoLTIV5GTWElXulAdirz52YOrfEOMkpub2DTXaXidqyJTVrNrbc+qq4ca8jzYmTeZTnZ9pPd6NQ=="], | ||
|
|
||
| "@opentui/core-linux-arm64": ["@opentui/[email protected]", "", { "os": "linux", "cpu": "arm64" }, "sha512-sVv6PbOxZsZLqSSwJ9R51SQaurvRUzoGJslGWickpxk5QaGHDDxj01H4gR8LfVsRK0HeEdLGvPZLFI7csRn/5A=="], | ||
|
|
||
| "@opentui/core-linux-x64": ["@opentui/[email protected]", "", { "os": "linux", "cpu": "x64" }, "sha512-7fM7V1SKY3iYa1kDMmOHaDskziOzFFnEzkHcfuoLBK4qxJX196GNfH3xi5URQl/JBhavzHqctmv4KvMPOE5PMQ=="], | ||
|
|
||
| "@opentui/core-win32-arm64": ["@opentui/[email protected]", "", { "os": "win32", "cpu": "arm64" }, "sha512-oUzb43lv8dx0oijLE2WItciAcE4+c+p78Cl922T3UDyYXUqNP0r3sZDV+Fo3j8XrgpmRWXBj1s7P5igIEGjMUw=="], | ||
|
|
||
| "@opentui/core-win32-x64": ["@opentui/[email protected]", "", { "os": "win32", "cpu": "x64" }, "sha512-oxx55mF6pbC9ARqy4gAmchqFXvCLsEOGQmwMrF5xS8KsBGp5As9tB1ZEYRa6zqiPhAHyp/dAv5OgO6Xvcy2Hew=="], | ||
|
|
||
| "@opentui/react": ["@opentui/react@workspace:packages/react"], | ||
|
|
||
| "@opentui/solid": ["@opentui/solid@workspace:packages/solid"], | ||
|
|
@@ -247,7 +259,7 @@ | |
|
|
||
| "@types/bun": ["@types/[email protected]", "", { "dependencies": { "bun-types": "1.3.4" } }, "sha512-EEPTKXHP+zKGPkhRLv+HI0UEX8/o+65hqARxLy8Ov5rIxMBPNTjeZww00CIihrIQGEQBYg+0roO5qOnS/7boGA=="], | ||
|
|
||
| "@types/node": ["@types/[email protected].1", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-GNWcUTRBgIRJD5zj+Tq0fKOJ5XZajIiBroOF0yvj2bSU1WvNdYS/dn9UxwsujGW4JX06dnHyjV2y9rRaybH0iQ=="], | ||
| "@types/node": ["@types/[email protected].2", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-WOhQTZ4G8xZ1tjJTvKOpyEVSGgOTvJAfDK3FNFgELyaTpzhdgHVHeqW8V+UJvzF5BT+/B54T/1S2K6gd9c7bbA=="], | ||
|
|
||
| "@types/react": ["@types/[email protected]", "", { "dependencies": { "csstype": "^3.0.2" } }, "sha512-cMoR+FoAf/Jyq6+Df2/Z41jISvGZZ2eTlnsaJRptmZ76Caldwy1odD4xTr/gNV9VLj0AWgg/nmkevIyUfIIq5w=="], | ||
|
|
||
|
|
@@ -529,6 +541,8 @@ | |
|
|
||
| "babel-plugin-jsx-dom-expressions/@babel/helper-module-imports": ["@babel/[email protected]", "", { "dependencies": { "@babel/types": "^7.18.6" } }, "sha512-0NFvs3VkuSYbFi1x2Vd6tKrywq+z/cLeYC/RJNFrIX/30Bf5aiGYbtvGXolEktzJH8o5E5KJ3tT+nkxuuZFVlA=="], | ||
|
|
||
| "bun-types/@types/node": ["@types/[email protected]", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-GNWcUTRBgIRJD5zj+Tq0fKOJ5XZajIiBroOF0yvj2bSU1WvNdYS/dn9UxwsujGW4JX06dnHyjV2y9rRaybH0iQ=="], | ||
|
|
||
| "image-q/@types/node": ["@types/[email protected]", "", {}, "sha512-QpLcX9ZSsq3YYUUnD3nFDY8H7wctAhQj/TFKL8Ya8v5fMm3CFXxo8zStsLAl780ltoYoo1WvKUVGBQK+1ifr7g=="], | ||
|
|
||
| "path-scurry/lru-cache": ["[email protected]", "", {}, "sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ=="], | ||
|
|
||
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,41 @@ | ||
| import { env, registerEnvVar } from "../lib/env" | ||
| import type { RenderContext } from "../types" | ||
|
|
||
| export type ImageProtocol = "kitty" | "iterm2" | "none" | ||
|
|
||
| export interface GraphicsSupport { | ||
| readonly protocol: ImageProtocol | ||
| } | ||
|
|
||
| registerEnvVar({ | ||
| name: "OTUI_PREFER_KITTY_GRAPHICS", | ||
| description: "Force-enable kitty graphics protocol when available.", | ||
| type: "boolean", | ||
| default: false, | ||
| }) | ||
|
|
||
| export function detectGraphicsSupport(): GraphicsSupport { | ||
| const termProgram = process.env["TERM_PROGRAM"] ?? "" | ||
| const term = process.env["TERM"] ?? "" | ||
| if (termProgram === "iTerm.app") { | ||
| return { protocol: "iterm2" } | ||
| } | ||
| if (term.toLowerCase().includes("kitty") || env.OTUI_PREFER_KITTY_GRAPHICS) { | ||
| return { protocol: "kitty" } | ||
| } | ||
| return { protocol: "none" } | ||
| } | ||
|
|
||
| export function encodeItermImage(image: Buffer, widthPx: number, heightPx: number): string { | ||
| const base64 = image.toString("base64") | ||
| return `\u001b]1337;File=inline=1;width=${widthPx}px;height=${heightPx}px;preserveAspectRatio=1:${base64}\u0007` | ||
| } | ||
|
|
||
| export function encodeKittyImage(id: number, image: Buffer, widthPx: number, heightPx: number): string { | ||
| const base64 = image.toString("base64") | ||
| return `\u001b_Gf=100,a=T,s=${widthPx},v=${heightPx},i=${id};${base64}\u001b\\` | ||
| } | ||
|
|
||
| export function encodeKittyDelete(id: number): string { | ||
| return `\u001b_Ga=d,d=0,i=${id}\u001b\\` | ||
| } | ||
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,54 @@ | ||
| import { Renderable, type RenderableOptions } from "../Renderable" | ||
| import type { RenderContext } from "../types" | ||
| import type { OptimizedBuffer } from "../buffer" | ||
| import { RGBA, parseColor } from "../lib/RGBA" | ||
| import type { GraphicsSupport } from "../graphics/protocol" | ||
|
|
||
| export type ImageFit = "contain" | "cover" | "fill" | ||
|
|
||
| export interface ImageOptions extends RenderableOptions<ImageRenderable> { | ||
| src?: string | Buffer | ||
| alt?: string | ||
| width?: number | ||
| height?: number | ||
| fit?: ImageFit | ||
| pixelWidth?: number | ||
| pixelHeight?: number | ||
| } | ||
|
|
||
| export class ImageRenderable extends Renderable { | ||
| src?: string | Buffer | ||
| alt?: string | ||
| fit: ImageFit | ||
| pixelWidth?: number | ||
| pixelHeight?: number | ||
| constructor(ctx: RenderContext, options: ImageOptions) { | ||
| super(ctx, options) | ||
| this.src = options.src | ||
| this.alt = options.alt | ||
| this.fit = options.fit ?? "contain" | ||
| this.width = options.width ?? 0 | ||
| this.height = options.height ?? 0 | ||
| this.pixelWidth = options.pixelWidth | ||
| this.pixelHeight = options.pixelHeight | ||
| } | ||
|
|
||
| protected renderSelf(buffer: OptimizedBuffer, _deltaTime: number): void { | ||
| const width = Math.max(this.width, 0) | ||
| const height = Math.max(this.height, 0) | ||
| if (width === 0 || height === 0) return | ||
|
|
||
| // Clear the target area so previous frame contents do not bleed through | ||
| buffer.fillRect(this.x, this.y, width, height, RGBA.fromInts(0, 0, 0, 0)) | ||
|
|
||
| const graphics = (this._ctx.graphicsSupport ?? null) as GraphicsSupport | null | ||
| const shouldShowFallback = !graphics || graphics.protocol === "none" || !this.src | ||
| if (!shouldShowFallback) return | ||
|
|
||
| const fallback = this.alt ?? "" | ||
| if (fallback.length === 0) return | ||
|
|
||
| const trimmed = fallback.slice(0, Math.max(width, 1)) | ||
| buffer.drawText(trimmed, this.x, this.y, parseColor("#A0A0A0")) | ||
| } | ||
| } |
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.
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.
I think there is a better way to detect kitty graphics support, there should be already something in the codebase