-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·54 lines (42 loc) · 1.17 KB
/
index.js
File metadata and controls
executable file
·54 lines (42 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#! /usr/bin/env node
var
https = require('https'),
os = require('os');
function exit() {
process.stdout.write('Bye!' + os.EOL);
process.exit();
}
process.stdin.resume();
process.on('SIGINT', exit);
function explain(cmd, callback) {
https.get('https://www.mankier.com/api/v2/explain/?cols=' + process.stdout.columns + '&q=' + encodeURIComponent(cmd), function (res) {
res.on('data', function (d) {
process.stdout.write(d);
});
res.on('end', function () {
callback();
});
}).on('error', function (e) {
console.log('Got error: ' + e.message);
});
}
if (process.argv.length === 2) {
process.stdout.write('Command: ');
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (text) {
var cmd = text.trim();
if (cmd === '' || cmd === 'quit') {
exit();
} else {
explain(cmd, function () {
process.stdout.write('Command: ');
});
}
});
} else {
var myArgs = process.argv.slice(2);
explain(myArgs.join(' '), function () {
process.exit();
});
}