Skip to content
Permalink
Browse files

Attempt to read width from a custom environment variable

This proves to be handy for processes to share their `cli-width` with
their children process, who by default get a value of zero otherwise.

Example:

  var child_process = require('child_process');
  var cliWidth = require('cli-width');

  child_process.spawn('node', [ 'foo.js'  ], {
    env: {
      CLI_WIDTH: cliWidth()
    }
  });

Implements: #4
  • Loading branch information...
Juan Cruz Viotti
Juan Cruz Viotti committed Oct 15, 2015
1 parent 58a609f commit a67acc77dd0adfb7e15badd94717317ba215e1b0
Showing with 30 additions and 1 deletion.
  1. +3 −1 README.md
  2. +8 −0 index.js
  3. +19 −0 test/index.js
@@ -1,7 +1,7 @@
cli-width
=========

Get stdout window width, with two fallbacks, `tty` and then a default.
Get stdout window width, with three fallbacks, `tty`, a custom environment variable and then a default.

## Usage

@@ -17,6 +17,8 @@ var cliWidth = require('cli-width');
cliWidth(); // maybe 204 :)
```

You can also set the `CLI_WIDTH` environment variable.

If none of the methods are supported, the default is `0` and
can be changed via `cliWidth.defaultWidth = 200;`.

@@ -14,6 +14,14 @@ function cliWidth() {
return tty.getWindowSize()[1];
}
else {
if (process.env.CLI_WIDTH) {
var width = parseInt(process.env.CLI_WIDTH, 10);

if (!isNaN(width)) {
return width;
}
}

return exports.defaultWidth;
}
}
@@ -25,6 +25,25 @@ test('uses tty.getWindowSize', function (t) {
t.end();
});

test('uses custom env var', function (t) {
tty.getWindowSize = undefined;
process.env.CLI_WIDTH = 30;

t.equal(lib(), 30, 'equal to mocked, 30');

delete process.env.CLI_WIDTH;
t.end();
});

test('uses default if env var is not a number', function (t) {
process.env.CLI_WIDTH = 'foo';

t.equal(lib(), 0, 'default unset value, 0');

delete process.env.CLI_WIDTH;
t.end();
});

test('uses default', function (t) {
tty.getWindowSize = undefined;

0 comments on commit a67acc7

Please sign in to comment.
You can’t perform that action at this time.