pax_global_header00006660000000000000000000000064131470326220014512gustar00rootroot0000000000000052 comment=176376e902a7d9fde3d82c885978e6d13d6496fc cli-width-2.2.0/000077500000000000000000000000001314703262200133775ustar00rootroot00000000000000cli-width-2.2.0/.gitignore000066400000000000000000000000541314703262200153660ustar00rootroot00000000000000node_modules coverage *.log *.swp .DS_Store cli-width-2.2.0/.npmignore000066400000000000000000000000331314703262200153720ustar00rootroot00000000000000test coverage CHANGELOG.md cli-width-2.2.0/.travis.yml000066400000000000000000000002211314703262200155030ustar00rootroot00000000000000language: node_js node_js: - '0.10' - '0.11' - '0.12' - 'iojs-1' - 'iojs-2' - 'iojs-3' - '4.0' after_script: - npm run coveralls cli-width-2.2.0/CHANGELOG.md000066400000000000000000000011201314703262200152020ustar00rootroot00000000000000# Change Log All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. # [2.2.0](https://github.com/knownasilya/cli-width/compare/v2.1.1...v2.2.0) (2017-08-22) ### Features * return default if env is 0 ([1833baf](https://github.com/knownasilya/cli-width/commit/1833baf)), closes [#9](https://github.com/knownasilya/cli-width/issues/9) ## [2.1.1](https://github.com/knownasilya/cli-width/compare/v2.1.0...v2.1.1) (2017-08-22) cli-width-2.2.0/LICENSE000066400000000000000000000013671314703262200144130ustar00rootroot00000000000000Copyright (c) 2015, Ilya Radchenko Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. cli-width-2.2.0/README.md000066400000000000000000000034271314703262200146640ustar00rootroot00000000000000cli-width ========= Get stdout window width, with four fallbacks, `tty`, `output.columns`, a custom environment variable and then a default. [![npm version](https://badge.fury.io/js/cli-width.svg)](http://badge.fury.io/js/cli-width) [![Build Status](https://travis-ci.org/knownasilya/cli-width.svg)](https://travis-ci.org/knownasilya/cli-width) [![Coverage Status](https://coveralls.io/repos/knownasilya/cli-width/badge.svg?branch=master&service=github)](https://coveralls.io/github/knownasilya/cli-width?branch=master) ## Usage ``` npm install --save cli-width ``` ```js 'use strict'; var cliWidth = require('cli-width'); cliWidth(); // maybe 204 :) ``` You can also set the `CLI_WIDTH` environment variable. If none of the methods are supported, and the environment variable isn't set, the default width value is going to be `0`, that can be changed using the configurable `options`. ## API ### cliWidth([options]) `cliWidth` can be configured using an `options` parameter, the possible properties are: - **defaultWidth**\ Defines a default value to be used if none of the methods are available, defaults to `0` - **output**\ A stream to be used to read width values from, defaults to `process.stdout` - **tty**\ TTY module to try to read width from as a fallback, defaults to `require('tty')` ### Examples Defining both a default width value and a stream output to try to read from: ```js var cliWidth = require('cli-width'); var ttys = require('ttys'); cliWidth({ defaultWidth: 80, output: ttys.output }); ``` Defines a different tty module to read width from: ```js var cliWidth = require('cli-width'); var ttys = require('ttys'); cliWidth({ tty: ttys }); ``` ## Tests ```bash npm install npm test ``` Coverage can be generated with `npm run coverage`. cli-width-2.2.0/index.js000066400000000000000000000017771314703262200150600ustar00rootroot00000000000000'use strict'; exports = module.exports = cliWidth; function normalizeOpts(options) { var defaultOpts = { defaultWidth: 0, output: process.stdout, tty: require('tty') }; if (!options) { return defaultOpts; } else { Object.keys(defaultOpts).forEach(function (key) { if (!options[key]) { options[key] = defaultOpts[key]; } }); return options; } } function cliWidth(options) { var opts = normalizeOpts(options); if (opts.output.getWindowSize) { return opts.output.getWindowSize()[0] || opts.defaultWidth; } else { if (opts.tty.getWindowSize) { return opts.tty.getWindowSize()[1] || opts.defaultWidth; } else { if (opts.output.columns) { return opts.output.columns; } else { if (process.env.CLI_WIDTH) { var width = parseInt(process.env.CLI_WIDTH, 10); if (!isNaN(width) && width !== 0) { return width; } } } return opts.defaultWidth; } } }; cli-width-2.2.0/package.json000066400000000000000000000016131314703262200156660ustar00rootroot00000000000000{ "name": "cli-width", "version": "2.2.0", "description": "Get stdout window width, with two fallbacks, tty and then a default.", "main": "index.js", "scripts": { "test": "node test | tspec", "coverage": "isparta cover test/*.js | tspec", "coveralls": "npm run coverage -s && coveralls < coverage/lcov.info", "postcoveralls": "rimraf ./coverage", "release": "standard-version" }, "repository": { "type": "git", "url": "git@github.com:knownasilya/cli-width.git" }, "author": "Ilya Radchenko ", "license": "ISC", "bugs": { "url": "https://github.com/knownasilya/cli-width/issues" }, "homepage": "https://github.com/knownasilya/cli-width", "devDependencies": { "coveralls": "^2.11.4", "isparta": "^3.0.4", "rimraf": "^2.4.3", "standard-version": "^4.2.0", "tap-spec": "^4.1.0", "tape": "^3.4.0" } } cli-width-2.2.0/test/000077500000000000000000000000001314703262200143565ustar00rootroot00000000000000cli-width-2.2.0/test/index.js000066400000000000000000000055671314703262200160400ustar00rootroot00000000000000'use strict'; var tty = require('tty'); var test = require('tape'); var lib = require('../'); test('uses process.stdout.getWindowSize', function (t) { // mock stdout.getWindowSize process.stdout.getWindowSize = function () { return [100]; }; t.equal(lib(), 100, 'equal to mocked, 100'); t.end(); }); test('uses defaultWidth if process.stdout.getWindowSize reports width of 0', function (t) { process.stdout.getWindowSize = function () { return [0]; }; t.equal(lib({ defaultWidth: 10 }), 10, 'equal to mocked, 10'); t.end(); }) test('uses tty.getWindowSize', function (t) { process.stdout.getWindowSize = undefined; tty.getWindowSize = function () { return [3, 5]; }; t.equal(lib(), 5, 'equal to mocked, 5'); t.end(); }); test('uses default if tty.getWindowSize reports width of 0', function (t) { process.stdout.getWindowSize = undefined; tty.getWindowSize = function () { return [0, 0]; }; t.equal(lib({ defaultWidth: 10 }), 10, 'equal to mocked, 10'); t.end(); }) test('uses custom env var', function (t) { var oldWidth = process.stdout.columns; process.stdout.columns = undefined; tty.getWindowSize = undefined; process.env.CLI_WIDTH = 30; t.equal(lib(), 30, 'equal to mocked, 30'); delete process.env.CLI_WIDTH; process.stdout.columns = oldWidth; t.end(); }); test('uses default if env var is not a number', function (t) { var oldWidth = process.stdout.columns; process.stdout.columns = undefined; process.env.CLI_WIDTH = 'foo'; t.equal(lib(), 0, 'default unset value, 0'); delete process.env.CLI_WIDTH; process.stdout.columns = oldWidth; t.end(); }); test('uses default', function (t) { var oldWidth = process.stdout.columns; process.stdout.columns = undefined; tty.getWindowSize = undefined; t.equal(lib(), 0, 'default unset value, 0'); process.stdout.columns = oldWidth; t.end(); }); test('uses overridden default', function (t) { var oldWidth = process.stdout.columns; process.stdout.columns = undefined; t.equal(lib({ defaultWidth: 10 }), 10, 'user-set defaultWidth value, 10'); process.stdout.columns = oldWidth; t.end(); }); test('uses user-configured output stream', function (t) { var outputMock = { getWindowSize: function () { return [10]; } }; t.equal(lib({ output: outputMock }), 10, 'user-set output stream, 10'); t.end(); }); test('uses user-configured tty', function (t) { var ttyMock = { getWindowSize: function () { return [2, 5]; } }; t.equal(lib({ tty: ttyMock }), 5, 'user-set tty, 5'); t.end(); }); test('uses output.columns', function (t) { var oldWidth = process.stdout.columns; process.stdout.columns = 15; process.stdout.getWindowSize = undefined; delete process.env.CLI_WIDTH; t.equal(lib({ output: process.stdout }), 15, 'user-set output, 15'); process.stdout.columns = oldWidth; t.end(); })