[Pkg-javascript-commits] [node-cli-width] 01/04: Import Upstream version 2.1.0
Paolo Greppi
paolog-guest at moszumanska.debian.org
Thu Dec 22 23:02:44 UTC 2016
This is an automated email from the git hooks/post-receive script.
paolog-guest pushed a commit to branch master
in repository node-cli-width.
commit fd4f046c551bb2895f3ce6fe60200b9d666e8047
Author: Paolo Greppi <paolo.greppi at libpf.com>
Date: Thu Dec 22 22:28:47 2016 +0000
Import Upstream version 2.1.0
---
.gitignore | 5 +++
.npmignore | 1 +
.travis.yml | 11 +++++
LICENSE | 13 ++++++
README.md | 72 +++++++++++++++++++++++++++++++++
index.js | 49 ++++++++++++++++++++++
package.json | 29 ++++++++++++++
test/index.js | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 307 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..07f7f5e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+node_modules
+coverage
+*.log
+*.swp
+.DS_Store
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..9daeafb
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1 @@
+test
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..7a188cb
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,11 @@
+language: node_js
+node_js:
+ - '0.10'
+ - '0.11'
+ - '0.12'
+ - 'iojs-1'
+ - 'iojs-2'
+ - 'iojs-3'
+ - '4.0'
+after_script:
+ - npm run coveralls
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..173ed31
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,13 @@
+Copyright (c) 2015, Ilya Radchenko <ilya at burstcreations.com>
+
+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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..6783c3e
--- /dev/null
+++ b/README.md
@@ -0,0 +1,72 @@
+cli-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 stict';
+
+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**\<number\> Defines a default value to be used if none of the methods are available, defaults to `0`
+- **output**\<object\> A stream to be used to read width values from, defaults to `process.stdout`
+- **tty**\<object\> 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`.
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..70f63e2
--- /dev/null
+++ b/index.js
@@ -0,0 +1,49 @@
+'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)) {
+ return width;
+ }
+ }
+ }
+
+ return opts.defaultWidth;
+ }
+ }
+};
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..724db5e
--- /dev/null
+++ b/package.json
@@ -0,0 +1,29 @@
+{
+ "name": "cli-width",
+ "version": "2.1.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"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git at github.com:knownasilya/cli-width.git"
+ },
+ "author": "Ilya Radchenko <ilya at burstcreations.com>",
+ "license": "ISC",
+ "bugs": {
+ "url": "https://github.com/knownasilya/cli-width/issues"
+ },
+ "homepage": "https://github.com/knownasilya/cli-width",
+ "devDependencies": {
+ "tap-spec": "^4.1.0",
+ "tape": "^3.4.0",
+ "coveralls": "^2.11.4",
+ "isparta": "^3.0.4",
+ "rimraf": "^2.4.3"
+ }
+}
diff --git a/test/index.js b/test/index.js
new file mode 100644
index 0000000..fbbefc3
--- /dev/null
+++ b/test/index.js
@@ -0,0 +1,127 @@
+'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();
+})
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-cli-width.git
More information about the Pkg-javascript-commits
mailing list