Skip to content

Commit 67476b3

Browse files
committedFeb 10, 2015
refactor two-column table layout so that we can use it for examples and usage
1 parent b109ffc commit 67476b3

File tree

3 files changed

+69
-50
lines changed

3 files changed

+69
-50
lines changed
 

‎index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function Argv (processArgs, cwd) {
7575

7676
var examples = [];
7777
self.example = function (cmd, description) {
78-
examples.push([cmd, description]);
78+
examples.push([cmd, description || '']);
7979
return self;
8080
};
8181
self.getExamples = function() {

‎lib/usage.js

+67-48
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,16 @@ function Usage (yargs) {
8686
example[0] = example[0].replace(/\$0/g, yargs.$0);
8787
});
8888

89-
var commandlen = longest(examples.map(function (a) {
90-
return a[0];
91-
}));
92-
93-
var exampleLines = examples.map(function(example) {
94-
var command = example[0];
95-
var description = example[1];
96-
command += Array(commandlen + 5 - command.length).join(' ');
97-
return ' ' + command + description;
89+
var examplesTable = {};
90+
examples.forEach(function(example) {
91+
examplesTable[example[0]] = {
92+
desc: example[1],
93+
extra: ''
94+
};
9895
});
9996

100-
exampleLines.push('');
101-
help = exampleLines.concat(help);
97+
help = formatTable(examplesTable, 5).concat(help);
98+
help.push('');
10299
help.unshift('Examples:');
103100
}
104101

@@ -114,6 +111,7 @@ function Usage (yargs) {
114111
return -1 == (options.alias[alias] || []).indexOf(key);
115112
});
116113
});
114+
117115
var switches = keys.reduce(function (acc, key) {
118116
acc[key] = [ key ].concat(options.alias[key] || [])
119117
.map(function (sw) {
@@ -124,44 +122,17 @@ function Usage (yargs) {
124122
return acc;
125123
}, {});
126124

127-
var switchlen = longest(Object.keys(switches).map(function (s) {
128-
return switches[s] || '';
129-
}));
130-
131-
var desclen = longest(Object.keys(descriptions).map(function (d) {
132-
return descriptions[d] || '';
133-
}));
134-
125+
var switchTable = {};
135126
keys.forEach(function (key) {
136127
var kswitch = switches[key];
137128
var desc = descriptions[key] || '';
138-
139-
if (wrap) {
140-
desc = wordwrap(switchlen + 4, wrap)(desc)
141-
.slice(switchlen + 4)
142-
;
143-
}
144-
145-
var spadding = new Array(
146-
Math.max(switchlen - kswitch.length + 3, 0)
147-
).join(' ');
148-
149-
var dpadding = new Array(
150-
Math.max(desclen - desc.length + 1, 0)
151-
).join(' ');
152-
153129
var type = null;
154130

155131
if (options.boolean[key]) type = '[boolean]';
156132
if (options.count[key]) type = '[count]';
157133
if (options.string[key]) type = '[string]';
158134
if (options.normalize[key]) type = '[string]';
159135

160-
if (!wrap && dpadding.length > 0) {
161-
desc += dpadding;
162-
}
163-
164-
var prelude = ' ' + kswitch + spadding;
165136
var extra = [
166137
type,
167138
demanded[key]
@@ -174,6 +145,59 @@ function Usage (yargs) {
174145
: null
175146
].filter(Boolean).join(' ');
176147

148+
switchTable[kswitch] = {
149+
desc: desc,
150+
extra: extra
151+
};
152+
});
153+
help.push.apply(help, formatTable(switchTable, 3));
154+
155+
if (keys.length) help.push('');
156+
return help.join('\n');
157+
};
158+
159+
self.showHelp = function (level) {
160+
level = level || 'error';
161+
console[level](self.help());
162+
}
163+
164+
// word-wrapped two-column layout used by
165+
// examples and options.
166+
function formatTable (table, padding) {
167+
var output = [];
168+
169+
// determine lengths of left column, and
170+
// description column.
171+
var llen = longest(Object.keys(table));
172+
173+
var desclen = longest(Object.keys(table).map(function (k) {
174+
return table[k].desc;
175+
}));
176+
177+
Object.keys(table).forEach(function(left) {
178+
var desc = table[left].desc,
179+
extra = table[left].extra;
180+
181+
if (wrap) {
182+
desc = wordwrap(llen + padding + 1, wrap)(desc)
183+
.slice(llen + padding + 1)
184+
;
185+
}
186+
187+
var lpadding = new Array(
188+
Math.max(llen - left.length + padding, 0)
189+
).join(' ');
190+
191+
var dpadding = new Array(
192+
Math.max(desclen - desc.length + 1, 0)
193+
).join(' ');
194+
195+
if (!wrap && dpadding.length > 0) {
196+
desc += dpadding;
197+
}
198+
199+
var prelude = ' ' + left + lpadding;
200+
177201
var body = [ desc, extra ].filter(Boolean).join(' ');
178202

179203
if (wrap) {
@@ -182,7 +206,7 @@ function Usage (yargs) {
182206
+ (dlines.length === 1 ? prelude.length : 0)
183207

184208
if (extra.length > wrap) {
185-
body = desc + '\n' + wordwrap(switchlen + 4, wrap)(extra)
209+
body = desc + '\n' + wordwrap(llen + 4, wrap)(extra)
186210
} else {
187211
body = desc + (dlen + extra.length > wrap - 2
188212
? '\n'
@@ -194,18 +218,13 @@ function Usage (yargs) {
194218
}
195219
}
196220

197-
help.push(prelude + body);
221+
output.push(prelude + body);
198222
});
199223

200-
if (keys.length) help.push('');
201-
return help.join('\n');
202-
};
203-
204-
self.showHelp = function (level) {
205-
level = level || 'error';
206-
console[level](self.help());
224+
return output;
207225
}
208226

227+
// find longest string in array of strings.
209228
function longest (xs) {
210229
return Math.max.apply(
211230
null,

‎test/usage.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ describe('usage tests', function () {
551551
r.should.have.property('exit').and.be.ok;
552552
r.errors.join('\n').split(/\n+/).should.deep.equal([
553553
'Examples:',
554-
' ./usage something description',
554+
' ./usage something description ',
555555
' ./usage something else other description',
556556
'Options:',
557557
' -y [required]',

0 commit comments

Comments
 (0)
Please sign in to comment.