Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6150eca
commit afa45a3
Showing
6 changed files
with
94 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/// <reference types="node"/> | ||
import {ImageOptions} from 'ansi-escapes'; | ||
|
||
declare class UnsupportedTerminalErrorClass extends Error { | ||
readonly name: 'UnsupportedTerminalError'; | ||
|
||
constructor(); | ||
} | ||
|
||
declare namespace termImg { | ||
interface Options<FallbackType = unknown> extends ImageOptions { | ||
/** | ||
Enables you to do something else when the terminal doesn't support images. | ||
@default () => throw new UnsupportedTerminalError() | ||
*/ | ||
readonly fallback?: () => FallbackType; | ||
} | ||
|
||
type UnsupportedTerminalError = UnsupportedTerminalErrorClass; | ||
} | ||
|
||
declare const termImg: { | ||
/** | ||
Log the image to the terminal directly. | ||
@param image - Filepath to an image or an image as a buffer. | ||
@example | ||
``` | ||
import termImg = require('term-img'); | ||
function fallback() { | ||
// Do something else when not supported | ||
} | ||
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 = unknown>( | ||
image: string | Buffer, | ||
options?: termImg.Options<FallbackType> | ||
): string | FallbackType; | ||
|
||
UnsupportedTerminalError: typeof UnsupportedTerminalErrorClass; | ||
}; | ||
|
||
export = termImg; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {expectType} from 'tsd'; | ||
import termImg = require('.'); | ||
import {UnsupportedTerminalError} from '.'; | ||
|
||
expectType<void>(termImg('/foo/bar.jpg')); | ||
expectType<void>(termImg(new Buffer(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(new Buffer(1))); | ||
expectType<string>(termImg.string('/foo/bar.jpg', {width: 1})); | ||
expectType<string | boolean>( | ||
termImg.string('/foo/bar.jpg', {fallback: () => false}) | ||
); | ||
|
||
const unsupportedTerminalError = new UnsupportedTerminalError(); | ||
expectType<UnsupportedTerminalError>(unsupportedTerminalError); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import test from 'ava'; | ||
import m from '.'; | ||
import termImg from '.'; | ||
|
||
test('main', t => { | ||
// TODO: Write some real tests | ||
t.is(typeof m, 'function'); | ||
t.is(typeof termImg, 'function'); | ||
}); |