UNPKG

3.87 kBMarkdownView Raw
1# terminal-image
2
3> Display images in the terminal
4
5Works in any terminal that supports colors.
6
7<img src="screenshot.png" width="1082">
8
9*In iTerm, the image will be [displayed in full resolution](screenshot-iterm.jpg), since iTerm has [special image support](https://www.iterm2.com/documentation-images.html).*
10
11## Install
12
13```
14$ npm install terminal-image
15```
16
17## Usage
18
19```js
20import terminalImage from 'terminal-image';
21
22console.log(await terminalImage.file('unicorn.jpg'));
23```
24
25Optionally, you can specify the `height` and/or `width` to scale the image. That can be either the percentage of the terminal window or number of rows and/or columns. Please note that the image will always be scaled to fit the size of the terminal. If width and height are not defined, by default the image will take the width and height of the terminal.
26
27It is recommended to use the percentage option.
28
29```js
30import terminalImage from 'terminal-image';
31
32console.log(await terminalImage.file('unicorn.jpg', {width: '50%', height: '50%'}));
33```
34
35You can set width and/or height as columns and/or rows of the terminal window as well.
36
37```js
38import terminalImage from 'terminal-image';
39
40console.log(await terminalImage.file('unicorn.jpg', {width: 50}));
41```
42
43By default, aspect ratio is always maintained. If you don't want to maintain aspect ratio, set `preserveAspectRatio` to false. However, your image will be scaled to fit the size of the terminal.
44
45```js
46import terminalImage from 'terminal-image';
47
48console.log(await terminalImage.file('unicorn.jpg', {width: 70, height: 50, preserveAspectRatio: false}));
49```
50
51## API
52
53Supports PNG and JPEG images. Animated GIFs are also supported with `.gifBuffer` and `.gifFile`.
54
55### terminalImage.buffer(imageBuffer, options?)
56### terminalImage.file(filePath, options?)
57
58Returns a `Promise<string>` with the ANSI escape codes to display the image.
59
60### terminalImage.gifBuffer(imageBuffer, options?)
61### terminalImage.gifFile(filePath, options?)
62
63Returns a function that can be called to stop the GIF animation.
64
65#### options
66
67Type: `object`
68
69##### height
70
71Type: `string | number`
72
73Custom image height.
74
75Can be set as percentage or number of rows of the terminal. It is recommended to use the percentage options.
76
77##### width
78
79Type: `string | number`
80
81Custom image width.
82
83Can be set as percentage or number of columns of the terminal. It is recommended to use the percentage options.
84
85##### preserveAspectRatio
86
87Type: `boolean`\
88Default: `true`
89
90Whether to maintain image aspect ratio or not.
91
92##### maximumFrameRate
93
94**Only works for `terminalImage.gifBuffer` or `terminalImage.gifFile`**
95
96Type: `number`\
97Default: `30`
98
99Maximum framerate to render the GIF. This option is ignored when using iTerm.
100
101##### renderFrame
102
103**Only works for `terminalImage.gifBuffer` or `terminalImage.gifFile`**
104
105Type: `(text: string) => void`\
106Default: [log-update](https://github.com/sindresorhus/log-update)
107
108Custom handler which is run for each frame of the GIF.
109
110This can be set to change how each frame is shown.
111
112##### renderFrame.done
113
114**Only works for `terminalImage.gifBuffer` or `terminalImage.gifFile`**
115
116Type: `() => void`\
117Default: [log-update](https://github.com/sindresorhus/log-update)
118
119Custom handler which is run when the animation playback is stopped.
120
121This can be set to perform a cleanup when playback has finished.
122
123## Tip
124
125### Display a remote image
126
127```js
128import terminalImage from 'terminal-image';
129import got from 'got';
130
131const body = await got('https://sindresorhus.com/unicorn').buffer();
132console.log(await terminalImage.buffer(body));
133```
134
135## Related
136
137- [terminal-image-cli](https://github.com/sindresorhus/terminal-image-cli) - CLI for this module
138- [terminal-link](https://github.com/sindresorhus/terminal-link) - Create clickable links in the terminal
139- [chalk](https://github.com/chalk/chalk) - Style and color text in the terminal