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).
This commit is contained in:
Matthew Riley MacPherson 2015-10-14 20:16:37 +01:00
Родитель 6338ec57df
Коммит 17a5f93e1c
2 изменённых файлов: 45 добавлений и 2 удалений

Просмотреть файл

@ -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());

Просмотреть файл

@ -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);
});
});