diff --git a/tabtab/tabtab-tests.ts b/tabtab/tabtab-tests.ts new file mode 100644 index 0000000000..3204b7a05f --- /dev/null +++ b/tabtab/tabtab-tests.ts @@ -0,0 +1,31 @@ + +/// +/// + +import tabtab = require('tabtab'); +import child_process = require('child_process'); +import string_decoder = require('string_decoder'); + +if (process.argv.slice(2)[0] === 'completion') { + tabtab.complete('pkgname', function(err, data) { + if (err || !data) return; + if (/^--\w?/.test(data.last)) return tabtab.log(['help', 'version'], data, '--'); + if (/^-\w?/.test(data.last)) return tabtab.log(['n', 'o', 'd', 'e'], data, '-'); + tabtab.log(['list', 'of', 'commands'], data); + + child_process.exec('rake -H', function(err, stdout, stderr) { + if (err) return; + var decoder = new string_decoder.StringDecoder('utf8'); + var parsed = tabtab.parseOut(decoder.write(stdout)); + if (/^--\w?/.test(data.last)) return tabtab.log(parsed.longs, data, '--'); + if (/^-\w?/.test(data.last)) return tabtab.log(parsed.shorts, data, '-'); + }); + + child_process.exec('cake', function(err, stdout, stderr) { + if (err) return; + var decoder = new string_decoder.StringDecoder('utf8'); + var tasks = tabtab.parseTasks(decoder.write(stdout), 'cake'); + tabtab.log(tasks, data); + }); + }); +} diff --git a/tabtab/tabtab.d.ts b/tabtab/tabtab.d.ts new file mode 100644 index 0000000000..a744904acc --- /dev/null +++ b/tabtab/tabtab.d.ts @@ -0,0 +1,91 @@ +// Type definitions for tabtab 0.0.4 +// Project: https://github.com/mklabs/node-tabtab +// Definitions by: Vojtěch Habarta +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module "tabtab" { + + /** + * Main completion method, has support for installation and actual completion. + * @param name Name of the command to complete. + * @param cb Get called when a tab-completion command happens. + */ + export function complete(name: string, cb: CallBack): void; + + /** + * Main completion method, has support for installation and actual completion. + * @param name Name of the command to complete. + * @param completer Name of the command to call on completion. + * @param cb Get called when a tab-completion command happens. + */ + export function complete(name: string, completer: string, cb: CallBack): void; + + /** + * Simple helper function to know if the script is run in the context of a completion command. + */ + export function isComplete(): boolean; + + /** + * Helper to return the list of short and long options, parsed from the usual --help output of a command (cake/rake -H, vagrant, commander -h, optimist.help(), ...). + */ + export function parseOut(str: string): { shorts: string[]; longs: string[] }; + + /** + * Same purpose as parseOut, but for parsing tasks from an help command (cake/rake -T, vagrant, etc.). + */ + export function parseTasks(str: string, prefix: string, reg?: RegExp|string): string[]; + + /** + * Helper to return completion output and log to standard output. + * @param values Array of values to complete against. + * @param data The data object returned by the complete callback, used mainly to filter results accordingly upon the text that is supplied by the user. + * @param prefix A prefix to add to the completion results, useful for options to add dashes (eg. - or --). + */ + export function log(values: string[], data: Data, prefix?: string): void; + + interface CallBack { + (error?: Error, data?: Data, text?: string): any; + } + + /** + * Holds interesting values to drive the output of the completion. + */ + interface Data { + + /** + * full command being completed + */ + line: string; + + /** + * number of words + */ + words: number; + + /** + * cursor position + */ + point: number; + + /** + * tabing in the middle of a word: foo bar baz bar foobarrrrrrr + */ + partial: string; + + /** + * last word of the line + */ + last: string; + + /** + * last partial of the line + */ + lastPartial: string; + + /** + * the previous word + */ + prev: string; + } + +}