brsh 2.2.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/bin/brsh-shell.js +8 -0
- package/dist/shell.es.js +49 -23
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +2 -2
- package/dist/shell.min.js.map +1 -1
- package/dist/terminal.es.js +49 -23
- package/dist/terminal.es.js.map +1 -1
- package/dist/terminal.min.js +2 -2
- package/dist/terminal.min.js.map +1 -1
- package/lib/ScriptCommand.js +67 -38
- package/lib/local/read.js +10 -0
- package/package.json +1 -1
package/lib/ScriptCommand.js
CHANGED
|
@@ -17,7 +17,14 @@ class ScriptCommand extends LocalCommand {
|
|
|
17
17
|
this.source = source;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
// Only capture input when the sandbox itself has a command waiting for it
|
|
21
|
+
// (e.g. an active `read`). This prevents swallowing keystrokes during
|
|
22
|
+
// normal command execution.
|
|
23
|
+
get captureInput() {
|
|
24
|
+
return !!(this.sandbox
|
|
25
|
+
&& this.sandbox.runningCommand
|
|
26
|
+
&& this.sandbox.runningCommand.captureInput);
|
|
27
|
+
}
|
|
21
28
|
|
|
22
29
|
onInput(char) {
|
|
23
30
|
if (this.sandbox) {
|
|
@@ -35,8 +42,8 @@ class ScriptCommand extends LocalCommand {
|
|
|
35
42
|
});
|
|
36
43
|
this.sandbox.context.source(this.context);
|
|
37
44
|
|
|
38
|
-
this.sandbox.on('stdOut', data => this.stdOut = data);
|
|
39
|
-
this.sandbox.on('stdErr', data => this.stdErr = data);
|
|
45
|
+
this.sandbox.on('stdOut', data => { this.stdOut = data; this.flush(); });
|
|
46
|
+
this.sandbox.on('stdErr', data => { this.stdErr = data; this.flush(); });
|
|
40
47
|
|
|
41
48
|
this._scriptFunctions = {};
|
|
42
49
|
|
|
@@ -110,50 +117,51 @@ class ScriptCommand extends LocalCommand {
|
|
|
110
117
|
}
|
|
111
118
|
|
|
112
119
|
_parseIf(lines, start) {
|
|
113
|
-
|
|
114
|
-
.replace(/^if\s+/, '')
|
|
120
|
+
const parseCondition = str => str
|
|
121
|
+
.replace(/^(?:if|elif)\s+/, '')
|
|
115
122
|
.replace(/\s*;\s*then\s*$/, '')
|
|
116
123
|
.replace(/\s+then\s*$/, '')
|
|
117
124
|
.trim();
|
|
118
125
|
|
|
126
|
+
const clauses = [{ condition: parseCondition(lines[start].trim()), bodyLines: [] }];
|
|
127
|
+
const elseLines = [];
|
|
128
|
+
let currentClause = clauses[0];
|
|
129
|
+
let inElse = false;
|
|
130
|
+
let depth = 1;
|
|
131
|
+
|
|
119
132
|
let i = start + 1;
|
|
120
133
|
if (i < lines.length && lines[i].trim() === 'then') i++;
|
|
121
134
|
|
|
122
|
-
const thenLines = [], elseLines = [];
|
|
123
|
-
let inElse = false, inElif = false;
|
|
124
|
-
let depth = 1;
|
|
125
|
-
|
|
126
135
|
while (i < lines.length) {
|
|
127
136
|
const line = lines[i].trim();
|
|
128
137
|
|
|
129
|
-
if (line.startsWith('if ')) depth++;
|
|
138
|
+
if (line.startsWith('if ') || line === 'if') depth++;
|
|
130
139
|
|
|
131
140
|
if (line === 'fi') {
|
|
132
141
|
depth--;
|
|
133
|
-
if (depth === 0) {
|
|
134
|
-
if (inElif) elseLines.push('fi');
|
|
135
|
-
i++; break;
|
|
136
|
-
}
|
|
142
|
+
if (depth === 0) { i++; break; }
|
|
137
143
|
}
|
|
138
144
|
|
|
139
|
-
if (depth === 1 &&
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
145
|
+
if (depth === 1 && line.startsWith('elif ')) {
|
|
146
|
+
const newClause = { condition: parseCondition(line), bodyLines: [] };
|
|
147
|
+
clauses.push(newClause);
|
|
148
|
+
currentClause = newClause;
|
|
149
|
+
i++;
|
|
150
|
+
if (i < lines.length && lines[i].trim() === 'then') i++;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (depth === 1 && line === 'else') {
|
|
155
|
+
inElse = true;
|
|
148
156
|
i++; continue;
|
|
149
157
|
}
|
|
150
158
|
|
|
151
159
|
if (inElse) elseLines.push(lines[i]);
|
|
152
|
-
else
|
|
160
|
+
else currentClause.bodyLines.push(lines[i]);
|
|
153
161
|
i++;
|
|
154
162
|
}
|
|
155
163
|
|
|
156
|
-
return { node: { type: 'if',
|
|
164
|
+
return { node: { type: 'if', clauses, elseLines }, consumed: i - start };
|
|
157
165
|
}
|
|
158
166
|
|
|
159
167
|
_parseWhile(lines, start) {
|
|
@@ -226,13 +234,26 @@ class ScriptCommand extends LocalCommand {
|
|
|
226
234
|
const FuncCmd = class extends LocalCmdBase {
|
|
227
235
|
static get name() { return funcName; }
|
|
228
236
|
run() {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
237
|
+
// Set positional parameters ($1, $2, ...) from call arguments
|
|
238
|
+
const saved = this.arguments.map((arg, i) => ({
|
|
239
|
+
key: String(i + 1),
|
|
240
|
+
prev: self.sandbox.context.getVar(String(i + 1))
|
|
241
|
+
}));
|
|
242
|
+
this.arguments.forEach((arg, i) =>
|
|
243
|
+
self.sandbox.context.setVar(String(i + 1), arg));
|
|
244
|
+
const restore = () => saved.forEach(({ key, prev }) =>
|
|
245
|
+
prev ? self.sandbox.context.setVar(key, prev)
|
|
246
|
+
: self.sandbox.context.unsetVar(key));
|
|
247
|
+
return self._executeFunctionBody(funcBody)
|
|
248
|
+
.then(() => { restore(); })
|
|
249
|
+
.catch(e => {
|
|
250
|
+
restore();
|
|
251
|
+
if (e && e.__type === 'return') {
|
|
252
|
+
this.exitCode = e.code;
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
return Promise.reject(e);
|
|
256
|
+
});
|
|
236
257
|
}
|
|
237
258
|
};
|
|
238
259
|
this.sandbox.context.setCommand(FuncCmd);
|
|
@@ -255,13 +276,21 @@ class ScriptCommand extends LocalCommand {
|
|
|
255
276
|
}
|
|
256
277
|
|
|
257
278
|
_executeIf(node) {
|
|
258
|
-
|
|
259
|
-
if (
|
|
260
|
-
return
|
|
261
|
-
|
|
262
|
-
|
|
279
|
+
const tryClause = (i) => {
|
|
280
|
+
if (i >= node.clauses.length) {
|
|
281
|
+
return node.elseLines.length > 0
|
|
282
|
+
? this._executeNodes(this._parseLines(node.elseLines))
|
|
283
|
+
: Promise.resolve();
|
|
263
284
|
}
|
|
264
|
-
|
|
285
|
+
const clause = node.clauses[i];
|
|
286
|
+
return this.sandbox.onCommand(clause.condition, false).then(() => {
|
|
287
|
+
if (this.sandbox.lastCode === 0) {
|
|
288
|
+
return this._executeNodes(this._parseLines(clause.bodyLines));
|
|
289
|
+
}
|
|
290
|
+
return tryClause(i + 1);
|
|
291
|
+
});
|
|
292
|
+
};
|
|
293
|
+
return tryClause(0);
|
|
265
294
|
}
|
|
266
295
|
|
|
267
296
|
_executeWhile(node) {
|
package/lib/local/read.js
CHANGED
|
@@ -58,6 +58,16 @@ class Read extends LocalCommand {
|
|
|
58
58
|
this.inputBuffer += key;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
if (key === 'Enter') {
|
|
62
|
+
if (this.var) {
|
|
63
|
+
this.context.setVar(this.var, this.inputBuffer || '');
|
|
64
|
+
this.completionCallback();
|
|
65
|
+
} else {
|
|
66
|
+
this.completionCallback(this.inputBuffer || '');
|
|
67
|
+
}
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
61
71
|
if(this.inputBuffer.length === this.nchars) {
|
|
62
72
|
if(this.var) {
|
|
63
73
|
this.context.setVar(this.var, this.inputBuffer)
|