From 17a5f93e1cf1f9cdf1bee71afa41f13fa3044730 Mon Sep 17 00:00:00 2001 From: Matthew Riley MacPherson Date: Wed, 14 Oct 2015 20:16:37 +0100 Subject: [PATCH] Use terminal width for CLI wrapping I was using a larger-than-usual terminal window and noticed the output was still wrapped to 78 characters. This uses process.stdout.columns if available to set the CLI wrap value. (minus two pixels to preserve a similar padding effect as using 78 over 80). --- src/cli.js | 15 ++++++++++++++- tests/test.cli.js | 32 +++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/cli.js b/src/cli.js index 4a371c3a..47f6bfeb 100644 --- a/src/cli.js +++ b/src/cli.js @@ -4,6 +4,19 @@ import { singleLineString } from 'utils'; import { version } from 'json!../package'; +export function _terminalWidth(_process=process) { + if (_process && _process.stdout && _process.stdout.columns > 0) { + var width = _process.stdout.columns - 2; + // Terminals less than ten pixels wide seem silly. + if (width < 10) { + width = 10; + } + + return width; + } else { + return 78; + } +} export default argv .usage('Usage: ./$0 [options] addon-package \n\n' + @@ -81,4 +94,4 @@ export default argv .demand(1) .help('help') .alias('h', 'help') - .wrap(78); + .wrap(_terminalWidth()); diff --git a/tests/test.cli.js b/tests/test.cli.js index 02d36d55..ad4eca2b 100644 --- a/tests/test.cli.js +++ b/tests/test.cli.js @@ -1,4 +1,4 @@ -import { default as cli_ } from 'cli'; +import { default as cli_, _terminalWidth } from 'cli'; var cli; @@ -69,4 +69,34 @@ describe('Basic CLI tests', function() { 'Invalid values:\n Argument: output, Given: "false"')); }); + it('should use 78 as a width if process.stdout.columns is undefined', () => { + var fakeProcess = null; + assert.equal(_terminalWidth(fakeProcess), 78); + fakeProcess = {stdout: null}; + assert.equal(_terminalWidth(fakeProcess), 78); + fakeProcess = {stdout: {columns: null}}; + assert.equal(_terminalWidth(fakeProcess), 78); + }); + + it('should always use a positive terminal width', () => { + var fakeProcess = {stdout: {columns: 1}}; + assert.equal(_terminalWidth(fakeProcess), 10); + }); + + it('should not use a width under 10 columns', () => { + var fakeProcess = {stdout: {columns: 12}}; + assert.equal(_terminalWidth(fakeProcess), 10); + + fakeProcess = {stdout: {columns: 11}}; + assert.equal(_terminalWidth(fakeProcess), 10); + + fakeProcess = {stdout: {columns: 79}}; + assert.equal(_terminalWidth(fakeProcess), 77); + }); + + it('should use a terminal width of $COLUMNS - 2', () => { + var fakeProcess = {stdout: {columns: 170}}; + assert.equal(_terminalWidth(fakeProcess), 168); + }); + });