Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add TypeScript definition #15

Merged
merged 1 commit into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: node_js
node_js:
- '12'
- '10'
- '8'
41 changes: 41 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/// <reference types="node"/>

declare const terminalImage: {
/**
Display images in the terminal.

@param imageBuffer - Buffer with the image.
@returns The ansi escape codes to display the image.

@example
```
import terminalImage = require('terminal-image');
import got = require('got');

(async () => {
const {body} = await got('sindresorhus.com/unicorn', {encoding: null});
console.log(await terminalImage.buffer(body));
})();
```
*/
buffer(imageBuffer: Buffer): Promise<string>;

/**
Display images in the terminal.

@param filePath - File path to the image.
@returns The ansi escape codes to display the image.

@example
```
import terminalImage = require('terminal-image');

(async () => {
console.log(await terminalImage.file('unicorn.jpg'));
})();
```
*/
file(filePath: string): Promise<string>;
}

export = terminalImage;
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ async function render(buffer) {
image.scaleToFit(columns, rows * 2);
}

let ret = '';
let result = '';
for (let y = 0; y < image.bitmap.height - 1; y += 2) {
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) {
ret += chalk.reset(' ');
result += chalk.reset(' ');
} else {
ret += chalk.bgRgb(r, g, b).rgb(r2, g2, b2)(PIXEL);
result += chalk.bgRgb(r, g, b).rgb(r2, g2, b2)(PIXEL);
}
}

ret += '\n';
result += '\n';
}

return ret;
return result;
}

exports.buffer = async buffer => {
Expand Down
5 changes: 5 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {expectType} from 'tsd';
import terminalImage = require('.');

expectType<Promise<string>>(terminalImage.file('unicorn.jpg'));
expectType<Promise<string>>(terminalImage.buffer(Buffer.alloc(1)));
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"terminal",
Expand All @@ -40,11 +41,13 @@
],
"dependencies": {
"@sindresorhus/jimp": "^0.3.0",
"chalk": "^2.4.1",
"term-img": "^3.0.0"
"chalk": "^2.4.2",
"term-img": "^4.1.0"
},
"devDependencies": {
"ava": "^0.25.0",
"xo": "^0.23.0"
"@types/node": "^11.13.8",
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}