forked from sharkdp/insect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·124 lines (104 loc) · 3.05 KB
/
index.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env node
var Insect = require('./output/Insect/index.js');
var insectEnv = Insect.initialEnvironment;
function usage() {
console.log("Usage: insect [EXPR]");
process.exit(1);
}
function runInsect(fmt, line) {
var lineTrimmed = line.trim();
if (lineTrimmed === "" || lineTrimmed[0] === "#") {
return undefined;
}
// Run insect
var res = Insect.repl(fmt)(insectEnv)(line);
// Update environment
insectEnv = res.newEnv;
return res;
}
// Handle command line arguments
if (process.argv.length >= 4) {
usage();
} else if (process.argv.length == 3) {
var arg = process.argv[2];
if (arg === "-h" || arg === "--help") {
usage();
} else {
// Execute a single command
var res = runInsect(Insect.fmtPlain, arg);
if (res.msgType === "value" || res.msgType === "info") {
console.log(res.msg);
} else if (res.msgType === "error") {
console.error(res.msg);
}
process.exit(0);
}
}
var interactive = process.stdin.isTTY;
if (interactive) {
var readline = require('historic-readline');
var xdgBasedir = require('xdg-basedir');
var path = require('path');
// Set up REPL
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
path: path.join(xdgBasedir.config, "insect-history"),
completer: function(line) {
var identifiers = Insect.identifiers(insectEnv);
var keywords =
identifiers.concat(Insect.functions(insectEnv))
.concat(Insect.supportedUnits)
.concat(Insect.commands);
var lastWord = line;
if (line.trim() !== "") {
var words = line.split(/\b/);
lastWord = words[words.length - 1];
keywords= keywords.filter(function(kw) {
return kw.indexOf(lastWord) === 0;
});
}
return [keywords, lastWord];
},
next: function(rl) {
var prompt = '\x1b[01m>>>\x1b[0m ';
// The visual length of the prompt (4) needs to be set explicitly for
// older versions of node:
rl.setPrompt(prompt, 4);
rl.prompt();
rl.on('line', function(line) {
var res = runInsect(Insect.fmtConsole, line);
if (res) {
if (res.msgType == "quit") {
process.exit(0);
} else if (res.msgType == "clear") {
process.stdout.write('\x1Bc');
} else {
console.log(res.msg + "\n");
}
}
rl.prompt();
}).on('close', function() {
process.exit(0);
});
}
});
} else {
// Read from non-interactive stream (shell pipe)
var lineReader = require("line-reader");
lineReader.eachLine(process.stdin, function(line) {
var res = runInsect(Insect.fmtPlain, line);
if (res) {
// Only output values and halt on errors. Ignore 'info' and 'value-set'
// message types.
if (res.msgType === "value") {
console.log(res.msg);
} else if (res.msgType == "error") {
console.error(res.msg);
process.exit(1);
} else if (res.msgType == "quit") {
process.exit(0);
}
}
});
}