Skip to content

Commit

Permalink
add play-complete script, completion from play help output
Browse files Browse the repository at this point in the history
  • Loading branch information
mklabs committed Oct 23, 2011
1 parent 5ea2d4c commit f8347bb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
31 changes: 31 additions & 0 deletions bin/play-complete
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env node
var completion = require('../lib/completion'),
exec = require('child_process').exec,
path = require('path'),
completer = path.basename(__filename);

var parseTasks = completion.parseTasks,
parseOut = completion.parseOut,
log = completion.log;

completion.complete('play', completer, function(err, o) {
if(err || !o) return;

exec('play help', function(err, stdout, stderr) {
if(err) return;
stdout = stdout.split('Core commands')[1].split(/^~$/m)[0];
var parsed = parseTasks(stdout, '~', /~\s[\w-:]+\s/gm);
log(parsed, o);
});
});

if(completion.isComplete()) return;

console.log([
'play-complete called outside of a completion context.',
'» You may want to setup the completion in your ~/.bashrc or ~/.zshrc file, if not already.',
'',
' play-complete completion >> ~/.bashrc',
'Or',
' play-complete completion >> ~/.zshrc'
].join('\n'));
2 changes: 1 addition & 1 deletion bin/rake-complete
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ completion.complete('rake', completer, function(err, o) {
if(completion.isComplete()) return;

console.log([
'vagrant-complete called outside of a completion context.',
'rake-complete called outside of a completion context.',
'» You may want to setup the completion in your ~/.bashrc or ~/.zshrc file, if not already.',
'',
' rake-complete completion >> ~/.bashrc',
Expand Down
6 changes: 4 additions & 2 deletions lib/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var fs = require('fs'),
path = require('path'),
args = process.argv.slice(2),
completeCmd = args[0] === 'completion',
installCmd = completeCmd && args[1] === 'install',
uninstallCmd = completeCmd && args[1] === 'uninstall',
words = process.env.COMP_CWORD,
point = process.env.COMP_POINT,
line = process.env.COMP_LINE;
Expand Down Expand Up @@ -63,8 +65,8 @@ exports.parseOut = function parseOut(str) {
};

// specific to cake case
exports.parseTasks = function(str, prefix) {
var tasks = str.match(new RegExp(prefix + '\\s[^#]+', 'gm')) || [];
exports.parseTasks = function(str, prefix, reg) {
var tasks = str.match(reg || new RegExp('^' + prefix + '\\s[^#]+', 'gm')) || [];
return tasks.map(trim).map(function(s) {
return s.replace(prefix + ' ', '');
});
Expand Down

0 comments on commit f8347bb

Please sign in to comment.