Using the Array spaces trick to generate padding and indentation.

This commit is contained in:
Jeremy Ashkenas 2010-09-21 23:17:43 -04:00
parent c0a89a7988
commit 25c8b4b34f
6 changed files with 8 additions and 20 deletions

View File

@ -57,19 +57,13 @@
});
};
printTasks = function() {
var _ref, _result, desc, i, name, spaces, task;
var _ref, desc, name, spaces, task;
puts('');
_ref = tasks;
for (name in _ref) {
task = _ref[name];
spaces = 20 - name.length;
spaces = spaces > 0 ? (function() {
_result = [];
for (i = 0; (0 <= spaces ? i <= spaces : i >= spaces); (0 <= spaces ? i += 1 : i -= 1)) {
_result.push(' ');
}
return _result;
})().join('') : '';
spaces = spaces > 0 ? Array(spaces + 1).join(' ') : '';
desc = task.description ? ("# " + (task.description)) : '';
puts("cake " + (name) + (spaces) + " " + (desc));
}

View File

@ -165,7 +165,7 @@
if (match[2]) {
this.token('HERECOMMENT', this.sanitizeHeredoc(match[2], {
herecomment: true,
indent: new Array(this.indent + 1).join(' ')
indent: Array(this.indent + 1).join(' ')
}));
this.token('TERMINATOR', '\n');
}

View File

@ -38,7 +38,7 @@
return options;
};
OptionParser.prototype.help = function() {
var _i, _len, _ref, _result, i, letPart, lines, rule, spaces;
var _i, _len, _ref, letPart, lines, rule, spaces;
lines = ['Available options:'];
if (this.banner) {
lines.unshift("" + (this.banner) + "\n");
@ -47,13 +47,7 @@
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
rule = _ref[_i];
spaces = 15 - rule.longFlag.length;
spaces = spaces > 0 ? (function() {
_result = [];
for (i = 0; (0 <= spaces ? i <= spaces : i >= spaces); (0 <= spaces ? i += 1 : i -= 1)) {
_result.push(' ');
}
return _result;
})().join('') : '';
spaces = spaces > 0 ? Array(spaces + 1).join(' ') : '';
letPart = rule.shortFlag ? rule.shortFlag + ', ' : ' ';
lines.push(' ' + letPart + rule.longFlag + spaces + rule.description);
}

View File

@ -58,7 +58,7 @@ printTasks = ->
puts ''
for all name, task of tasks
spaces = 20 - name.length
spaces = if spaces > 0 then (' ' for i in [0..spaces]).join('') else ''
spaces = if spaces > 0 then Array(spaces + 1).join(' ') else ''
desc = if task.description then "# #{task.description}" else ''
puts "cake #{name}#{spaces} #{desc}"
puts oparse.help() if switches.length

View File

@ -140,7 +140,7 @@ exports.Lexer = class Lexer
@i += match[1].length
if match[2]
@token 'HERECOMMENT', @sanitizeHeredoc match[2],
herecomment: true, indent: new Array(@indent + 1).join(' ')
herecomment: true, indent: Array(@indent + 1).join(' ')
@token 'TERMINATOR', '\n'
true

View File

@ -47,7 +47,7 @@ exports.OptionParser = class OptionParser
lines.unshift "#{@banner}\n" if @banner
for rule in @rules
spaces = 15 - rule.longFlag.length
spaces = if spaces > 0 then (' ' for i in [0..spaces]).join('') else ''
spaces = if spaces > 0 then Array(spaces + 1).join(' ') else ''
letPart = if rule.shortFlag then rule.shortFlag + ', ' else ' '
lines.push ' ' + letPart + rule.longFlag + spaces + rule.description
"\n#{ lines.join('\n') }\n"