Skip to content

Commit

Permalink
Use termImg.string as the default function (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richienb authored May 21, 2020
1 parent 30ac208 commit 08f8cdb
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 55 deletions.
6 changes: 3 additions & 3 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const termImg = require('.');

termImg('fixture.jpg', {
console.log(termImg('fixture.jpg', {
width: 50,
fallback: () => console.log('Not supported here, sorry...')
});
fallback: () => 'Not supported here, sorry...'
}));
11 changes: 2 additions & 9 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ declare namespace termImg {

declare const termImg: {
/**
Log the image to the terminal directly.
Get the image as a `string` that you can log manually.
@param image - Filepath to an image or an image as a buffer.
Expand All @@ -37,14 +37,7 @@ declare const termImg: {
termImg('unicorn.jpg', {fallback});
```
*/
(image: string | Buffer, options?: termImg.Options): void;

/**
Get the image as a `string` that you can log manually.
@param image - Filepath to an image or an image as a buffer.
*/
string<FallbackType>(
<FallbackType>(
image: string | Buffer,
options?: termImg.Options<FallbackType>
): string | FallbackType;
Expand Down
29 changes: 3 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,28 @@ function unsupported() {
throw new UnsupportedTerminalError();
}

function main(image, options = {}) {
module.exports = (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;
return fallback();
}

const version = iterm2Version();

if (Number(version[0]) < 3) {
return fallback;
return fallback();
}

if (typeof image === 'string') {
image = fs.readFileSync(image);
}

return ansiEscapes.image(image, options);
}

const termImg = (image, options) => {
const ret = main(image, options);

if (typeof ret === 'function') {
ret();
return;
}

console.log(ret);
};

module.exports = termImg;

module.exports.string = (image, options) => {
const ret = main(image, options);

if (typeof ret === 'function') {
return ret();
}

return ret;
};

module.exports.UnsupportedTerminalError = UnsupportedTerminalError;
13 changes: 4 additions & 9 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ import {expectType} from 'tsd';
import termImg = require('.');
import {UnsupportedTerminalError} from '.';

expectType<void>(termImg('/foo/bar.jpg'));
expectType<void>(termImg(Buffer.from(1)));
expectType<void>(termImg('/foo/bar.jpg', {width: 1}));
expectType<void>(termImg('/foo/bar.jpg', {fallback: () => false}));

expectType<string>(termImg.string('/foo/bar.jpg'));
expectType<string>(termImg.string(Buffer.from(1)));
expectType<string>(termImg.string('/foo/bar.jpg', {width: 1}));
expectType<string>(termImg('/foo/bar.jpg'));
expectType<string>(termImg(Buffer.alloc(1)));
expectType<string>(termImg('/foo/bar.jpg', {width: 1}));
expectType<string | boolean>(
termImg.string('/foo/bar.jpg', {fallback: () => false})
termImg('/foo/bar.jpg', {fallback: () => false})
);

const unsupportedTerminalError = new UnsupportedTerminalError();
Expand Down
10 changes: 4 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

> Display images in iTerm
You probably want the higher-level [`terminal-image`](https://github.com/sindresorhus/terminal-image) package for displaying your images.

![](screenshot.jpg)

Even [animated gifs](https://github.com/vdemedes/gifi)!
Expand All @@ -22,21 +24,17 @@ $ npm install term-img
const termImg = require('term-img');

function fallback() {
// Do something else when not supported
// Return something else when not supported
}

termImg('unicorn.jpg', {fallback});
console.log(termImg('unicorn.jpg', {fallback}));
```


## API

### termImg(image, [options])

Log the image to the terminal directly.

### termImg.string(image, [options])

Get the image as a `string` that you can log manually.

#### image
Expand Down
5 changes: 3 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import test from 'ava';
import termImg from '.';

test('main', t => {
// TODO: Write some real tests
t.is(typeof termImg, 'function');
process.env.TERM_PROGRAM = 'iTerm.app';
process.env.TERM_PROGRAM_VERSION = '3.3.7';
t.snapshot(termImg('fixture.jpg'));
});
11 changes: 11 additions & 0 deletions test.js.md

Large diffs are not rendered by default.

Binary file added test.js.snap
Binary file not shown.

0 comments on commit 08f8cdb

Please sign in to comment.