UNPKG

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