@oh-my-pi/pi-tui 13.2.1 → 13.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@oh-my-pi/pi-tui",
4
- "version": "13.2.1",
4
+ "version": "13.3.0",
5
5
  "description": "Terminal User Interface library with differential rendering for efficient text-based applications",
6
6
  "homepage": "https://github.com/can1357/oh-my-pi",
7
7
  "author": "Can Boluk",
@@ -33,8 +33,8 @@
33
33
  "test": "bun test test/*.test.ts"
34
34
  },
35
35
  "dependencies": {
36
- "@oh-my-pi/pi-natives": "13.2.1",
37
- "@oh-my-pi/pi-utils": "13.2.1",
36
+ "@oh-my-pi/pi-natives": "13.3.0",
37
+ "@oh-my-pi/pi-utils": "13.3.0",
38
38
  "@types/mime-types": "^3.0",
39
39
  "chalk": "^5.6",
40
40
  "marked": "^17.0",
@@ -56,7 +56,10 @@ export class Image implements Component {
56
56
  let lines: string[];
57
57
 
58
58
  if (TERMINAL.imageProtocol) {
59
- const result = renderImage(this.#base64Data, this.#dimensions, { maxWidthCells: maxWidth });
59
+ const result = renderImage(this.#base64Data, this.#dimensions, {
60
+ maxWidthCells: maxWidth,
61
+ maxHeightCells: this.#options.maxHeightCells,
62
+ });
60
63
 
61
64
  if (result) {
62
65
  // Return `rows` lines so TUI accounts for image height
@@ -214,6 +214,35 @@ export function calculateImageRows(
214
214
  return Math.max(1, rows);
215
215
  }
216
216
 
217
+ function calculateImageFit(
218
+ imageDimensions: ImageDimensions,
219
+ options: ImageRenderOptions,
220
+ cellDims: CellDimensions,
221
+ ): { columns: number; rows: number } {
222
+ const maxColumns = options.maxWidthCells !== undefined ? Math.max(1, Math.floor(options.maxWidthCells)) : undefined;
223
+ const maxRows = options.maxHeightCells !== undefined ? Math.max(1, Math.floor(options.maxHeightCells)) : undefined;
224
+
225
+ if (maxColumns === undefined && maxRows === undefined) {
226
+ const columns = Math.max(1, Math.ceil(imageDimensions.widthPx / cellDims.widthPx));
227
+ const rows = Math.max(1, Math.ceil(imageDimensions.heightPx / cellDims.heightPx));
228
+ return { columns, rows };
229
+ }
230
+
231
+ const maxWidthPx = maxColumns !== undefined ? maxColumns * cellDims.widthPx : Number.POSITIVE_INFINITY;
232
+ const maxHeightPx = maxRows !== undefined ? maxRows * cellDims.heightPx : Number.POSITIVE_INFINITY;
233
+ const scale = Math.min(maxWidthPx / imageDimensions.widthPx, maxHeightPx / imageDimensions.heightPx);
234
+ const fittedWidthPx = imageDimensions.widthPx * scale;
235
+ const fittedHeightPx = imageDimensions.heightPx * scale;
236
+
237
+ const columns = Math.max(1, Math.floor(fittedWidthPx / cellDims.widthPx));
238
+ const rows = Math.max(1, Math.ceil(fittedHeightPx / cellDims.heightPx));
239
+
240
+ return {
241
+ columns: maxColumns !== undefined ? Math.min(columns, maxColumns) : columns,
242
+ rows: maxRows !== undefined ? Math.min(rows, maxRows) : rows,
243
+ };
244
+ }
245
+
217
246
  export function getPngDimensions(base64Data: string): ImageDimensions | null {
218
247
  try {
219
248
  const buffer = Buffer.from(base64Data, "base64");
@@ -364,21 +393,23 @@ export function renderImage(
364
393
  return null;
365
394
  }
366
395
 
367
- const maxWidth = options.maxWidthCells ?? 80;
368
- const rows = calculateImageRows(imageDimensions, maxWidth, getCellDimensions());
396
+ const fit = calculateImageFit(imageDimensions, options, getCellDimensions());
369
397
 
370
398
  if (TERMINAL.imageProtocol === ImageProtocol.Kitty) {
371
- const sequence = encodeKitty(base64Data, { columns: maxWidth, rows });
372
- return { sequence, rows };
399
+ const sequence = encodeKitty(base64Data, {
400
+ columns: fit.columns,
401
+ rows: fit.rows,
402
+ });
403
+ return { sequence, rows: fit.rows };
373
404
  }
374
405
 
375
406
  if (TERMINAL.imageProtocol === ImageProtocol.Iterm2) {
376
407
  const sequence = encodeITerm2(base64Data, {
377
- width: maxWidth,
408
+ width: fit.columns,
378
409
  height: "auto",
379
410
  preserveAspectRatio: options.preserveAspectRatio ?? true,
380
411
  });
381
- return { sequence, rows };
412
+ return { sequence, rows: fit.rows };
382
413
  }
383
414
 
384
415
  return null;