Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 3, 2021
1 parent 81679a5 commit d4b79e4
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 118 deletions.
3 changes: 0 additions & 3 deletions .github/funding.yml

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 10
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
3 changes: 1 addition & 2 deletions example-gif.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict';
const terminalImage = require('.');
import terminalImage from './index.js';

terminalImage.gifFile('fixture.gif');
3 changes: 1 addition & 2 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const terminalImage = require('.');
import terminalImage from './index.js';

(async () => {
console.log(await terminalImage.file('fixture.jpg'));
Expand Down
96 changes: 42 additions & 54 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
/// <reference types="node"/>

declare namespace terminalImage {
export type RenderFrame = {
/**
Custom handler which is run when the animation playback is stopped.
export type RenderFrame = {
/**
Custom handler which is run when the animation playback is stopped.
This can be set to perform a cleanup when playback has finished.
*/
done?: () => void;
This can be set to perform a cleanup when playback has finished.
*/
done?: () => void;

/**
Custom handler which is run for each frame of the GIF.
/**
Custom handler which is run for each frame of the GIF.
This can be set to change how each frame is shown.
This can be set to change how each frame is shown.
@param text - The frame which should be rendered.
*/
(text: string): void;
};
}
@param text - The frame which should be rendered.
*/
(text: string): void;
};

declare const terminalImage: {
/**
Expand All @@ -41,16 +37,14 @@ declare const terminalImage: {
@example
```
import terminalImage = require('terminal-image');
import got = require('got');
(async () => {
const body = await got('https://sindresorhus.com/unicorn').buffer();
console.log(await terminalImage.buffer(body));
console.log(await terminalImage.buffer(body, {width: '50%', height: '50%'}));
console.log(await terminalImage.buffer(body, {width: 50 }));
console.log(await terminalImage.buffer(body, {width: 70, height: 50, preserveAspectRatio: false}));
})();
import terminalImage from 'terminal-image';
import got from 'got';
const body = await got('https://sindresorhus.com/unicorn').buffer();
console.log(await terminalImage.buffer(body));
console.log(await terminalImage.buffer(body, {width: '50%', height: '50%'}));
console.log(await terminalImage.buffer(body, {width: 50 }));
console.log(await terminalImage.buffer(body, {width: 70, height: 50, preserveAspectRatio: false}));
```
*/
buffer: (imageBuffer: Readonly<Buffer>, options?: Readonly<{
Expand Down Expand Up @@ -79,14 +73,12 @@ declare const terminalImage: {
@example
```
const terminalImage = require('terminal-image');
(async () => {
console.log(await terminalImage.file('unicorn.jpg'));
console.log(await terminalImage.file('unicorn.jpg', {width: '50%', height: '50%'}));
console.log(await terminalImage.file('unicorn.jpg', {width: 50 }));
console.log(await terminalImage.file('unicorn.jpg', {width: 70, height: 50, preserveAspectRatio: false}));
})();
import terminalImage from 'terminal-image';
console.log(await terminalImage.file('unicorn.jpg'));
console.log(await terminalImage.file('unicorn.jpg', {width: '50%', height: '50%'}));
console.log(await terminalImage.file('unicorn.jpg', {width: 50 }));
console.log(await terminalImage.file('unicorn.jpg', {width: 70, height: 50, preserveAspectRatio: false}));
```
*/
file: (
Expand Down Expand Up @@ -121,24 +113,22 @@ declare const terminalImage: {
@example
```
import terminalImage = require('terminal-image');
import delay = require('delay');
const {promises: fs} = require('fs');
import {setTimeout} from 'node:timers/promises';
import fs from 'node:fs/promises';
import terminalImage from 'terminal-image';
(async () => {
const gifData = await fs.readFile('unicorn.gif');
const stopAnimation = terminalImage.gifBuffer(gifData);
const gifData = await fs.readFile('unicorn.gif');
const stopAnimation = terminalImage.gifBuffer(gifData);
await delay(5000);
stopAnimation();
})();
await delay(5000);
stopAnimation();
```
*/
gifBuffer: (imageBuffer: Readonly<Buffer>, options?: Readonly<{
width?: string | number;
height?: string | number;
maximumFrameRate?: number;
renderFrame?: terminalImage.RenderFrame;
renderFrame?: RenderFrame;
}>) => () => void;

/**
Expand All @@ -164,15 +154,13 @@ declare const terminalImage: {
@example
```
import terminalImage = require('terminal-image');
import delay = require('delay');
import {setTimeout} from 'node:timers/promises';
import terminalImage from 'terminal-image';
(async () => {
const stopAnimation = terminalImage.gifFile('unicorn.gif');
const stopAnimation = terminalImage.gifFile('unicorn.gif');
await delay(5000);
stopAnimation();
})();
await setTimeout(5000);
stopAnimation();
```
*/
gifFile: (
Expand All @@ -181,9 +169,9 @@ declare const terminalImage: {
width?: string | number;
height?: string | number;
maximumFrameRate?: number;
renderFrame?: terminalImage.RenderFrame;
renderFrame?: RenderFrame;
}>
) => () => void;
};

export = terminalImage;
export default terminalImage;
40 changes: 18 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
'use strict';
const {promisify} = require('util');
const fs = require('fs');
const chalk = require('chalk');
const Jimp = require('jimp');
const termImg = require('term-img');
const renderGif = require('render-gif');
const logUpdate = require('log-update');
import fs, {promises as fsPromises} from 'node:fs';
import chalk from 'chalk';
import Jimp from 'jimp';
import termImg from 'term-img';
import renderGif from 'render-gif';
import logUpdate from 'log-update';

// `log-update` adds an extra newline so the generated frames need to be 2 pixels shorter.
const ROW_OFFSET = 2;

const PIXEL = '\u2584';
const readFile = promisify(fs.readFile);

function scale(width, height, originalWidth, originalHeight) {
const originalRatio = originalWidth / originalHeight;
Expand Down Expand Up @@ -83,12 +80,7 @@ async function render(buffer, {width: inputWidth, height: inputHeight, preserveA
for (let x = 0; x < image.bitmap.width; x++) {
const {r, g, b, a} = Jimp.intToRGBA(image.getPixelColor(x, y));
const {r: r2, g: g2, b: b2} = Jimp.intToRGBA(image.getPixelColor(x, y + 1));

if (a === 0) {
result += chalk.reset(' ');
} else {
result += chalk.bgRgb(r, g, b).rgb(r2, g2, b2)(PIXEL);
}
result += a === 0 ? chalk.reset(' ') : chalk.bgRgb(r, g, b).rgb(r2, g2, b2)(PIXEL);
}

result += '\n';
Expand All @@ -97,18 +89,20 @@ async function render(buffer, {width: inputWidth, height: inputHeight, preserveA
return result;
}

exports.buffer = async (buffer, {width = '100%', height = '100%', preserveAspectRatio = true} = {}) => {
const terminalImage = {};

terminalImage.buffer = async (buffer, {width = '100%', height = '100%', preserveAspectRatio = true} = {}) => {
return termImg(buffer, {
width,
height,
fallback: () => render(buffer, {height, width, preserveAspectRatio})
});
};

exports.file = async (filePath, options = {}) =>
exports.buffer(await readFile(filePath), options);
terminalImage.file = async (filePath, options = {}) =>
terminalImage.buffer(await fsPromises.readFile(filePath), options);

exports.gifBuffer = (buffer, options = {}) => {
terminalImage.gifBuffer = (buffer, options = {}) => {
options = {
renderFrame: logUpdate,
maximumFrameRate: 30,
Expand All @@ -133,7 +127,7 @@ exports.gifBuffer = (buffer, options = {}) => {
}

const animation = renderGif(buffer, async frameData => {
options.renderFrame(await exports.buffer(Buffer.from(frameData), options));
options.renderFrame(await terminalImage.buffer(Buffer.from(frameData), options));
}, options);

return () => {
Expand All @@ -142,5 +136,7 @@ exports.gifBuffer = (buffer, options = {}) => {
};
};

exports.gifFile = (filePath, options = {}) =>
exports.gifBuffer(fs.readFileSync(filePath), options);
terminalImage.gifFile = (filePath, options = {}) =>
terminalImage.gifBuffer(fs.readFileSync(filePath), options);

export default terminalImage;
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import terminalImage = require('.');
import terminalImage from './index.js';

expectType<Promise<string>>(terminalImage.file('unicorn.jpg'));
expectType<Promise<string>>(terminalImage.buffer(Buffer.alloc(1)));
Expand Down
18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=10"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -44,16 +46,16 @@
"sequence"
],
"dependencies": {
"chalk": "^4.0.0",
"jimp": "^0.14.0",
"chalk": "^4.1.1",
"jimp": "^0.16.1",
"log-update": "^4.0.0",
"render-gif": "^2.0.4",
"term-img": "^5.0.0"
"term-img": "^6.0.0"
},
"devDependencies": {
"@types/node": "^13.13.5",
"ava": "^1.4.1",
"tsd": "^0.11.0",
"xo": "^0.30.0"
"@types/node": "^15.0.1",
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.39.1"
}
}
34 changes: 12 additions & 22 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,35 @@ $ npm install terminal-image
## Usage

```js
const terminalImage = require('terminal-image');
import terminalImage from 'terminal-image';

(async () => {
console.log(await terminalImage.file('unicorn.jpg'));
})();
console.log(await terminalImage.file('unicorn.jpg'));
```

Optionally, 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.

It is recommended to use the percentage option.

```js
const terminalImage = require('terminal-image');
import terminalImage from 'terminal-image';

(async () => {
console.log(await terminalImage.file('unicorn.jpg', {width: '50%', height: '50%'}));
})();
console.log(await terminalImage.file('unicorn.jpg', {width: '50%', height: '50%'}));
```

You can set width and/or height as columns and/or rows of the terminal window as well.

```js
const terminalImage = require('terminal-image');
import terminalImage from 'terminal-image';

(async () => {
console.log(await terminalImage.file('unicorn.jpg', {width: 50}));
})();
console.log(await terminalImage.file('unicorn.jpg', {width: 50}));
```

By 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.

```js
const terminalImage = require('terminal-image');
import terminalImage from 'terminal-image';

(async () => {
console.log(await terminalImage.file('unicorn.jpg', {width: 70, height: 50, preserveAspectRatio: false}));
})();
console.log(await terminalImage.file('unicorn.jpg', {width: 70, height: 50, preserveAspectRatio: false}));
```

## API
Expand Down Expand Up @@ -133,13 +125,11 @@ This can be set to perform a cleanup when playback has finished.
### Display a remote image

```js
const terminalImage = require('terminal-image');
const got = require('got');
import terminalImage from 'terminal-image';
import got from 'got';

(async () => {
const body = await got('https://sindresorhus.com/unicorn').buffer();
console.log(await terminalImage.buffer(body));
})();
const body = await got('https://sindresorhus.com/unicorn').buffer();
console.log(await terminalImage.buffer(body));
```

## Related
Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import fs from 'node:fs';
import delay from 'delay';
import test from 'ava';
import terminalImage from '.';
import terminalImage from './index.js';

test('.buffer()', async t => {
const result = await terminalImage.buffer(fs.readFileSync('fixture.jpg'));
Expand Down

0 comments on commit d4b79e4

Please sign in to comment.