-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
completion - install instruction and simple line parsing/callback api
Run `<pkgname> completion` for install instruction. Run `<pkgname> completion >> datauri completion >> ~/.bashrc` (or ~/.zshrc) Simply log in the callback to show completion output.
- Loading branch information
Showing
4 changed files
with
100 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,31 @@ | ||
#!/usr/bin/env node | ||
var completion = require('../lib/completion'); | ||
|
||
console.log('Executing pkgrc bin file'); | ||
// this file is mainly just an example of usage | ||
|
||
var args = process.argv.slice(2); | ||
console.log('Args are: ', args); | ||
// pass the module name, will change to module | ||
// reference + dir walk to find a package.json | ||
completion.complete('pkgrc', function(err, o) { | ||
if(err || !o) return; | ||
|
||
// options: | ||
// - line: the full line from compgen | ||
// - partial: the partial line, part of the line where the cursor is | ||
// - words: the word counter, a number mapping the number of words from compgen | ||
// - point: cursor offset | ||
|
||
// | ||
// Setting completion output is simply a matter of writing to the console | ||
// | ||
|
||
console.log(o.line); | ||
console.log(o.partial); | ||
console.log(o.words); | ||
console.log(o.point); | ||
console.log(o.last); | ||
console.log(o.lastPartial); | ||
|
||
if(o.partial === 'hello') { | ||
console.log('World'); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
var fs = require('fs'), | ||
path = require('path'), | ||
args = process.argv.slice(2), | ||
complete = args[0] === 'completion', | ||
words = process.env.COMP_CWORD, | ||
point = process.env.COMP_POINT, | ||
line = process.env.COMP_LINE; | ||
|
||
// check bin/pkgrc for usage example. | ||
// | ||
// Run `<pkgname> completion` for install instruction. | ||
// Run `<pkgname> completion >> datauri completion >> ~/.bashrc` (or ~/.zshrc) | ||
// | ||
// Simply log in the callback to show completion output. | ||
|
||
exports.complete = function(name, cb) { | ||
|
||
cb = cb || function(){}; | ||
|
||
// if not a complete command, return here. | ||
if(!complete) return cb(); | ||
|
||
// if the COMP_* are not in the env, then dump the install script. | ||
if(!words || !point || !line) return script(name, cb); | ||
|
||
var partial = line.substr(0, point), | ||
last = line.split(' ').slice(-1).join(''), | ||
lastPartial = partial.split(' ').slice(-1).join(''); | ||
|
||
cb(null, { | ||
line: line, | ||
words: words, | ||
point: point, | ||
partial: partial, | ||
last: last, | ||
lastPartial: lastPartial | ||
}); | ||
}; | ||
|
||
// simple helper function to know if the script is run | ||
// in the context of a completion command. Also mapping the | ||
// special `<pkgname> completion` cmd. | ||
exports.isComplete = function() { | ||
return complete || (words && point && line); | ||
}; | ||
|
||
// output the completion.sh script to the console for install instructions. | ||
// This is actually a 'template' where the package name is used to setup | ||
// the completion on the right command, and properly name the bash/zsh functions. | ||
function script (name, cb) { | ||
var p = path.resolve(__dirname, 'completion/completion.sh'); | ||
|
||
fs.readFile(p, 'utf8', function (er, d) { | ||
if (er) return cb(er); | ||
d = d.replace(/\{pkgname\}/g, name); | ||
console.log(d); | ||
cb(); | ||
}); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters