brsh 3.10.5 → 3.10.6
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/dist/shell.es.js +28 -10
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +2 -1
- package/dist/shell.min.js.map +1 -1
- package/lib/local/read.js +63 -16
- package/package.json +1 -1
- package/test/local-commands.test.js +86 -0
package/lib/local/read.js
CHANGED
|
@@ -29,7 +29,8 @@ class Read extends LocalCommand {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
this.nchars = options.nchars;
|
|
32
|
-
this.
|
|
32
|
+
this.names = options.name || [];
|
|
33
|
+
this.raw = options.raw;
|
|
33
34
|
|
|
34
35
|
return new Promise((resolve) => {
|
|
35
36
|
const tid = setTimeout(() => {
|
|
@@ -54,35 +55,75 @@ class Read extends LocalCommand {
|
|
|
54
55
|
this.inputBuffer = ""
|
|
55
56
|
}
|
|
56
57
|
|
|
57
|
-
|
|
58
|
-
|
|
58
|
+
//without -r a backslash escapes the next char, and backslash-newline continues the line
|
|
59
|
+
if(!this.raw && !this.escaped && key === '\\') {
|
|
60
|
+
this.escaped = true;
|
|
61
|
+
return;
|
|
59
62
|
}
|
|
60
63
|
|
|
61
|
-
if
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
} else {
|
|
66
|
-
this.completionCallback(this.inputBuffer || '');
|
|
64
|
+
if(this.escaped) {
|
|
65
|
+
delete this.escaped;
|
|
66
|
+
if(key === 'Enter') {
|
|
67
|
+
return;
|
|
67
68
|
}
|
|
68
|
-
return;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
if(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
this.completionCallback();
|
|
71
|
+
if (key.length === 1) {
|
|
72
|
+
this.inputBuffer += key;
|
|
73
|
+
if(this.inputBuffer.length !== this.nchars) {
|
|
75
74
|
return;
|
|
76
75
|
}
|
|
77
|
-
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if(key === 'Enter' || this.inputBuffer.length === this.nchars) {
|
|
79
|
+
this._finish();
|
|
78
80
|
}
|
|
79
81
|
}
|
|
80
82
|
|
|
83
|
+
_finish() {
|
|
84
|
+
if(this.names.length < 1) {
|
|
85
|
+
return this.completionCallback(this.inputBuffer);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
//getVar returns '' for unset, but an empty IFS is meaningful
|
|
89
|
+
const ifs = this.context.vars.IFS;
|
|
90
|
+
this._split(this.inputBuffer, ifs === undefined ? ' \t\n' : ifs)
|
|
91
|
+
.forEach((value, i) => this.context.setVar(this.names[i], value));
|
|
92
|
+
this.completionCallback();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
//one value per name, the last one takes the remainder
|
|
96
|
+
_split(line, ifs) {
|
|
97
|
+
const {names} = this;
|
|
98
|
+
const esc = s => s.replace(/[\]\\^-]/g, '\\$&');
|
|
99
|
+
const ws = esc(ifs.replace(/[^ \t\n]/g, ''));
|
|
100
|
+
const other = esc(ifs.replace(/[ \t\n]/g, ''));
|
|
101
|
+
const trimStart = s => ws ? s.replace(new RegExp(`^[${ws}]+`), '') : s;
|
|
102
|
+
const trimEnd = s => ws ? s.replace(new RegExp(`[${ws}]+$`), '') : s;
|
|
103
|
+
const delim = ifs === '' ? null : new RegExp(other ? `[${ws}]*[${other}][${ws}]*|[${ws}]+` : `[${ws}]+`);
|
|
104
|
+
|
|
105
|
+
let rest = trimStart(line);
|
|
106
|
+
return names.map((_, i) => {
|
|
107
|
+
if(i === names.length -1) {
|
|
108
|
+
return trimEnd(rest);
|
|
109
|
+
}
|
|
110
|
+
const m = delim && delim.exec(rest);
|
|
111
|
+
if(!m) {
|
|
112
|
+
const field = rest;
|
|
113
|
+
rest = '';
|
|
114
|
+
return field;
|
|
115
|
+
}
|
|
116
|
+
const field = rest.slice(0, m.index);
|
|
117
|
+
rest = rest.slice(m.index + m[0].length);
|
|
118
|
+
return field;
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
81
122
|
printHelp(e) {
|
|
82
123
|
if(e) {
|
|
83
124
|
this.stdOut = e.message;
|
|
84
125
|
}
|
|
85
|
-
this.stdOut = 'usage: read [-n nchars] [-p prompt] [-t timeout] [name ...]'
|
|
126
|
+
this.stdOut = 'usage: read [-r] [-n nchars] [-p prompt] [-t timeout] [name ...]'
|
|
86
127
|
}
|
|
87
128
|
|
|
88
129
|
parseArgs() {
|
|
@@ -102,6 +143,11 @@ class Read extends LocalCommand {
|
|
|
102
143
|
alias: 't',
|
|
103
144
|
type: Number
|
|
104
145
|
},
|
|
146
|
+
{
|
|
147
|
+
name: 'raw',
|
|
148
|
+
alias: 'r',
|
|
149
|
+
type: Boolean
|
|
150
|
+
},
|
|
105
151
|
{
|
|
106
152
|
name: 'help',
|
|
107
153
|
alias: 'h',
|
|
@@ -109,6 +155,7 @@ class Read extends LocalCommand {
|
|
|
109
155
|
},
|
|
110
156
|
{
|
|
111
157
|
defaultOption: true,
|
|
158
|
+
multiple: true,
|
|
112
159
|
name: 'name',
|
|
113
160
|
type: String
|
|
114
161
|
},
|
package/package.json
CHANGED
|
@@ -176,6 +176,92 @@ describe('read', () => {
|
|
|
176
176
|
expect(shell.context.getVar('CHAR')).toBe('x');
|
|
177
177
|
});
|
|
178
178
|
|
|
179
|
+
const feed = async (cmd, text) => {
|
|
180
|
+
const p = shell.onCommand(cmd);
|
|
181
|
+
await new Promise(resolve => setImmediate(resolve));
|
|
182
|
+
[...text].forEach(c => shell.onInput(c));
|
|
183
|
+
shell.onInput('Enter');
|
|
184
|
+
await p;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
it('splits input across multiple names, last takes the remainder', async () => {
|
|
188
|
+
await feed('read -t 1 A B C', ' one two three four ');
|
|
189
|
+
expect(shell.context.getVar('A')).toBe('one');
|
|
190
|
+
expect(shell.context.getVar('B')).toBe('two');
|
|
191
|
+
expect(shell.context.getVar('C')).toBe('three four');
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('respects IFS', async () => {
|
|
195
|
+
await shell.onCommand('export IFS=:');
|
|
196
|
+
await feed('read -t 1 A B', 'x:y:z');
|
|
197
|
+
expect(shell.context.getVar('A')).toBe('x');
|
|
198
|
+
expect(shell.context.getVar('B')).toBe('y:z');
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('trims surrounding IFS whitespace with a single name', async () => {
|
|
202
|
+
await feed('read -t 1 V', ' padded value ');
|
|
203
|
+
expect(shell.context.getVar('V')).toBe('padded value');
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('leaves extra names empty when input has fewer fields', async () => {
|
|
207
|
+
await feed('read -t 1 A B C', 'only');
|
|
208
|
+
expect(shell.context.getVar('A')).toBe('only');
|
|
209
|
+
expect(shell.context.getVar('B')).toBe('');
|
|
210
|
+
expect(shell.context.getVar('C')).toBe('');
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('handles whitespace around non-whitespace IFS separators', async () => {
|
|
214
|
+
await shell.onCommand('export IFS=" :"');
|
|
215
|
+
await feed('read -t 1 A B C', 'x : y z');
|
|
216
|
+
expect(shell.context.getVar('A')).toBe('x');
|
|
217
|
+
expect(shell.context.getVar('B')).toBe('y');
|
|
218
|
+
expect(shell.context.getVar('C')).toBe('z');
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it('treats an escaped backslash as a literal backslash without -r', async () => {
|
|
222
|
+
await feed('read -t 1 V', 'a\\\\b');
|
|
223
|
+
expect(shell.context.getVar('V')).toBe('a\\b');
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it('continues the line on backslash-newline without -r', async () => {
|
|
227
|
+
const p = shell.onCommand('read -t 1 V');
|
|
228
|
+
await new Promise(resolve => setImmediate(resolve));
|
|
229
|
+
['a', '\\', 'Enter', 'b', 'Enter'].forEach(k => shell.onInput(k));
|
|
230
|
+
await p;
|
|
231
|
+
expect(shell.context.getVar('V')).toBe('ab');
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it('ends the line on backslash-newline with -r', async () => {
|
|
235
|
+
const p = shell.onCommand('read -r -t 1 V');
|
|
236
|
+
await new Promise(resolve => setImmediate(resolve));
|
|
237
|
+
['a', '\\', 'Enter'].forEach(k => shell.onInput(k));
|
|
238
|
+
await p;
|
|
239
|
+
expect(shell.context.getVar('V')).toBe('a\\');
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('accepts -r combined with -n', async () => {
|
|
243
|
+
const p = shell.onCommand('read -r -n 3 -t 1 V');
|
|
244
|
+
await new Promise(resolve => setImmediate(resolve));
|
|
245
|
+
[...'a\\bcd'].forEach(c => shell.onInput(c));
|
|
246
|
+
await p;
|
|
247
|
+
expect(shell.context.getVar('V')).toBe('a\\b');
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it('mentions -r in usage', async () => {
|
|
251
|
+
const { output } = await run(shell, 'read -h');
|
|
252
|
+
expect(output.join(' ')).toContain('-r');
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it('strips backslashes without -r', async () => {
|
|
256
|
+
await feed('read -t 1 V', 'a\\bc');
|
|
257
|
+
expect(shell.context.getVar('V')).toBe('abc');
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('keeps backslashes with -r', async () => {
|
|
261
|
+
await feed('read -r -t 1 V', 'a\\bc');
|
|
262
|
+
expect(shell.context.getVar('V')).toBe('a\\bc');
|
|
263
|
+
});
|
|
264
|
+
|
|
179
265
|
it('shows help with -h', async () => {
|
|
180
266
|
const { output } = await run(shell, 'read -h');
|
|
181
267
|
expect(output.join(' ')).toMatch(/usage/i);
|