1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

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

View file

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

View file

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

View file

@ -58,7 +58,7 @@ printTasks = ->
puts '' puts ''
for all name, task of tasks for all name, task of tasks
spaces = 20 - name.length 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 '' desc = if task.description then "# #{task.description}" else ''
puts "cake #{name}#{spaces} #{desc}" puts "cake #{name}#{spaces} #{desc}"
puts oparse.help() if switches.length puts oparse.help() if switches.length

View file

@ -140,7 +140,7 @@ exports.Lexer = class Lexer
@i += match[1].length @i += match[1].length
if match[2] if match[2]
@token 'HERECOMMENT', @sanitizeHeredoc 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' @token 'TERMINATOR', '\n'
true true

View file

@ -47,7 +47,7 @@ exports.OptionParser = class OptionParser
lines.unshift "#{@banner}\n" if @banner lines.unshift "#{@banner}\n" if @banner
for rule in @rules for rule in @rules
spaces = 15 - rule.longFlag.length 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 ' ' letPart = if rule.shortFlag then rule.shortFlag + ', ' else ' '
lines.push ' ' + letPart + rule.longFlag + spaces + rule.description lines.push ' ' + letPart + rule.longFlag + spaces + rule.description
"\n#{ lines.join('\n') }\n" "\n#{ lines.join('\n') }\n"