Skip to content

Commit ac3a1f0

Browse files
committed
Merge pull request DefinitelyTyped#4726 from vojtechhabarta/tabtab
added definition for tabtab node module
2 parents 534bebd + 9a5e66c commit ac3a1f0

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

tabtab/tabtab-tests.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
/// <reference path="../node/node.d.ts" />
3+
/// <reference path="../tabtab/tabtab.d.ts" />
4+
5+
import tabtab = require('tabtab');
6+
import child_process = require('child_process');
7+
import string_decoder = require('string_decoder');
8+
9+
if (process.argv.slice(2)[0] === 'completion') {
10+
tabtab.complete('pkgname', function(err, data) {
11+
if (err || !data) return;
12+
if (/^--\w?/.test(data.last)) return tabtab.log(['help', 'version'], data, '--');
13+
if (/^-\w?/.test(data.last)) return tabtab.log(['n', 'o', 'd', 'e'], data, '-');
14+
tabtab.log(['list', 'of', 'commands'], data);
15+
16+
child_process.exec('rake -H', function(err, stdout, stderr) {
17+
if (err) return;
18+
var decoder = new string_decoder.StringDecoder('utf8');
19+
var parsed = tabtab.parseOut(decoder.write(stdout));
20+
if (/^--\w?/.test(data.last)) return tabtab.log(parsed.longs, data, '--');
21+
if (/^-\w?/.test(data.last)) return tabtab.log(parsed.shorts, data, '-');
22+
});
23+
24+
child_process.exec('cake', function(err, stdout, stderr) {
25+
if (err) return;
26+
var decoder = new string_decoder.StringDecoder('utf8');
27+
var tasks = tabtab.parseTasks(decoder.write(stdout), 'cake');
28+
tabtab.log(tasks, data);
29+
});
30+
});
31+
}

tabtab/tabtab.d.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Type definitions for tabtab 0.0.4
2+
// Project: https://github.com/mklabs/node-tabtab
3+
// Definitions by: Vojtěch Habarta <https://github.com/vojtechhabarta>
4+
// Definitions: https://github.com/borisyankov/DefinitelyTyped
5+
6+
declare module "tabtab" {
7+
8+
/**
9+
* Main completion method, has support for installation and actual completion.
10+
* @param name Name of the command to complete.
11+
* @param cb Get called when a tab-completion command happens.
12+
*/
13+
export function complete(name: string, cb: CallBack): void;
14+
15+
/**
16+
* Main completion method, has support for installation and actual completion.
17+
* @param name Name of the command to complete.
18+
* @param completer Name of the command to call on completion.
19+
* @param cb Get called when a tab-completion command happens.
20+
*/
21+
export function complete(name: string, completer: string, cb: CallBack): void;
22+
23+
/**
24+
* Simple helper function to know if the script is run in the context of a completion command.
25+
*/
26+
export function isComplete(): boolean;
27+
28+
/**
29+
* 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(), ...).
30+
*/
31+
export function parseOut(str: string): { shorts: string[]; longs: string[] };
32+
33+
/**
34+
* Same purpose as parseOut, but for parsing tasks from an help command (cake/rake -T, vagrant, etc.).
35+
*/
36+
export function parseTasks(str: string, prefix: string, reg?: RegExp|string): string[];
37+
38+
/**
39+
* Helper to return completion output and log to standard output.
40+
* @param values Array of values to complete against.
41+
* @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.
42+
* @param prefix A prefix to add to the completion results, useful for options to add dashes (eg. - or --).
43+
*/
44+
export function log(values: string[], data: Data, prefix?: string): void;
45+
46+
interface CallBack {
47+
(error?: Error, data?: Data, text?: string): any;
48+
}
49+
50+
/**
51+
* Holds interesting values to drive the output of the completion.
52+
*/
53+
interface Data {
54+
55+
/**
56+
* full command being completed
57+
*/
58+
line: string;
59+
60+
/**
61+
* number of words
62+
*/
63+
words: number;
64+
65+
/**
66+
* cursor position
67+
*/
68+
point: number;
69+
70+
/**
71+
* tabing in the middle of a word: foo bar baz bar foobarrrrrrr
72+
*/
73+
partial: string;
74+
75+
/**
76+
* last word of the line
77+
*/
78+
last: string;
79+
80+
/**
81+
* last partial of the line
82+
*/
83+
lastPartial: string;
84+
85+
/**
86+
* the previous word
87+
*/
88+
prev: string;
89+
}
90+
91+
}

0 commit comments

Comments
 (0)