* Generate Kitty graphics protocol escape sequence * @param base64Data - Base64 encoded image data * @param options - Display options
(
base64Data: string,
options: {
width?: number // cells
height?: number // cells
id?: number
} = {},
)
| 118 | * @param options - Display options |
| 119 | */ |
| 120 | function generateKittyImageSequence( |
| 121 | base64Data: string, |
| 122 | options: { |
| 123 | width?: number // cells |
| 124 | height?: number // cells |
| 125 | id?: number |
| 126 | } = {}, |
| 127 | ): string { |
| 128 | const { width, height, id } = options |
| 129 | |
| 130 | // Build key-value pairs for the control data |
| 131 | const kvPairs: string[] = [ |
| 132 | 'a=T', // action: transmit and display |
| 133 | 'f=100', // format: PNG (100) - let Kitty auto-detect |
| 134 | 't=d', // transmission: direct (data follows) |
| 135 | ] |
| 136 | |
| 137 | if (width) { |
| 138 | kvPairs.push(`c=${width}`) // columns |
| 139 | } |
| 140 | |
| 141 | if (height) { |
| 142 | kvPairs.push(`r=${height}`) // rows |
| 143 | } |
| 144 | |
| 145 | if (id) { |
| 146 | kvPairs.push(`i=${id}`) // image id |
| 147 | } |
| 148 | |
| 149 | const controlData = kvPairs.join(',') |
| 150 | |
| 151 | // Kitty requires chunked transmission for large images |
| 152 | // For simplicity, we'll send in one chunk if small enough |
| 153 | const CHUNK_SIZE = 4096 |
| 154 | |
| 155 | if (base64Data.length <= CHUNK_SIZE) { |
| 156 | // Single chunk: ESC _ G <control> ; <data> ESC \ |
| 157 | return `\x1b_G${controlData};${base64Data}\x1b\\` |
| 158 | } |
| 159 | |
| 160 | // Multi-chunk transmission |
| 161 | const chunks: string[] = [] |
| 162 | for (let i = 0; i < base64Data.length; i += CHUNK_SIZE) { |
| 163 | const chunk = base64Data.slice(i, i + CHUNK_SIZE) |
| 164 | const isLast = i + CHUNK_SIZE >= base64Data.length |
| 165 | const chunkControl = isLast ? controlData : `${controlData},m=1` // m=1 means more chunks coming |
| 166 | chunks.push(`\x1b_G${chunkControl};${chunk}\x1b\\`) |
| 167 | } |
| 168 | |
| 169 | return chunks.join('') |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Render an image inline in the terminal |