forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathinspect.ts
More file actions
68 lines (56 loc) · 1.63 KB
/
Copy pathinspect.ts
File metadata and controls
68 lines (56 loc) · 1.63 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { Block, Tokens } from '../primitives.js';
import { isSpace } from '../util.js';
interface Width {
line: number;
start: number;
delimiter: number;
postDelimiter: number;
tag: number;
postTag: number;
name: number;
postName: number;
type: number;
postType: number;
description: number;
end: number;
lineEnd: number;
}
const zeroWidth = {
line: 0,
start: 0,
delimiter: 0,
postDelimiter: 0,
tag: 0,
postTag: 0,
name: 0,
postName: 0,
type: 0,
postType: 0,
description: 0,
end: 0,
lineEnd: 0,
};
const headers = { lineEnd: 'CR' };
const fields = Object.keys(zeroWidth);
const repr = (x: string) => (isSpace(x) ? `{${x.length}}` : x);
const frame = (line: string[]) => '|' + line.join('|') + '|';
const align = (width: Width, tokens: Tokens): string[] =>
Object.keys(tokens).map((k) => repr(tokens[k]).padEnd(width[k]));
export default function inspect({ source }: Block): string {
if (source.length === 0) return '';
const width: Width = { ...zeroWidth };
for (const f of fields) width[f] = (headers[f] ?? f).length;
for (const { number, tokens } of source) {
width.line = Math.max(width.line, number.toString().length);
for (const k in tokens)
width[k] = Math.max(width[k], repr(tokens[k]).length);
}
const lines: string[][] = [[], []];
for (const f of fields) lines[0].push((headers[f] ?? f).padEnd(width[f]));
for (const f of fields) lines[1].push('-'.padEnd(width[f], '-'));
for (const { number, tokens } of source) {
const line = number.toString().padStart(width.line);
lines.push([line, ...align(width, tokens)]);
}
return lines.map(frame).join('\n');
}