fix(ImageShowTool): use terminal height instead of width for dimension calculation
Browse filesSwitch primary dimension from terminal columns to terminal rows so the
image scales proportionally regardless of terminal orientation.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
src/tools/ImageShowTool/ImageShowTool.ts
CHANGED
|
@@ -64,24 +64,25 @@ export async function loadImage(path: string) {
|
|
| 64 |
export function calculateDimensions(
|
| 65 |
imageWidth: number,
|
| 66 |
imageHeight: number,
|
| 67 |
-
|
| 68 |
): ImageDimensions {
|
| 69 |
-
|
| 70 |
-
const
|
| 71 |
-
const targetH_pixels =
|
| 72 |
-
const
|
| 73 |
|
| 74 |
-
//
|
| 75 |
-
//
|
| 76 |
// placeholder Box and the actual Kitty image.
|
| 77 |
-
const
|
| 78 |
-
const
|
|
|
|
| 79 |
|
| 80 |
return {
|
| 81 |
width: targetW_chars,
|
| 82 |
height: targetH_chars,
|
| 83 |
-
pixelWidth:
|
| 84 |
-
pixelHeight:
|
| 85 |
};
|
| 86 |
}
|
| 87 |
|
|
@@ -175,11 +176,11 @@ export const ImageShowTool = buildTool({
|
|
| 175 |
async call({ src }) {
|
| 176 |
try {
|
| 177 |
const image = await loadImage(src)
|
| 178 |
-
const
|
| 179 |
const dims = calculateDimensions(
|
| 180 |
image.bitmap.width,
|
| 181 |
image.bitmap.height,
|
| 182 |
-
|
| 183 |
)
|
| 184 |
|
| 185 |
// Generate full-resolution Kitty protocol sequence via timg
|
|
|
|
| 64 |
export function calculateDimensions(
|
| 65 |
imageWidth: number,
|
| 66 |
imageHeight: number,
|
| 67 |
+
terminalRows: number
|
| 68 |
): ImageDimensions {
|
| 69 |
+
// Height = 16.18% of terminal height; width derived from aspect ratio.
|
| 70 |
+
const targetH_chars = Math.floor(terminalRows * 0.1618);
|
| 71 |
+
const targetH_pixels = targetH_chars * CELL_HEIGHT;
|
| 72 |
+
const minW_chars = 3;
|
| 73 |
|
| 74 |
+
// Pixel width from aspect ratio, then back-compute char width to guarantee
|
| 75 |
+
// pixelWidth === width * CELL_WIDTH β no rounding gap between the
|
| 76 |
// placeholder Box and the actual Kitty image.
|
| 77 |
+
const targetW_pixels = Math.floor(targetH_pixels * (imageWidth / imageHeight));
|
| 78 |
+
const targetW_chars = Math.max(Math.ceil(targetW_pixels / CELL_WIDTH), minW_chars);
|
| 79 |
+
const finalPixelWidth = targetW_chars * CELL_WIDTH;
|
| 80 |
|
| 81 |
return {
|
| 82 |
width: targetW_chars,
|
| 83 |
height: targetH_chars,
|
| 84 |
+
pixelWidth: finalPixelWidth,
|
| 85 |
+
pixelHeight: targetH_pixels,
|
| 86 |
};
|
| 87 |
}
|
| 88 |
|
|
|
|
| 176 |
async call({ src }) {
|
| 177 |
try {
|
| 178 |
const image = await loadImage(src)
|
| 179 |
+
const rows = process.stdout.rows ?? 24
|
| 180 |
const dims = calculateDimensions(
|
| 181 |
image.bitmap.width,
|
| 182 |
image.bitmap.height,
|
| 183 |
+
rows,
|
| 184 |
)
|
| 185 |
|
| 186 |
// Generate full-resolution Kitty protocol sequence via timg
|
src/tools/ImageShowTool/UI.tsx
CHANGED
|
@@ -154,13 +154,8 @@ export function ImageDisplay({
|
|
| 154 |
|
| 155 |
// ββ Tool rendering functions ββ
|
| 156 |
|
| 157 |
-
export function renderToolUseMessage(
|
| 158 |
-
|
| 159 |
-
{ verbose }: { theme?: string; verbose: boolean },
|
| 160 |
-
): React.ReactNode {
|
| 161 |
-
if (!src) return null;
|
| 162 |
-
if (verbose) return `src: "${src}"`;
|
| 163 |
-
return src;
|
| 164 |
}
|
| 165 |
|
| 166 |
export function renderToolUseProgressMessage(): React.ReactNode {
|
|
|
|
| 154 |
|
| 155 |
// ββ Tool rendering functions ββ
|
| 156 |
|
| 157 |
+
export function renderToolUseMessage(): React.ReactNode {
|
| 158 |
+
return "ImageShow";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
}
|
| 160 |
|
| 161 |
export function renderToolUseProgressMessage(): React.ReactNode {
|
src/tools/ImageShowTool/standalone.tsx
CHANGED
|
@@ -36,7 +36,7 @@ function App() {
|
|
| 36 |
const dims = calculateDimensions(
|
| 37 |
image.bitmap.width,
|
| 38 |
image.bitmap.height,
|
| 39 |
-
process.stdout.
|
| 40 |
);
|
| 41 |
setDimensions(dims);
|
| 42 |
} catch (e) {
|
|
|
|
| 36 |
const dims = calculateDimensions(
|
| 37 |
image.bitmap.width,
|
| 38 |
image.bitmap.height,
|
| 39 |
+
process.stdout.rows ?? 24,
|
| 40 |
);
|
| 41 |
setDimensions(dims);
|
| 42 |
} catch (e) {
|