-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
39 lines (30 loc) · 881 Bytes
/
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
import process from 'node:process';
import fs from 'node:fs';
import iterm2Version from 'iterm2-version';
import ansiEscapes from 'ansi-escapes';
export class UnsupportedTerminalError extends Error {
constructor() {
super('iTerm >=3 required');
this.name = 'UnsupportedTerminalError';
}
}
function unsupported() {
throw new UnsupportedTerminalError();
}
export default function terminalImage(image, options = {}) {
const fallback = typeof options.fallback === 'function' ? options.fallback : unsupported;
if (!(image && image.length > 0)) {
throw new TypeError('Image required');
}
if (process.env.TERM_PROGRAM !== 'iTerm.app') {
return fallback();
}
const version = iterm2Version();
if (Number(version[0]) < 3) {
return fallback();
}
if (typeof image === 'string') {
image = fs.readFileSync(image);
}
return ansiEscapes.image(image, options);
}