forked from sindresorhus/terminal-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (46 loc) · 1.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
const util = require('util');
const fs = require('fs');
const chalk = require('chalk');
const Jimp = require('@sindresorhus/jimp');
const termImg = require('term-img');
const PIXEL = '\u2584';
const readFile = util.promisify(fs.readFile);
const toArray = require('stream-to-array');
const request = require('request');
async function render(buffer) {
const image = await Jimp.read(buffer);
const columns = process.stdout.columns || 80;
const rows = process.stdout.rows || 24;
if (image.bitmap.width > columns || (image.bitmap.height / 2) > rows) {
image.scaleToFit(columns, rows * 2);
}
let ret = '';
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(' ');
} else {
ret += chalk.bgRgb(r, g, b).rgb(r2, g2, b2)(PIXEL);
}
}
ret += '\n';
}
return ret;
}
exports.buffer = async buffer => {
return termImg.string(buffer, {
width: '100%',
height: '100%',
fallback: () => render(buffer)
});
};
const getImgFromUrl = async url => {
const imgBuffParts = await toArray(request.get(url));
const buffers = imgBuffParts.map(part => Buffer.isBuffer(part) ? part : Buffer.from(part));
return Buffer.concat(buffers);
};
exports.file = async filePath => exports.buffer(await readFile(filePath));
exports.url = async url => exports.buffer(await getImgFromUrl(url));