Skip to content

Commit 274f9d6

Browse files
committed
Require Node.js 18
1 parent 2332986 commit 274f9d6

File tree

7 files changed

+48
-41
lines changed

7 files changed

+48
-41
lines changed

.github/workflows/main.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 16
14-
- 14
15-
- 12
13+
- 22
14+
- 20
15+
- 18
1616
steps:
17-
- uses: actions/checkout@v2
18-
- uses: actions/setup-node@v2
17+
- uses: actions/checkout@v4
18+
- uses: actions/setup-node@v4
1919
with:
2020
node-version: ${{ matrix.node-version }}
2121
- run: npm install

example.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
import terminalImage from './index.js';
22

3-
(async () => {
4-
console.log(await terminalImage.file('fixture.jpg'));
5-
})();
3+
console.log(await terminalImage.file('fixture.jpg'));

index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ declare const terminalImage: {
4747
console.log(await terminalImage.buffer(body, {width: 70, height: 50, preserveAspectRatio: false}));
4848
```
4949
*/
50-
buffer: (imageBuffer: Readonly<Buffer>, options?: Readonly<{
50+
buffer: (imageBuffer: Readonly<Uint8Array>, options?: Readonly<{
5151
width?: string | number;
5252
height?: string | number;
5353
preserveAspectRatio?: boolean;
@@ -124,7 +124,7 @@ declare const terminalImage: {
124124
stopAnimation();
125125
```
126126
*/
127-
gifBuffer: (imageBuffer: Readonly<Buffer>, options?: Readonly<{
127+
gifBuffer: (imageBuffer: Readonly<Uint8Array>, options?: Readonly<{
128128
width?: string | number;
129129
height?: string | number;
130130
maximumFrameRate?: number;

index.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import fs, {promises as fsPromises} from 'node:fs';
1+
import process from 'node:process';
2+
import fs from 'node:fs';
3+
import fsPromises from 'node:fs/promises';
24
import chalk from 'chalk';
35
import Jimp from 'jimp';
46
import termImg from 'term-img';
57
import renderGif from 'render-gif';
68
import logUpdate from 'log-update';
9+
import {imageDimensionsFromData} from 'image-dimensions';
710

811
// `log-update` adds an extra newline so the generated frames need to be 2 pixels shorter.
912
const ROW_OFFSET = 2;
@@ -68,7 +71,7 @@ function calculateWidthHeight(imageWidth, imageHeight, inputWidth, inputHeight,
6871
}
6972

7073
async function render(buffer, {width: inputWidth, height: inputHeight, preserveAspectRatio}) {
71-
const image = await Jimp.read(buffer);
74+
const image = await Jimp.read(Buffer.from(buffer));
7275
const {bitmap} = image;
7376

7477
const {width, height} = calculateWidthHeight(bitmap.width, bitmap.height, inputWidth, inputHeight, preserveAspectRatio);
@@ -91,13 +94,11 @@ async function render(buffer, {width: inputWidth, height: inputHeight, preserveA
9194

9295
const terminalImage = {};
9396

94-
terminalImage.buffer = async (buffer, {width = '100%', height = '100%', preserveAspectRatio = true} = {}) => {
95-
return termImg(buffer, {
96-
width,
97-
height,
98-
fallback: () => render(buffer, {height, width, preserveAspectRatio})
99-
});
100-
};
97+
terminalImage.buffer = async (buffer, {width = '100%', height = '100%', preserveAspectRatio = true} = {}) => termImg(buffer, {
98+
width,
99+
height,
100+
fallback: () => render(buffer, {height, width, preserveAspectRatio}),
101+
});
101102

102103
terminalImage.file = async (filePath, options = {}) =>
103104
terminalImage.buffer(await fsPromises.readFile(filePath), options);
@@ -106,19 +107,22 @@ terminalImage.gifBuffer = (buffer, options = {}) => {
106107
options = {
107108
renderFrame: logUpdate,
108109
maximumFrameRate: 30,
109-
...options
110+
...options,
110111
};
111112

112113
const finalize = () => {
113-
if (options.renderFrame.done) {
114-
options.renderFrame.done();
115-
}
114+
options.renderFrame.done?.();
116115
};
117116

117+
const dimensions = imageDimensionsFromData(buffer);
118+
if (dimensions?.width < 2 || dimensions?.width < 2) {
119+
throw new Error('The image is too small to be rendered.');
120+
}
121+
118122
const result = termImg(buffer, {
119123
width: options.width,
120124
height: options.height,
121-
fallback: () => false
125+
fallback: () => false,
122126
});
123127

124128
if (result) {
@@ -127,7 +131,7 @@ terminalImage.gifBuffer = (buffer, options = {}) => {
127131
}
128132

129133
const animation = renderGif(buffer, async frameData => {
130-
options.renderFrame(await terminalImage.buffer(Buffer.from(frameData), options));
134+
options.renderFrame(await terminalImage.buffer(frameData, options));
131135
}, options);
132136

133137
return () => {

package.json

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
"url": "https://sindresorhus.com"
1212
},
1313
"type": "module",
14-
"exports": "./index.js",
14+
"exports": {
15+
"types": "./index.d.ts",
16+
"default": "./index.js"
17+
},
18+
"sideEffects": false,
1519
"engines": {
16-
"node": ">=12"
20+
"node": ">=18"
1721
},
1822
"scripts": {
1923
"test": "xo && ava && tsd"
@@ -46,16 +50,17 @@
4650
"sequence"
4751
],
4852
"dependencies": {
49-
"chalk": "^4.1.1",
50-
"jimp": "^0.16.1",
51-
"log-update": "^4.0.0",
53+
"chalk": "^5.3.0",
54+
"image-dimensions": "^2.3.0",
55+
"jimp": "^0.22.12",
56+
"log-update": "^6.1.0",
5257
"render-gif": "^2.0.4",
53-
"term-img": "^6.0.0"
58+
"term-img": "^7.0.0"
5459
},
5560
"devDependencies": {
56-
"@types/node": "^15.0.1",
57-
"ava": "^3.15.0",
58-
"tsd": "^0.14.0",
59-
"xo": "^0.39.1"
61+
"@types/node": "^20.14.12",
62+
"ava": "^6.1.3",
63+
"tsd": "^0.31.1",
64+
"xo": "^0.59.2"
6065
}
6166
}

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ Works in any terminal that supports colors.
1010

1111
## Install
1212

13-
```
14-
$ npm install terminal-image
13+
```sh
14+
npm install terminal-image
1515
```
1616

1717
## Usage

test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ test('.file()', async t => {
1616
test('.gifBuffer()', async t => {
1717
let result = '';
1818
const stopAnimation = terminalImage.gifBuffer(fs.readFileSync('fixture.gif'), {
19-
renderFrame: text => {
19+
renderFrame(text) {
2020
result += text;
21-
}
21+
},
2222
});
2323
await delay(500);
2424
stopAnimation();
@@ -28,9 +28,9 @@ test('.gifBuffer()', async t => {
2828
test('.gifFile()', async t => {
2929
let result = '';
3030
const stopAnimation = terminalImage.gifFile('fixture.gif', {
31-
renderFrame: text => {
31+
renderFrame(text) {
3232
result += text;
33-
}
33+
},
3434
});
3535
await delay(500);
3636
stopAnimation();

0 commit comments

Comments
 (0)