UNPKG

8.64 kBTypeScriptView Raw
1export type RenderFrame = {
2 /**
3 Custom handler which is run when the animation playback is stopped.
4
5 This can be set to perform a cleanup when playback has finished.
6 */
7 done?: () => void;
8
9 /**
10 Custom handler which is run for each frame of the GIF.
11
12 This can be set to change how each frame is shown.
13
14 @param text - The frame which should be rendered.
15 */
16 (text: string): void;
17};
18
19declare const terminalImage: {
20 /**
21 Display images in the terminal.
22
23 Optionally, you can specify the height and/or width to scale the image.
24 That can be either the percentage of the terminal window or number of rows and/or columns.
25 Please note that the image will always be scaled to fit the size of the terminal.
26 If width and height are not defined, by default the image will take the width and height of the terminal.
27 It is recommended to use the percentage option.
28 You can set width and/or height as columns and/or rows of the terminal window as well.
29 By default, aspect ratio is always maintained. If you don't want to maintain aspect ratio, set preserveAspectRatio to false.
30
31 @param imageBuffer - Buffer with the image.
32 @param options - Image rendering options.
33 @param options.width - Optional: Custom image width. Can be set as percentage or number of columns of the terminal. It is recommended to use the percentage options.
34 @param options.height - Optional: Custom image height. Can be set as percentage or number of rows of the terminal. It is recommended to use the percentage options.
35 @param options.preserveAspectRatio - Optional: Whether to maintain image aspect ratio or not. Default: true.
36 @returns The ansi escape codes to display the image.
37
38 @example
39 ```
40 import terminalImage from 'terminal-image';
41 import got from 'got';
42
43 const body = await got('https://sindresorhus.com/unicorn').buffer();
44 console.log(await terminalImage.buffer(body));
45 console.log(await terminalImage.buffer(body, {width: '50%', height: '50%'}));
46 console.log(await terminalImage.buffer(body, {width: 50 }));
47 console.log(await terminalImage.buffer(body, {width: 70, height: 50, preserveAspectRatio: false}));
48 ```
49 */
50 buffer: (imageBuffer: Readonly<Buffer>, options?: Readonly<{
51 width?: string | number;
52 height?: string | number;
53 preserveAspectRatio?: boolean;
54 }>) => Promise<string>;
55
56 /**
57 Display images in the terminal. Please note that the image will always be scaled to fit the size of the terminal.
58
59 Optionally, you can specify the height and/or width to scale the image.
60 That can be either the percentage of the terminal window or number of rows and/or columns.
61 Please note that the image will always be scaled to fit the size of the terminal.
62 If width and height are not defined, by default the image will take the width and height of the terminal.
63 It is recommended to use the percentage option.
64 You can set width and/or height as columns and/or rows of the terminal window as well.
65 By default, aspect ratio is always maintained. If you don't want to maintain aspect ratio, set preserveAspectRatio to false.
66
67 @param filePath - File path to the image.
68 @param options - Image rendering options.
69 @param options.width - Optional: Custom image width. Can be set as percentage or number of columns of the terminal. It is recommended to use the percentage options.
70 @param options.height - Optional: Custom image height. Can be set as percentage or number of rows of the terminal. It is recommended to use the percentage options.
71 @param options.preserveAspectRatio - Optional: Whether to maintain image aspect ratio or not. Default: true.
72 @returns The ANSI escape codes to display the image.
73
74 @example
75 ```
76 import terminalImage from 'terminal-image';
77
78 console.log(await terminalImage.file('unicorn.jpg'));
79 console.log(await terminalImage.file('unicorn.jpg', {width: '50%', height: '50%'}));
80 console.log(await terminalImage.file('unicorn.jpg', {width: 50 }));
81 console.log(await terminalImage.file('unicorn.jpg', {width: 70, height: 50, preserveAspectRatio: false}));
82 ```
83 */
84 file: (
85 filePath: string,
86 options?: Readonly<{
87 width?: string | number;
88 height?: string | number;
89 preserveAspectRatio?: boolean;
90 }>
91 ) => Promise<string>;
92
93 /**
94 Display GIFs in the terminal.
95
96 Optionally, you can specify the height and/or width to scale the image.
97 That can be either the percentage of the terminal window or number of rows and/or columns.
98 Please note that the image will always be scaled to fit the size of the terminal.
99 If width and height are not defined, by default the image will take the width and height of the terminal.
100 It is recommended to use the percentage option.
101 You can set width and/or height as columns and/or rows of the terminal window as well.
102 By default, aspect ratio is always maintained. If you don't want to maintain aspect ratio, set preserveAspectRatio to false.
103 Each frame of the GIF is by default printed to the terminal, overwriting the previous one. To change this behavior, set `renderFrame` to a different function. To change the code run when the animation playback is stopped, set `renderFrame.done` to a different function.
104
105 @param imageBuffer - Buffer with the image.
106 @param options - Image rendering options.
107 @param options.width - Optional: Custom image width. Can be set as percentage or number of columns of the terminal. It is recommended to use the percentage options.
108 @param options.height - Optional: Custom image height. Can be set as percentage or number of rows of the terminal. It is recommended to use the percentage options.
109 @param options.maximumFrameRate - Optional: Maximum framerate to render the GIF. This option is ignored when using iTerm. Defaults to 30.
110 @param options.renderFrame - Optional: Custom handler which is run for each frame of the GIF.
111 @param options.renderFrame.done - Optional: Custom handler which is run when the animation playback is stopped.
112 @returns A function that can be called to stop the GIF animation.
113
114 @example
115 ```
116 import {setTimeout} from 'node:timers/promises';
117 import fs from 'node:fs/promises';
118 import terminalImage from 'terminal-image';
119
120 const gifData = await fs.readFile('unicorn.gif');
121 const stopAnimation = terminalImage.gifBuffer(gifData);
122
123 await delay(5000);
124 stopAnimation();
125 ```
126 */
127 gifBuffer: (imageBuffer: Readonly<Buffer>, options?: Readonly<{
128 width?: string | number;
129 height?: string | number;
130 maximumFrameRate?: number;
131 renderFrame?: RenderFrame;
132 }>) => () => void;
133
134 /**
135 Display gifs in the terminal.
136
137 Optionally, you can specify the height and/or width to scale the image.
138 That can be either the percentage of the terminal window or number of rows and/or columns.
139 Please note that the image will always be scaled to fit the size of the terminal.
140 If width and height are not defined, by default the image will take the width and height of the terminal.
141 It is recommended to use the percentage option.
142 You can set width and/or height as columns and/or rows of the terminal window as well.
143 By default, aspect ratio is always maintained. If you don't want to maintain aspect ratio, set preserveAspectRatio to false.
144 Each frame of the gif is by default logged to the terminal, overwriting the previous one. To change this behaviour, set renderFrame to a different function. To change the code run when the animation playback is stopped, set renderFrame.done to a different function.
145
146 @param imageBuffer - Buffer with the image.
147 @param options - Image rendering options.
148 @param options.width - Optional: Custom image width. Can be set as percentage or number of columns of the terminal. It is recommended to use the percentage options.
149 @param options.height - Optional: Custom image height. Can be set as percentage or number of rows of the terminal. It is recommended to use the percentage options.
150 @param options.maximumFrameRate - Optional: Maximum framerate to render the GIF. This option is ignored by iTerm. Defaults to 30.
151 @param options.renderFrame - Optional: Custom handler which is run for each frame of the gif.
152 @param options.renderFrame.done - Optional: Custom handler which is run when the animation playback is stopped.
153 @returns A function that can be called to stop the gif animation.
154
155 @example
156 ```
157 import {setTimeout} from 'node:timers/promises';
158 import terminalImage from 'terminal-image';
159
160 const stopAnimation = terminalImage.gifFile('unicorn.gif');
161
162 await setTimeout(5000);
163 stopAnimation();
164 ```
165 */
166 gifFile: (
167 filePath: string,
168 options?: Readonly<{
169 width?: string | number;
170 height?: string | number;
171 maximumFrameRate?: number;
172 renderFrame?: RenderFrame;
173 }>
174 ) => () => void;
175};
176
177export default terminalImage;