├── .editorconfig ├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── example.js ├── fixture.gif ├── index.js └── package.json /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | end_of_line = lf 6 | charset = utf-8 7 | indent_size = 2 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) EGOIST <0x142857@gmail.com> (github.com/egoist) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # imgcat [![NPM version](https://img.shields.io/npm/v/imgcat.svg)](https://npmjs.com/package/imgcat) [![NPM downloads](https://img.shields.io/npm/dm/imgcat.svg)](https://npmjs.com/package/imgcat) 2 | 3 | This module is a high level wrapper of SindreSorhus's [term-img](https://github.com/sindresorhus/term-img), to display images from both URL and file path. Actually this requires iTerm 2.9+ too. 4 | 5 | ## Install 6 | 7 | ```bash 8 | $ npm install --save imgcat 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | const imgcat = require('imgcat') 15 | 16 | // print image from file 17 | imgcat('a.gif') 18 | .then(image => { 19 | console.log(image) 20 | }) 21 | .catch(e => { 22 | console.log(e.name) 23 | }) 24 | 25 | // print image from url 26 | // console.log directly 27 | imgcat('http://path/to/image', {log: true}) 28 | ``` 29 | 30 | ## API 31 | 32 | ### imgcat(input, [options, events]) 33 | 34 | #### input 35 | 36 | Type: `string` 37 | 38 | Image path or URL. 39 | 40 | #### options 41 | 42 | [term-img2](https://github.com/EGOIST-robot/term-img2) options. See https://github.com/sindresorhus/term-img#api. 43 | 44 | #### events 45 | 46 | Type: `object` 47 | 48 | ##### before 49 | 50 | Type: `function` 51 | 52 | Everything started. 53 | 54 | ##### after 55 | 56 | Type: `function` 57 | 58 | Everything done. 59 | 60 | ##### beforeDownload 61 | 62 | Type: `function` 63 | 64 | The download is started. 65 | 66 | ##### afterDownload 67 | 68 | Type: `function` 69 | 70 | The download is done. 71 | 72 | ## Related 73 | 74 | - [imgcat-cli](https://github.com/egoist/imgcat-cli) - CLI of this module. 75 | 76 | ## License 77 | 78 | MIT © [EGOIST](https://github.com/egoist) 79 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | const co = require('co') 3 | const imgcat = require('./') 4 | 5 | co(function* () { 6 | console.log('file') 7 | const tempPath = yield imgcat('fixture.gif') 8 | console.log(tempPath) 9 | console.log('url') 10 | let time = new Date().getTime() 11 | const events = { 12 | before() { 13 | console.log('before') 14 | }, 15 | beforeDownload() { 16 | console.log('Downloading...') 17 | }, 18 | afterDownload() { 19 | const dur = new Date().getTime() - time 20 | console.log(`Downloaded...${dur}ms`) 21 | }, 22 | after() { 23 | console.log('after') 24 | } 25 | } 26 | const image = yield imgcat('http://ww4.sinaimg.cn/large/a15b4afegw1enz38of1lug20dw07t1kx.gif', {}, events) 27 | console.log(image) 28 | }).catch(e => { 29 | console.log(e.stack) 30 | process.exit(1) 31 | }) 32 | -------------------------------------------------------------------------------- /fixture.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egoist/imgcat/33478bb2d003830d24893effb812de76bee5ffc2/fixture.gif -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | /* eslint no-unused-expressions: [2, { allowShortCircuit: true }] */ 3 | const path = require('path') 4 | const co = require('co') 5 | const tempFile = require('tempfile') 6 | const termImg = require('term-img2') 7 | const isUrl = require('is-url') 8 | const pget = require('pget') 9 | 10 | module.exports = co.wrap(function* (file, options, events) { 11 | const on = events || {} 12 | on.before && on.before() 13 | let tempPath 14 | let image 15 | if (isUrl(file)) { 16 | tempPath = tempFile() 17 | const dir = path.dirname(tempPath) 18 | const target = path.basename(tempPath) 19 | on.beforeDownload && on.beforeDownload() 20 | yield pget(file, {dir, target, quiet: true}) 21 | on.afterDownload && on.afterDownload() 22 | image = termImg(tempPath, options) 23 | } else { 24 | image = termImg(file, options) 25 | } 26 | on.after && on.after() 27 | return image 28 | }) 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "imgcat", 3 | "version": "2.3.0", 4 | "description": "Display image in iTerm2 version 2.9+", 5 | "license": "MIT", 6 | "repository": "egoist/imgcat", 7 | "author": { 8 | "name": "EGOIST", 9 | "email": "0x142857@gmail.com", 10 | "url": "github.com/egoist" 11 | }, 12 | "engines": { 13 | "node": ">=4" 14 | }, 15 | "scripts": { 16 | "test": "xo" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "imgcat", 23 | "image" 24 | ], 25 | "devDependencies": { 26 | "ava": "latest" 27 | }, 28 | "xo": { 29 | "semicolon": false, 30 | "space": 2 31 | }, 32 | "dependencies": { 33 | "co": "^4.6.0", 34 | "is-url": "^1.2.1", 35 | "pget": "^0.1.1", 36 | "tempfile": "^1.1.1", 37 | "term-img2": "^2.0.0" 38 | } 39 | } 40 | --------------------------------------------------------------------------------