@celljs/ta-workspace 0.0.0-stage → 4.6.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/README.md +28 -2
- package/package.json +39 -4
- package/web/index.js +7289 -0
- package/web-src/browser.tsx +240 -0
- package/web-src/common.tsx +296 -0
- package/web-src/editors.tsx +471 -0
- package/web-src/explorer.tsx +479 -0
- package/web-src/file-icon.tsx +21 -0
- package/web-src/host.ts +121 -0
- package/web-src/i18n.ts +320 -0
- package/web-src/index.tsx +65 -0
- package/web-src/open-files.tsx +80 -0
- package/web-src/preview.tsx +132 -0
- package/web-src/react.ts +8 -0
- package/web-src/scm.tsx +388 -0
- package/web-src/search.tsx +485 -0
- package/web-src/style.ts +225 -0
- package/web-src/terminal.tsx +350 -0
- package/web-src/util/ansi.spec.ts +66 -0
- package/web-src/util/ansi.ts +438 -0
- package/web-src/util/file-icon.spec.ts +35 -0
- package/web-src/util/file-icon.ts +23 -0
- package/web-src/util/git.ts +203 -0
- package/web-src/util/keys.ts +56 -0
- package/web-src/util/preview.spec.ts +48 -0
- package/web-src/util/preview.ts +90 -0
- package/web-src/util/search.spec.ts +164 -0
- package/web-src/util/search.ts +274 -0
- package/web-src/util/seti-font.ts +6 -0
- package/web-src/util/seti.ts +761 -0
- package/web-src/util/workspace.spec.ts +191 -0
- package/web-src/util/workspace.ts +331 -0
- package/web-src/workspace-client.ts +119 -0
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
/** How a run of characters looks: SGR of a terminal. Colors are 0–255 of the xterm palette, or `#rrggbb`. */
|
|
2
|
+
export interface AnsiStyle {
|
|
3
|
+
fg?: number | string;
|
|
4
|
+
bg?: number | string;
|
|
5
|
+
bold?: boolean;
|
|
6
|
+
dim?: boolean;
|
|
7
|
+
italic?: boolean;
|
|
8
|
+
underline?: boolean;
|
|
9
|
+
inverse?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface AnsiSpan {
|
|
13
|
+
text: string;
|
|
14
|
+
style: AnsiStyle;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface Cell {
|
|
18
|
+
char: string;
|
|
19
|
+
style: AnsiStyle;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const PLAIN: AnsiStyle = Object.freeze({});
|
|
23
|
+
/** A control sequence: parameters, intermediates and the final byte. */
|
|
24
|
+
const ESC = '\x1b';
|
|
25
|
+
const CSI = new RegExp(`^${ESC}\\[([0-?]*)([ -/]*)([@-~])`);
|
|
26
|
+
const DEFAULT_MAX_LINES = 2000;
|
|
27
|
+
const TAB = 8;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The smallest screen a shell needs: lines of styled characters and a cursor. It follows SGR colors,
|
|
31
|
+
* `\r`, `\b`, `\t`, moving the cursor, erasing and clearing; titles and modes it reads and drops.
|
|
32
|
+
* Lines that scroll off stay, up to `maxLines`.
|
|
33
|
+
*/
|
|
34
|
+
export class AnsiScreen {
|
|
35
|
+
protected lines: Cell[][] = [[]];
|
|
36
|
+
/** The line of the cursor, an index of `lines`. */
|
|
37
|
+
protected row = 0;
|
|
38
|
+
protected col = 0;
|
|
39
|
+
protected style: AnsiStyle = PLAIN;
|
|
40
|
+
/** The end of the last `feed`, when it stopped inside a sequence. */
|
|
41
|
+
protected rest = '';
|
|
42
|
+
protected savedCursor?: { row: number; col: number };
|
|
43
|
+
/** Bumped by each change: a view draws again when it changed. */
|
|
44
|
+
version = 0;
|
|
45
|
+
|
|
46
|
+
constructor(
|
|
47
|
+
protected cols = 80,
|
|
48
|
+
protected rows = 24,
|
|
49
|
+
protected readonly maxLines = DEFAULT_MAX_LINES,
|
|
50
|
+
) {}
|
|
51
|
+
|
|
52
|
+
resize(cols: number, rows: number): void {
|
|
53
|
+
this.cols = Math.max(1, cols);
|
|
54
|
+
this.rows = Math.max(1, rows);
|
|
55
|
+
this.version++;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
feed(input: string): void {
|
|
59
|
+
const text = this.rest + input;
|
|
60
|
+
this.rest = '';
|
|
61
|
+
let i = 0;
|
|
62
|
+
while (i < text.length) {
|
|
63
|
+
const char = text[i];
|
|
64
|
+
if (char === '\x1b') {
|
|
65
|
+
const used = this.escape(text, i);
|
|
66
|
+
if (used < 0) {
|
|
67
|
+
this.rest = text.slice(i);
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
i += used;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
i++;
|
|
74
|
+
if (char === '\r') {
|
|
75
|
+
this.col = 0;
|
|
76
|
+
} else if (char === '\n') {
|
|
77
|
+
this.lineFeed();
|
|
78
|
+
} else if (char === '\b') {
|
|
79
|
+
this.col = Math.max(0, this.col - 1);
|
|
80
|
+
} else if (char === '\t') {
|
|
81
|
+
this.col = Math.min(this.cols - 1, (Math.floor(this.col / TAB) + 1) * TAB);
|
|
82
|
+
} else if (char >= ' ' && char !== '\x7f') {
|
|
83
|
+
const code = char.charCodeAt(0);
|
|
84
|
+
// A pair of surrogates is one character.
|
|
85
|
+
if (code >= 0xd800 && code < 0xdc00 && i < text.length) {
|
|
86
|
+
this.put(char + text[i]);
|
|
87
|
+
i++;
|
|
88
|
+
} else {
|
|
89
|
+
this.put(char);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
this.version++;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
clear(): void {
|
|
97
|
+
this.lines = [[]];
|
|
98
|
+
this.row = 0;
|
|
99
|
+
this.col = 0;
|
|
100
|
+
this.version++;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The lines, each a list of runs of one style. */
|
|
104
|
+
spans(): AnsiSpan[][] {
|
|
105
|
+
return this.lines.map(line => {
|
|
106
|
+
const spans: AnsiSpan[] = [];
|
|
107
|
+
for (const cell of line) {
|
|
108
|
+
const last = spans[spans.length - 1];
|
|
109
|
+
if (last && last.style === cell.style) {
|
|
110
|
+
last.text += cell.char;
|
|
111
|
+
} else {
|
|
112
|
+
spans.push({ text: cell.char, style: cell.style });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return spans;
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** The text, without styles; for tests and copying. */
|
|
120
|
+
text(): string {
|
|
121
|
+
return this.lines
|
|
122
|
+
.map(line =>
|
|
123
|
+
line
|
|
124
|
+
.map(cell => cell.char)
|
|
125
|
+
.join('')
|
|
126
|
+
.trimEnd(),
|
|
127
|
+
)
|
|
128
|
+
.join('\n');
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
cursor(): { row: number; col: number } {
|
|
132
|
+
return { row: this.row, col: this.col };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
protected put(char: string): void {
|
|
136
|
+
if (this.col >= this.cols) {
|
|
137
|
+
this.col = 0;
|
|
138
|
+
this.lineFeed();
|
|
139
|
+
}
|
|
140
|
+
const line = this.lines[this.row];
|
|
141
|
+
while (line.length < this.col) {
|
|
142
|
+
line.push({ char: ' ', style: PLAIN });
|
|
143
|
+
}
|
|
144
|
+
line[this.col] = { char, style: this.style };
|
|
145
|
+
this.col++;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
protected lineFeed(): void {
|
|
149
|
+
this.row++;
|
|
150
|
+
if (this.row === this.lines.length) {
|
|
151
|
+
this.lines.push([]);
|
|
152
|
+
}
|
|
153
|
+
if (this.lines.length > this.maxLines) {
|
|
154
|
+
const drop = this.lines.length - this.maxLines;
|
|
155
|
+
this.lines.splice(0, drop);
|
|
156
|
+
this.row -= drop;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** The first line of the screen: the last `rows` lines are the screen. */
|
|
161
|
+
protected top(): number {
|
|
162
|
+
return Math.max(0, this.lines.length - this.rows);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** How many characters the sequence at `start` takes; -1 when it is not complete yet. */
|
|
166
|
+
protected escape(text: string, start: number): number {
|
|
167
|
+
const kind = text[start + 1];
|
|
168
|
+
if (kind === undefined) {
|
|
169
|
+
return -1;
|
|
170
|
+
}
|
|
171
|
+
if (kind === '[') {
|
|
172
|
+
const found = CSI.exec(text.slice(start, start + 64));
|
|
173
|
+
if (!found) {
|
|
174
|
+
return text.length - start < 64 ? -1 : 2;
|
|
175
|
+
}
|
|
176
|
+
this.csi(found[1], found[3]);
|
|
177
|
+
return found[0].length;
|
|
178
|
+
}
|
|
179
|
+
if (kind === ']' || kind === 'P' || kind === '_' || kind === '^') {
|
|
180
|
+
// A title, a link or a device string: up to BEL or ST.
|
|
181
|
+
const bel = text.indexOf('\x07', start + 2);
|
|
182
|
+
const st = text.indexOf('\x1b\\', start + 2);
|
|
183
|
+
const end = bel < 0 ? st : st < 0 ? bel : Math.min(bel, st);
|
|
184
|
+
if (end < 0) {
|
|
185
|
+
return -1;
|
|
186
|
+
}
|
|
187
|
+
return end - start + (end === bel ? 1 : 2);
|
|
188
|
+
}
|
|
189
|
+
if (kind === '(' || kind === ')' || kind === '#') {
|
|
190
|
+
return text.length - start < 3 ? -1 : 3;
|
|
191
|
+
}
|
|
192
|
+
if (kind === '7') {
|
|
193
|
+
this.savedCursor = { row: this.row, col: this.col };
|
|
194
|
+
} else if (kind === '8' && this.savedCursor) {
|
|
195
|
+
({ row: this.row, col: this.col } = this.savedCursor);
|
|
196
|
+
} else if (kind === 'M') {
|
|
197
|
+
this.row = Math.max(this.top(), this.row - 1);
|
|
198
|
+
} else if (kind === 'c') {
|
|
199
|
+
this.clear();
|
|
200
|
+
this.style = PLAIN;
|
|
201
|
+
}
|
|
202
|
+
return 2;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
protected csi(params: string, final: string): void {
|
|
206
|
+
if (params.startsWith('?') || params.startsWith('>') || params.startsWith('=')) {
|
|
207
|
+
// Private modes: bracketed paste, the cursor, the alternate screen.
|
|
208
|
+
if (/^\?(1049|47|1047)$/.test(params) && (final === 'h' || final === 'l')) {
|
|
209
|
+
this.eraseScreen(2);
|
|
210
|
+
}
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const values = params.split(';').map(value => (value === '' ? undefined : Number(value)));
|
|
214
|
+
const n = values[0] ?? 1;
|
|
215
|
+
switch (final) {
|
|
216
|
+
case 'm':
|
|
217
|
+
this.sgr(values.map(value => value ?? 0));
|
|
218
|
+
break;
|
|
219
|
+
case 'A':
|
|
220
|
+
this.row = Math.max(this.top(), this.row - n);
|
|
221
|
+
break;
|
|
222
|
+
case 'B':
|
|
223
|
+
this.moveRow(this.row + n);
|
|
224
|
+
break;
|
|
225
|
+
case 'C':
|
|
226
|
+
this.col = Math.min(this.cols - 1, this.col + n);
|
|
227
|
+
break;
|
|
228
|
+
case 'D':
|
|
229
|
+
this.col = Math.max(0, this.col - n);
|
|
230
|
+
break;
|
|
231
|
+
case 'E':
|
|
232
|
+
this.moveRow(this.row + n);
|
|
233
|
+
this.col = 0;
|
|
234
|
+
break;
|
|
235
|
+
case 'F':
|
|
236
|
+
this.row = Math.max(this.top(), this.row - n);
|
|
237
|
+
this.col = 0;
|
|
238
|
+
break;
|
|
239
|
+
case 'G':
|
|
240
|
+
this.col = Math.min(this.cols - 1, Math.max(0, n - 1));
|
|
241
|
+
break;
|
|
242
|
+
case 'H':
|
|
243
|
+
case 'f':
|
|
244
|
+
this.moveRow(this.top() + Math.max(0, (values[0] ?? 1) - 1));
|
|
245
|
+
this.col = Math.min(this.cols - 1, Math.max(0, (values[1] ?? 1) - 1));
|
|
246
|
+
break;
|
|
247
|
+
case 'J':
|
|
248
|
+
this.eraseScreen(values[0] ?? 0);
|
|
249
|
+
break;
|
|
250
|
+
case 'K':
|
|
251
|
+
this.eraseLine(values[0] ?? 0);
|
|
252
|
+
break;
|
|
253
|
+
case 'P':
|
|
254
|
+
this.lines[this.row].splice(this.col, n);
|
|
255
|
+
break;
|
|
256
|
+
case 'X':
|
|
257
|
+
for (let i = this.col; i < Math.min(this.col + n, this.lines[this.row].length); i++) {
|
|
258
|
+
this.lines[this.row][i] = { char: ' ', style: PLAIN };
|
|
259
|
+
}
|
|
260
|
+
break;
|
|
261
|
+
case '@':
|
|
262
|
+
if (this.col < this.lines[this.row].length) {
|
|
263
|
+
this.lines[this.row].splice(this.col, 0, ...Array.from({ length: n }, () => ({ char: ' ', style: PLAIN })));
|
|
264
|
+
}
|
|
265
|
+
break;
|
|
266
|
+
case 's':
|
|
267
|
+
this.savedCursor = { row: this.row, col: this.col };
|
|
268
|
+
break;
|
|
269
|
+
case 'u':
|
|
270
|
+
if (this.savedCursor) {
|
|
271
|
+
({ row: this.row, col: this.col } = this.savedCursor);
|
|
272
|
+
}
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
protected moveRow(row: number): void {
|
|
278
|
+
while (this.lines.length <= row) {
|
|
279
|
+
this.lines.push([]);
|
|
280
|
+
}
|
|
281
|
+
this.row = row;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
protected eraseLine(mode: number): void {
|
|
285
|
+
const line = this.lines[this.row];
|
|
286
|
+
if (mode === 0) {
|
|
287
|
+
line.length = Math.min(line.length, this.col);
|
|
288
|
+
} else if (mode === 1) {
|
|
289
|
+
for (let i = 0; i <= Math.min(this.col, line.length - 1); i++) {
|
|
290
|
+
line[i] = { char: ' ', style: PLAIN };
|
|
291
|
+
}
|
|
292
|
+
} else {
|
|
293
|
+
line.length = 0;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
protected eraseScreen(mode: number): void {
|
|
298
|
+
if (mode === 0) {
|
|
299
|
+
this.eraseLine(0);
|
|
300
|
+
this.lines.length = this.row + 1;
|
|
301
|
+
} else if (mode === 1) {
|
|
302
|
+
for (let i = this.top(); i < this.row; i++) {
|
|
303
|
+
this.lines[i] = [];
|
|
304
|
+
}
|
|
305
|
+
this.eraseLine(1);
|
|
306
|
+
} else {
|
|
307
|
+
// The screen scrolls away and stays above, as in xterm; 3 also drops what is above.
|
|
308
|
+
if (mode === 3) {
|
|
309
|
+
this.lines = [[]];
|
|
310
|
+
} else {
|
|
311
|
+
const used = this.lines.length > 1 || this.lines[0].length > 0;
|
|
312
|
+
for (let i = 0; used && i < this.rows; i++) {
|
|
313
|
+
this.lines.push([]);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
this.row = this.top();
|
|
317
|
+
this.col = 0;
|
|
318
|
+
if (this.lines.length > this.maxLines) {
|
|
319
|
+
const drop = this.lines.length - this.maxLines;
|
|
320
|
+
this.lines.splice(0, drop);
|
|
321
|
+
this.row -= drop;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
protected sgr(values: number[]): void {
|
|
327
|
+
const style: AnsiStyle = { ...this.style };
|
|
328
|
+
for (let i = 0; i < values.length; i++) {
|
|
329
|
+
const value = values[i];
|
|
330
|
+
if (value === 0) {
|
|
331
|
+
for (const key of Object.keys(style) as (keyof AnsiStyle)[]) {
|
|
332
|
+
delete style[key];
|
|
333
|
+
}
|
|
334
|
+
} else if (value === 1) {
|
|
335
|
+
style.bold = true;
|
|
336
|
+
} else if (value === 2) {
|
|
337
|
+
style.dim = true;
|
|
338
|
+
} else if (value === 3) {
|
|
339
|
+
style.italic = true;
|
|
340
|
+
} else if (value === 4) {
|
|
341
|
+
style.underline = true;
|
|
342
|
+
} else if (value === 7) {
|
|
343
|
+
style.inverse = true;
|
|
344
|
+
} else if (value === 22) {
|
|
345
|
+
delete style.bold;
|
|
346
|
+
delete style.dim;
|
|
347
|
+
} else if (value === 23) {
|
|
348
|
+
delete style.italic;
|
|
349
|
+
} else if (value === 24) {
|
|
350
|
+
delete style.underline;
|
|
351
|
+
} else if (value === 27) {
|
|
352
|
+
delete style.inverse;
|
|
353
|
+
} else if (value >= 30 && value <= 37) {
|
|
354
|
+
style.fg = value - 30;
|
|
355
|
+
} else if (value >= 90 && value <= 97) {
|
|
356
|
+
style.fg = value - 90 + 8;
|
|
357
|
+
} else if (value === 39) {
|
|
358
|
+
delete style.fg;
|
|
359
|
+
} else if (value >= 40 && value <= 47) {
|
|
360
|
+
style.bg = value - 40;
|
|
361
|
+
} else if (value >= 100 && value <= 107) {
|
|
362
|
+
style.bg = value - 100 + 8;
|
|
363
|
+
} else if (value === 49) {
|
|
364
|
+
delete style.bg;
|
|
365
|
+
} else if (value === 38 || value === 48) {
|
|
366
|
+
const key = value === 38 ? 'fg' : 'bg';
|
|
367
|
+
if (values[i + 1] === 5 && values[i + 2] !== undefined) {
|
|
368
|
+
style[key] = values[i + 2];
|
|
369
|
+
i += 2;
|
|
370
|
+
} else if (values[i + 1] === 2 && values[i + 4] !== undefined) {
|
|
371
|
+
style[key] = `#${values
|
|
372
|
+
.slice(i + 2, i + 5)
|
|
373
|
+
.map(part => Math.min(255, part).toString(16).padStart(2, '0'))
|
|
374
|
+
.join('')}`;
|
|
375
|
+
i += 4;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
this.style = Object.keys(style).length ? style : PLAIN;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** The colors of the xterm palette as CSS: the first 16 follow the theme of the page. */
|
|
384
|
+
export namespace AnsiUtil {
|
|
385
|
+
const BASIC = ['--ta-text', '--ta-red', '--ta-green', '--ta-yellow', '--ta-blue', '#c084fc', '#22d3ee', '--ta-secondary'];
|
|
386
|
+
const STEPS = [0, 95, 135, 175, 215, 255];
|
|
387
|
+
|
|
388
|
+
export function color(value: number | string | undefined): string | undefined {
|
|
389
|
+
if (value === undefined || typeof value === 'string') {
|
|
390
|
+
return value;
|
|
391
|
+
}
|
|
392
|
+
if (value < 16) {
|
|
393
|
+
if (value % 8 === 0) {
|
|
394
|
+
return 'var(--ta-muted)';
|
|
395
|
+
}
|
|
396
|
+
const name = value === 15 ? '--ta-text' : BASIC[value % 8];
|
|
397
|
+
return name.startsWith('--') ? `var(${name})` : name;
|
|
398
|
+
}
|
|
399
|
+
if (value < 232) {
|
|
400
|
+
const n = value - 16;
|
|
401
|
+
return `rgb(${STEPS[Math.floor(n / 36)]}, ${STEPS[Math.floor(n / 6) % 6]}, ${STEPS[n % 6]})`;
|
|
402
|
+
}
|
|
403
|
+
const gray = 8 + (value - 232) * 10;
|
|
404
|
+
return `rgb(${gray}, ${gray}, ${gray})`;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** Inline CSS of a style. */
|
|
408
|
+
export function css(style: AnsiStyle): Record<string, string> | undefined {
|
|
409
|
+
if (style === PLAIN || !Object.keys(style).length) {
|
|
410
|
+
return undefined;
|
|
411
|
+
}
|
|
412
|
+
let fg = color(style.fg);
|
|
413
|
+
let bg = color(style.bg);
|
|
414
|
+
if (style.inverse) {
|
|
415
|
+
[fg, bg] = [bg ?? 'var(--ta-main)', fg ?? 'var(--ta-text)'];
|
|
416
|
+
}
|
|
417
|
+
const css: Record<string, string> = {};
|
|
418
|
+
if (fg) {
|
|
419
|
+
css.color = fg;
|
|
420
|
+
}
|
|
421
|
+
if (bg) {
|
|
422
|
+
css.background = bg;
|
|
423
|
+
}
|
|
424
|
+
if (style.bold) {
|
|
425
|
+
css.fontWeight = '600';
|
|
426
|
+
}
|
|
427
|
+
if (style.dim) {
|
|
428
|
+
css.opacity = '0.65';
|
|
429
|
+
}
|
|
430
|
+
if (style.italic) {
|
|
431
|
+
css.fontStyle = 'italic';
|
|
432
|
+
}
|
|
433
|
+
if (style.underline) {
|
|
434
|
+
css.textDecoration = 'underline';
|
|
435
|
+
}
|
|
436
|
+
return css;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { FileIconUtil } from './file-icon';
|
|
3
|
+
import { SETI_EXTENSIONS, SETI_FILE, SETI_FILE_NAMES, SETI_ICONS } from './seti';
|
|
4
|
+
|
|
5
|
+
describe('FileIconUtil', () => {
|
|
6
|
+
it('picks by the whole name first, in any case', () => {
|
|
7
|
+
expect(FileIconUtil.of('src/README.md')).toBe(SETI_ICONS[SETI_FILE_NAMES['readme.md']]);
|
|
8
|
+
expect(FileIconUtil.of('Dockerfile')).toBe(SETI_ICONS[SETI_FILE_NAMES.dockerfile]);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('picks the longest extension, then shorter ones', () => {
|
|
12
|
+
expect(FileIconUtil.of('a/b.spec.ts')).toBe(SETI_ICONS[SETI_EXTENSIONS['spec.ts']]);
|
|
13
|
+
expect(FileIconUtil.of('a/b.ts')).toBe(SETI_ICONS[SETI_EXTENSIONS.ts]);
|
|
14
|
+
expect(FileIconUtil.of('a/b.d.ts')).toBe(SETI_ICONS[SETI_EXTENSIONS.ts]);
|
|
15
|
+
expect(FileIconUtil.of('.gitignore')).toBe(SETI_ICONS[SETI_EXTENSIONS.gitignore]);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('maps the languages of VS Code to extensions', () => {
|
|
19
|
+
expect(SETI_EXTENSIONS.ts).not.toBe(SETI_FILE);
|
|
20
|
+
expect(FileIconUtil.of('main.py')).not.toBe(SETI_ICONS[SETI_FILE]);
|
|
21
|
+
expect(FileIconUtil.of('index.tsx')).toBe(FileIconUtil.of('index.jsx'));
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('falls back to the icon of any file', () => {
|
|
25
|
+
expect(FileIconUtil.of('LICENSE.unknown-kind')).toBe(SETI_ICONS[SETI_FILE]);
|
|
26
|
+
expect(FileIconUtil.of('noextension')).toBe(SETI_ICONS[SETI_FILE]);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('gives a character of the font and a color for each theme', () => {
|
|
30
|
+
const [character, dark, light] = FileIconUtil.of('a.ts');
|
|
31
|
+
expect(character).toBeGreaterThanOrEqual(0xe000);
|
|
32
|
+
expect(dark).toMatch(/^#[0-9a-f]{6}$/i);
|
|
33
|
+
expect(light).toMatch(/^#[0-9a-f]{6}$/i);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { SETI_EXTENSIONS, SETI_FILE, SETI_FILE_NAMES, SETI_ICONS, type SetiIcon } from './seti';
|
|
2
|
+
|
|
3
|
+
/** The icons of files, as VS Code draws them by default (its Seti theme). */
|
|
4
|
+
export namespace FileIconUtil {
|
|
5
|
+
/**
|
|
6
|
+
* The icon of a file by its name, as VS Code picks it: the whole name, then the longest
|
|
7
|
+
* extension (`a.spec.ts` tries `spec.ts`, then `ts`), then the icon of any file.
|
|
8
|
+
*/
|
|
9
|
+
export function of(path: string): SetiIcon {
|
|
10
|
+
const name = path.slice(path.lastIndexOf('/') + 1).toLowerCase();
|
|
11
|
+
const byName = SETI_FILE_NAMES[name];
|
|
12
|
+
if (byName !== undefined) {
|
|
13
|
+
return SETI_ICONS[byName];
|
|
14
|
+
}
|
|
15
|
+
for (let dot = name.indexOf('.'); dot >= 0; dot = name.indexOf('.', dot + 1)) {
|
|
16
|
+
const byExtension = SETI_EXTENSIONS[name.slice(dot + 1)];
|
|
17
|
+
if (byExtension !== undefined) {
|
|
18
|
+
return SETI_ICONS[byExtension];
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return SETI_ICONS[SETI_FILE];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
export interface GitFile {
|
|
2
|
+
path: string;
|
|
3
|
+
/** The letter of `git status --porcelain` for the index, when it changed there. */
|
|
4
|
+
staged?: string;
|
|
5
|
+
/** The letter for the working tree, when it changed there (`?` for untracked). */
|
|
6
|
+
unstaged?: string;
|
|
7
|
+
/** Renamed or copied in the index: the path it came from, relative to the directory when inside it. */
|
|
8
|
+
from?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface GitStatus {
|
|
12
|
+
/** False when the directory is not in a git repository: the rest is left out. */
|
|
13
|
+
repository: boolean;
|
|
14
|
+
branch?: string;
|
|
15
|
+
ahead?: number;
|
|
16
|
+
behind?: number;
|
|
17
|
+
/** No commit yet: there is no HEAD to unstage against. */
|
|
18
|
+
initial?: boolean;
|
|
19
|
+
files: GitFile[];
|
|
20
|
+
/** Asked with `ignored`: what .gitignore leaves out; a directory left out whole ends with `/`. */
|
|
21
|
+
ignored?: string[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface FileDiff {
|
|
25
|
+
path: string;
|
|
26
|
+
/** The file in HEAD (or the index, for a change not staged); empty when new. */
|
|
27
|
+
before: string;
|
|
28
|
+
/** The file in the index (staged) or the working tree; empty when deleted. */
|
|
29
|
+
after: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** How the explorer marks files: the letter of each changed file and folder, and what is ignored. */
|
|
33
|
+
export interface GitDecorations {
|
|
34
|
+
files: Map<string, string>;
|
|
35
|
+
/** A folder takes the letter of the change inside it that matters most (a conflict, then a change, then a new file). */
|
|
36
|
+
folders: Map<string, string>;
|
|
37
|
+
ignored: string[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Conflicts first, as VS Code colors folders. */
|
|
41
|
+
const PRIORITY: Record<string, number> = { '!': 4, M: 3, R: 3, D: 3, C: 3, A: 2, U: 2 };
|
|
42
|
+
|
|
43
|
+
/** The arguments of git for the views, and its output read back. */
|
|
44
|
+
export namespace GitUtil {
|
|
45
|
+
export const PREFIX = ['rev-parse', '--show-prefix'];
|
|
46
|
+
export const COMMIT = ['commit', '-q', '-F', '-'];
|
|
47
|
+
|
|
48
|
+
/** `git status`, with what is ignored for the explorer (`--ignored=matching` stops at a directory ignored whole). */
|
|
49
|
+
export function status(ignored = false): string[] {
|
|
50
|
+
return [
|
|
51
|
+
'-c',
|
|
52
|
+
'core.quotepath=off',
|
|
53
|
+
'status',
|
|
54
|
+
'--porcelain=v1',
|
|
55
|
+
'-b',
|
|
56
|
+
'-z',
|
|
57
|
+
'--untracked-files=all',
|
|
58
|
+
...(ignored ? ['--ignored=matching'] : []),
|
|
59
|
+
'--',
|
|
60
|
+
'.',
|
|
61
|
+
];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The file at `revision` (`HEAD`, or `''` for the index), by its path relative to the directory. */
|
|
65
|
+
export function show(revision: string, path: string): string[] {
|
|
66
|
+
return ['show', `${revision}:./${path}`];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function tracked(path: string): string[] {
|
|
70
|
+
return ['ls-files', '--error-unmatch', '--', literal(path)];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** A path as git reads it, without its wildcards: `a[1].ts` is that file. `.` is the whole directory. */
|
|
74
|
+
export function literal(path: string): string {
|
|
75
|
+
return path === '.' ? '.' : `:(literal)${path}`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function stage(paths: string[]): string[] {
|
|
79
|
+
return ['add', '-A', '--', ...paths.map(literal)];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Before the first commit there is no HEAD: the files leave the index instead. */
|
|
83
|
+
export function unstage(paths: string[], initial: boolean): string[] {
|
|
84
|
+
return initial ? ['rm', '--cached', '-r', '-q', '--', ...paths.map(literal)] : ['reset', '-q', '--', ...paths.map(literal)];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The working tree back to the index, for files git tracks. */
|
|
88
|
+
export function discard(paths: string[]): string[] {
|
|
89
|
+
return ['checkout', '-q', '--', ...paths.map(literal)];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Deletes untracked files. */
|
|
93
|
+
export function clean(paths: string[]): string[] {
|
|
94
|
+
return ['clean', '-f', '-q', '--', ...paths.map(literal)];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* `git status --porcelain=v1 -b -z`: the branch and the files, with paths relative to `prefix`
|
|
99
|
+
* (`git rev-parse --show-prefix`, the directory asked for inside the repository); others are left out.
|
|
100
|
+
*/
|
|
101
|
+
export function parseStatus(output: string, prefix: string): GitStatus {
|
|
102
|
+
const status: GitStatus = { repository: true, files: [] };
|
|
103
|
+
const fields = output.split('\0');
|
|
104
|
+
const inside = (path: string) => path.startsWith(prefix);
|
|
105
|
+
for (let i = 0; i < fields.length; i++) {
|
|
106
|
+
const field = fields[i];
|
|
107
|
+
if (field.startsWith('## ')) {
|
|
108
|
+
Object.assign(status, parseBranch(field.slice(3)));
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (field.length < 4) {
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
const x = field[0];
|
|
115
|
+
const y = field[1];
|
|
116
|
+
const path = field.slice(3);
|
|
117
|
+
// The path it came from follows.
|
|
118
|
+
const from = x === 'R' || x === 'C' ? fields[++i] : undefined;
|
|
119
|
+
if (!inside(path)) {
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
const relative = path.slice(prefix.length);
|
|
123
|
+
if (x === '!') {
|
|
124
|
+
status.ignored ??= [];
|
|
125
|
+
status.ignored.push(relative);
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const file: GitFile = { path: relative };
|
|
129
|
+
if (x === '?') {
|
|
130
|
+
file.unstaged = '?';
|
|
131
|
+
} else {
|
|
132
|
+
if (x !== ' ') {
|
|
133
|
+
file.staged = x;
|
|
134
|
+
}
|
|
135
|
+
if (y !== ' ') {
|
|
136
|
+
file.unstaged = y;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (from !== undefined) {
|
|
140
|
+
file.from = inside(from) ? from.slice(prefix.length) : from;
|
|
141
|
+
}
|
|
142
|
+
status.files.push(file);
|
|
143
|
+
}
|
|
144
|
+
return status;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** `main...origin/main [ahead 1, behind 2]`, `No commits yet on main`, `HEAD (no branch)`. */
|
|
148
|
+
export function parseBranch(line: string): Pick<GitStatus, 'branch' | 'ahead' | 'behind' | 'initial'> {
|
|
149
|
+
const initial = /^(?:No commits yet on|Initial commit on) (.+)$/.exec(line);
|
|
150
|
+
if (initial) {
|
|
151
|
+
return { branch: initial[1], initial: true };
|
|
152
|
+
}
|
|
153
|
+
const [, branch = line, rest = ''] = /^(.+?)(?:\.\.\.\S+)?(?: \[(.+)\])?$/.exec(line) ?? [];
|
|
154
|
+
const result: Pick<GitStatus, 'branch' | 'ahead' | 'behind'> = { branch: branch === 'HEAD (no branch)' ? undefined : branch };
|
|
155
|
+
const ahead = /ahead (\d+)/.exec(rest);
|
|
156
|
+
const behind = /behind (\d+)/.exec(rest);
|
|
157
|
+
if (ahead) {
|
|
158
|
+
result.ahead = Number(ahead[1]);
|
|
159
|
+
}
|
|
160
|
+
if (behind) {
|
|
161
|
+
result.behind = Number(behind[1]);
|
|
162
|
+
}
|
|
163
|
+
return result;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** A conflict: both sides changed the file (`UU`, `AA`, `DD`, or a `U` on either). */
|
|
167
|
+
export function conflict(file: GitFile): boolean {
|
|
168
|
+
return file.staged === 'U' || file.unstaged === 'U' || (file.staged === 'A' && file.unstaged === 'A') || (file.staged === 'D' && file.unstaged === 'D');
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** The letter VS Code shows for a file: the working tree's over the index's, `U` untracked, `!` a conflict. */
|
|
172
|
+
export function letter(file: GitFile): string {
|
|
173
|
+
if (conflict(file)) {
|
|
174
|
+
return '!';
|
|
175
|
+
}
|
|
176
|
+
if (file.unstaged === '?') {
|
|
177
|
+
return 'U';
|
|
178
|
+
}
|
|
179
|
+
return file.unstaged ?? file.staged ?? 'M';
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function decorations(status: GitStatus): GitDecorations {
|
|
183
|
+
const files = new Map<string, string>();
|
|
184
|
+
const folders = new Map<string, string>();
|
|
185
|
+
for (const file of status.files) {
|
|
186
|
+
const mark = letter(file);
|
|
187
|
+
files.set(file.path, mark);
|
|
188
|
+
for (let slash = file.path.indexOf('/'); slash > 0; slash = file.path.indexOf('/', slash + 1)) {
|
|
189
|
+
const folder = file.path.slice(0, slash);
|
|
190
|
+
const previous = folders.get(folder);
|
|
191
|
+
if (!previous || (PRIORITY[mark] ?? 0) > (PRIORITY[previous] ?? 0)) {
|
|
192
|
+
folders.set(folder, mark);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return { files, folders, ignored: status.ignored ?? [] };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Whether .gitignore leaves the path out: it, or a directory around it. */
|
|
200
|
+
export function ignored(decorations: GitDecorations, path: string): boolean {
|
|
201
|
+
return decorations.ignored.some(entry => (entry.endsWith('/') ? path === entry.slice(0, -1) || path.startsWith(entry) : path === entry));
|
|
202
|
+
}
|
|
203
|
+
}
|