fix: reduce blank rows below Kitty images by using placement c=/r= bounds
Browse filesKitty placement now passes resolved character dimensions (charWidth,
charHeight) to limit the rendered image area. ImageBox uses pixel-derived
charHeight when loaded to avoid reserving too many Yoga rows.
LocalPicture.test.tsx computes pixelWidth/pixelHeight separately from
character dimensions for correct scaling.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Files changed (4)
6 unmodified lines7891011123 unmodified lines16171819202122232425264 unmodified lines3132333435363738394041424344454647489 unmodified lines58596061626364656 unmodified lines** Usage:* bun run src/ink-picture/__tests__/LocalPicture.test.tsx*/import { Jimp } from "jimp";3 unmodified linesconst IMAGE_PATH = "/home/yuki/Pictures/Wallpapers/3god.jpg";function App() {const { exit } = useApp();const [dimensions, setDimensions] = useState<{width: number;height: number;} | null>(null);const [err, setErr] = useState(false);4 unmodified linesconst origW = image.bitmap.width;const origH = image.bitmap.height;const cols = process.stdout.columns ?? 80;const targetW = Math.floor(cols * 0.6);const targetH = Math.floor(targetW * (origH / origW) / 2);setDimensions({ width: targetW, height: targetH });} catch {setErr(true);}})();}, []);useEffect(() => {const timer = setTimeout(() => exit(), 3000);return () => clearTimeout(timer);}, [exit]);if (err) {9 unmodified lines<InkPictureProvider><Imagesrc={IMAGE_PATH}width={dimensions.width}height={dimensions.height}alt="3god"/></InkPictureProvider>6 unmodified lines789101112133 unmodified lines17181920212223242526272829303132334 unmodified lines383940414243444546474849505152535455565758596061626364656667686970717273747576779 unmodified lines878889909192939495966 unmodified lines** Usage:* bun run src/ink-picture/__tests__/LocalPicture.test.tsx* Press Ctrl+C to exit (image persists)*/import { Jimp } from "jimp";3 unmodified linesconst IMAGE_PATH = "/home/yuki/Pictures/Wallpapers/3god.jpg";// 终端字符尺寸(像素)const CELL_WIDTH = 8;const CELL_HEIGHT = 16;function App() {const { exit } = useApp();const [dimensions, setDimensions] = useState<{width: number; // 字符列数height: number; // 字符行数pixelWidth: number; // 像素宽度pixelHeight: number; // 像素高度} | null>(null);const [err, setErr] = useState(false);4 unmodified linesconst origW = image.bitmap.width;const origH = image.bitmap.height;const cols = process.stdout.columns ?? 80;// 1. 目标字符尺寸const targetW_chars = Math.floor(cols * 0.6);// 2. 转为像素尺寸(用于图片缩放)const targetW_pixels = targetW_chars * CELL_WIDTH;const targetH_pixels = Math.floor(targetW_pixels * (origH / origW));// 3. 确保最小高度(至少 3 行字符)const minH_pixels = 3 * CELL_HEIGHT;const finalH_pixels = Math.max(targetH_pixels, minH_pixels);// 4. 转回字符行数(用于 Ink 占位)const targetH_chars = Math.ceil(finalH_pixels / CELL_HEIGHT);setDimensions({width: targetW_chars,height: targetH_chars,pixelWidth: targetW_pixels,pixelHeight: finalH_pixels,});} catch {setErr(true);}})();}, []);// Ctrl+C 直接退出,不清理图片useEffect(() => {const handleSigint = () => {exit();};process.on('SIGINT', handleSigint);return () => process.off('SIGINT', handleSigint);}, [exit]);if (err) {9 unmodified lines<InkPictureProvider><Imagesrc={IMAGE_PATH}width={dimensions.width} // 字符列数(Ink 占位)height={dimensions.height} // 字符行数(Ink 占位)pixelWidth={dimensions.pixelWidth} // 像素宽度(图片缩放)pixelHeight={dimensions.pixelHeight} // 像素高度(图片缩放)alt="3god"/></InkPictureProvider>
2 unmodified lines34567891 unmodified line11121314151617181920212 unmodified linesexport interface ImageBoxProps {width: number | string;height: number | string;alt?: string;error?: boolean;loaded?: boolean;1 unmodified line}const ImageBox = forwardRef<DOMElement, ImageBoxProps>(function ImageBox({ width, height, alt, error, loaded, children },ref,) {return (<Box ref={ref} flexDirection="column" width={width} height={height}>{loaded && children ? (children) : (2 unmodified lines3456789101 unmodified line121314151617181920212223242526272 unmodified linesexport interface ImageBoxProps {width: number | string;height: number | string; // 总高度(占位)imageHeight?: number; // 图片实际像素高度(新增)alt?: string;error?: boolean;loaded?: boolean;1 unmodified line}const ImageBox = forwardRef<DOMElement, ImageBoxProps>(function ImageBox({ width, height, imageHeight, alt, error, loaded, children },ref,) {// 计算图片占用的字符行数const charHeight = typeof imageHeight === 'number'? Math.ceil(imageHeight / 16) // 像素转字符行: height;return (<Box ref={ref} flexDirection="column" width={width} height={loaded ? charHeight : height}>{loaded && children ? (children) : (
77 unmodified lines7879808182838477 unmodified linesstdout.write("\r");stdout.write(cursorForward(componentPosition.col));stdout.write(makeKittyPlacement(imageId, 1));stdout.write("\x1b8");77 unmodified lines787980818283848577 unmodified linesstdout.write("\r");stdout.write(cursorForward(componentPosition.col));// 关键修改:传入 resolvedWidth 和 resolvedHeight(字符尺寸)stdout.write(makeKittyPlacement(imageId, 1, resolvedWidth, resolvedHeight));stdout.write("\x1b8");
30 unmodified lines313233343536373830 unmodified linesexport function makeKittyPlacement(imageId: number,placementId: number = 1,): string {return `\x1b_Ga=p,i=${imageId},p=${placementId},C=1,q=2\x1b\\`;}export function makeKittyDeletion(30 unmodified lines313233343536373839404142434445464748495030 unmodified linesexport function makeKittyPlacement(imageId: number,placementId: number = 1,charWidth?: number, // 新增:字符列数charHeight?: number, // 新增:字符行数): string {let params = `a=p,i=${imageId},p=${placementId},C=1,q=2`;// 新增:限制显示区域if (charWidth !== undefined) {params += `,c=${charWidth}`;}if (charHeight !== undefined) {params += `,r=${charHeight}`;}return `\x1b_G${params}\x1b\\`;}export function makeKittyDeletion(
src/ink-picture/__tests__/LocalPicture.test.tsx+40-9
src/ink-picture/components/ImageBox.tsx+9-3
src/ink-picture/components/image/Kitty.tsx+2-1
src/ink-picture/renderers/kitty.ts+13-1