UNPKG

4.09 kBMarkdownView Raw
1# terminal-image [![Build Status](https://travis-ci.com/sindresorhus/terminal-image.svg?branch=master)](https://travis-ci.com/sindresorhus/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
20const terminalImage = require('terminal-image');
21
22(async () => {
23 console.log(await terminalImage.file('unicorn.jpg'));
24})();
25```
26
27Optionally, 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.
28
29It is recommended to use the percentage option.
30
31```js
32const terminalImage = require('terminal-image');
33
34(async () => {
35 console.log(await terminalImage.file('unicorn.jpg', {width: '50%', height: '50%'}));
36})();
37```
38
39You can set width and/or height as columns and/or rows of the terminal window as well.
40
41```js
42const terminalImage = require('terminal-image');
43
44(async () => {
45 console.log(await terminalImage.file('unicorn.jpg', {width: 50}));
46})();
47```
48
49By 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.
50
51```js
52const terminalImage = require('terminal-image');
53
54(async () => {
55 console.log(await terminalImage.file('unicorn.jpg', {width: 70, height: 50, preserveAspectRatio: false}));
56})();
57```
58
59## API
60
61Supports PNG and JPEG images. Animated GIFs are also supported with `.gifBuffer` and `.gifFile`.
62
63### terminalImage.buffer(imageBuffer, options?)
64### terminalImage.file(filePath, options?)
65### terminalImage.gifBuffer(imageBuffer, options?)
66### terminalImage.gifFile(filePath, options?)
67
68Returns a `Promise<string>` with the ansi escape codes to display the image.
69
70#### options
71
72Type: `object`
73
74##### height
75
76Type: `string | number`
77
78Custom image height.
79
80Can be set as percentage or number of rows of the terminal. It is recommended to use the percentage options.
81
82##### width
83
84Type: `string | number`
85
86Custom image width.
87
88Can be set as percentage or number of columns of the terminal. It is recommended to use the percentage options.
89
90##### preserveAspectRatio
91
92Type: `boolean`\
93Default: `true`
94
95Whether to maintain image aspect ratio or not.
96
97##### maximumFrameRate
98
99**Only works for `terminalImage.gifBuffer` or `terminalImage.gifFile`**
100
101Type: `number`\
102Default: `30`
103
104Maximum framerate to render the GIF. This option is ignored when using iTerm.
105
106##### renderFrame
107
108**Only works for `terminalImage.gifBuffer` or `terminalImage.gifFile`**
109
110Type: `(text: string) => void`\
111Default: [log-update](https://github.com/sindresorhus/log-update)
112
113Custom handler which is run for each frame of the GIF.
114
115This can be set to change how each frame is shown.
116
117##### renderFrame.done
118
119**Only works for `terminalImage.gifBuffer` or `terminalImage.gifFile`**
120
121Type: `() => void`\
122Default: [log-update](https://github.com/sindresorhus/log-update)
123
124Custom handler which is run when the animation playback is stopped.
125
126This can be set to perform a cleanup when playback has finished.
127
128## Tip
129
130### Display a remote image
131
132```js
133const terminalImage = require('terminal-image');
134const got = require('got');
135
136(async () => {
137 const body = await got('https://sindresorhus.com/unicorn').buffer();
138 console.log(await terminalImage.buffer(body));
139})();
140```
141
142## Related
143
144- [terminal-image-cli](https://github.com/sindresorhus/terminal-image-cli) - CLI for this module
145- [terminal-link](https://github.com/sindresorhus/terminal-link) - Create clickable links in the terminal
146- [chalk](https://github.com/chalk/chalk) - Style and color text in the terminal