Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | 5x 5x 5x 5x 1x 1x 1x 10x 10x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 60x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x | const KITTY_GRAPHICS_ESCAPE_PREFIX = '\u001b_G';
const KITTY_GRAPHICS_ESCAPE_SUFFIX = '\u001b\\';
const DEFAULT_BASE64_CHUNK_SIZE = 4096;
const encodedChunkCache = new WeakMap<KittyGraphicsImage, Map<number, string[]>>();
export interface KittyGraphicsImage {
pixelWidth: number;
pixelHeight: number;
cellWidth: number;
cellHeight: number;
rgb: Uint8Array;
}
export function supportsKittyGraphicsProtocol(env: NodeJS.ProcessEnv = process.env): boolean {
const term = env.TERM?.toLowerCase() ?? '';
const termProgram = env.TERM_PROGRAM?.toLowerCase() ?? '';
return (
typeof env.KITTY_WINDOW_ID === 'string' ||
term.includes('kitty') ||
term.includes('ghostty') ||
termProgram === 'ghostty'
);
}
export function buildKittyGraphicsDeleteImageSequence(imageId: number): string {
const safeImageId = Math.max(1, Math.floor(imageId));
return `${KITTY_GRAPHICS_ESCAPE_PREFIX}a=d,d=I,i=${safeImageId},q=2${KITTY_GRAPHICS_ESCAPE_SUFFIX}`;
}
export function buildKittyGraphicsRenderSequence(options: {
imageId: number;
placementId?: number;
row: number;
column: number;
image: KittyGraphicsImage;
zIndex?: number;
doNotMoveCursor?: boolean;
chunkSize?: number;
}): string {
const safeImageId = Math.max(1, Math.floor(options.imageId));
const safePlacementId =
typeof options.placementId === 'number' && Number.isFinite(options.placementId)
? Math.max(1, Math.floor(options.placementId))
: undefined;
const safeRow = Math.max(1, Math.floor(options.row));
const safeColumn = Math.max(1, Math.floor(options.column));
const safePixelWidth = Math.max(1, Math.floor(options.image.pixelWidth));
const safePixelHeight = Math.max(1, Math.floor(options.image.pixelHeight));
const safeCellWidth = Math.max(1, Math.floor(options.image.cellWidth));
const safeCellHeight = Math.max(1, Math.floor(options.image.cellHeight));
const zIndex =
typeof options.zIndex === 'number' && Number.isFinite(options.zIndex) ? Math.floor(options.zIndex) : undefined;
const chunkSize = Math.max(256, Math.floor(options.chunkSize ?? DEFAULT_BASE64_CHUNK_SIZE));
const chunks = resolveEncodedChunks(options.image, chunkSize);
const parameters = [
'a=T',
't=d',
'f=24',
`s=${safePixelWidth}`,
`v=${safePixelHeight}`,
`c=${safeCellWidth}`,
`r=${safeCellHeight}`,
`i=${safeImageId}`,
'q=2',
safePlacementId ? `p=${safePlacementId}` : '',
zIndex !== undefined ? `z=${zIndex}` : '',
options.doNotMoveCursor ? 'C=1' : '',
]
.filter((value) => value.length > 0)
.join(',');
const commands = chunks
.map((chunk, index) => {
const more = index + 1 < chunks.length ? 1 : 0;
const prefix = index === 0 ? `${parameters},m=${more}` : `m=${more}`;
return `${KITTY_GRAPHICS_ESCAPE_PREFIX}${prefix};${chunk}${KITTY_GRAPHICS_ESCAPE_SUFFIX}`;
})
.join('');
return `\u001b[${safeRow};${safeColumn}H${commands}`;
}
function resolveEncodedChunks(image: KittyGraphicsImage, chunkSize: number): string[] {
const cachedByChunkSize = encodedChunkCache.get(image);
const cachedChunks = cachedByChunkSize?.get(chunkSize);
Iif (cachedChunks) {
return cachedChunks;
}
const chunks = splitBase64IntoChunks(Buffer.from(image.rgb).toString('base64'), chunkSize);
const nextCachedByChunkSize = cachedByChunkSize ?? new Map<number, string[]>();
nextCachedByChunkSize.set(chunkSize, chunks);
Eif (!cachedByChunkSize) {
encodedChunkCache.set(image, nextCachedByChunkSize);
}
return chunks;
}
function splitBase64IntoChunks(base64: string, chunkSize: number): string[] {
Iif (base64.length <= 0) {
return [''];
}
const chunks: string[] = [];
for (let index = 0; index < base64.length; index += chunkSize) {
chunks.push(base64.slice(index, index + chunkSize));
}
return chunks;
}
|