forked from sindresorhus/terminal-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
147 lines (115 loc) · 4.25 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import process from 'node:process';
import fs from 'node:fs';
import fsPromises from 'node:fs/promises';
import chalk from 'chalk';
import Jimp from 'jimp';
import termImg from 'term-img';
import logUpdate from 'log-update';
import {imageDimensionsFromData} from 'image-dimensions';
// `log-update` adds an extra newline so the generated frames need to be 2 pixels shorter.
const ROW_OFFSET = 2;
const PIXEL = '\u2584';
function scale(width, height, originalWidth, originalHeight) {
const originalRatio = originalWidth / originalHeight;
const factor = (width / height > originalRatio ? height / originalHeight : width / originalWidth);
width = factor * originalWidth;
height = factor * originalHeight;
return {width, height};
}
function checkAndGetDimensionValue(value, percentageBase) {
if (typeof value === 'string' && value.endsWith('%')) {
const percentageValue = Number.parseFloat(value);
if (!Number.isNaN(percentageValue) && percentageValue > 0 && percentageValue <= 100) {
return Math.floor(percentageValue / 100 * percentageBase);
}
}
if (typeof value === 'number') {
return value;
}
throw new Error(`${value} is not a valid dimension value`);
}
function calculateWidthHeight(imageWidth, imageHeight, inputWidth, inputHeight, preserveAspectRatio) {
const terminalColumns = process.stdout.columns || 80;
const terminalRows = process.stdout.rows - ROW_OFFSET || 24;
let width;
let height;
if (inputHeight && inputWidth) {
width = checkAndGetDimensionValue(inputWidth, terminalColumns);
height = checkAndGetDimensionValue(inputHeight, terminalRows) * 2;
if (preserveAspectRatio) {
({width, height} = scale(width, height, imageWidth, imageHeight));
}
} else if (inputWidth) {
width = checkAndGetDimensionValue(inputWidth, terminalColumns);
height = imageHeight * width / imageWidth;
} else if (inputHeight) {
height = checkAndGetDimensionValue(inputHeight, terminalRows) * 2;
width = imageWidth * height / imageHeight;
} else {
({width, height} = scale(terminalColumns, terminalRows * 2, imageWidth, imageHeight));
}
if (width > terminalColumns) {
({width, height} = scale(terminalColumns, terminalRows * 2, width, height));
}
width = Math.round(width);
height = Math.round(height);
return {width, height};
}
async function render(buffer, {width: inputWidth, height: inputHeight, preserveAspectRatio}) {
const image = await Jimp.read(Buffer.from(buffer));
const {bitmap} = image;
const {width, height} = calculateWidthHeight(bitmap.width, bitmap.height, inputWidth, inputHeight, preserveAspectRatio);
image.resize(width, height);
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));
result += a === 0 ? chalk.reset(' ') : chalk.bgRgb(r, g, b).rgb(r2, g2, b2)(PIXEL);
}
result += '\n';
}
return result;
}
const terminalImage = {};
terminalImage.buffer = async (buffer, {width = '100%', height = '100%', preserveAspectRatio = true} = {}) => termImg(buffer, {
width,
height,
fallback: () => render(buffer, {height, width, preserveAspectRatio}),
});
terminalImage.file = async (filePath, options = {}) =>
terminalImage.buffer(await fsPromises.readFile(filePath), options);
terminalImage.gifBuffer = async (buffer, options = {}) => {
options = {
renderFrame: logUpdate,
maximumFrameRate: 30,
...options,
};
const finalize = () => {
options.renderFrame.done?.();
};
const dimensions = imageDimensionsFromData(buffer);
if (dimensions?.width < 2 || dimensions?.width < 2) {
throw new Error('The image is too small to be rendered.');
}
const result = termImg(buffer, {
width: options.width,
height: options.height,
fallback: () => false,
});
if (result) {
options.renderFrame(result);
return finalize;
}
const renderGif = await import('render-gif');
const animation = renderGif(buffer, async frameData => {
options.renderFrame(await terminalImage.buffer(frameData, options));
}, options);
return () => {
animation.isPlaying = false;
finalize();
};
};
terminalImage.gifFile = (filePath, options = {}) =>
terminalImage.gifBuffer(fs.readFileSync(filePath), options);
export default terminalImage;