-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
343 lines (310 loc) · 9.47 KB
/
index.ts
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import tint from '@termsurf/tint-text'
export const TIME_FORM = 'yyyy/MM/dd @ hh:mm:ss.SSSaaa'
export type LineListLink = {
code?: number
file: string
line?: number
task?: string
}
export function makeLinkList(list: Array<string>, hook?: HostLinkHook) {
const noteLine: Array<string> = []
const linkList: Array<LineListLink> = []
let find = false
let i = list.length - 1
while (i >= 0) {
const line = list[i--]
if (!find && line?.startsWith(' at ')) {
const link = readListLine(line.slice(' at '.length), hook)
if (link) {
linkList.push(link)
}
} else if (line) {
find = true
noteLine.push(line)
}
}
return { linkList: linkList.reverse(), note: noteLine }
}
export const makeTextHead = (
note: string,
code: string,
host: string,
time: string,
) => {
const list: Array<string> = []
const R = { tone: 'red' }
const RB = { tone: 'red', bold: true }
const W = { tone: 'white' }
const H = { tone: 'blackBright' }
const C = { tone: 'cyan' }
list.push(
tint(`kink`, R) +
' ' +
tint('<', H) +
tint(`${note}`, RB) +
tint('>', H),
)
list.push(
' ' + tint(`time <`, H) + tint(`${time}`, W) + tint('>,', H),
)
list.push(
' ' + tint(`code <`, H) + tint(`${code}`, C) + tint(`>`, H),
)
list.push(' ' + tint(`host `, H) + tint(`${host}`, H))
return list
}
export type MakeText = {
host: string
code: string
note: string
link?: Record<string, unknown>
time: string
// stack trace
list: Array<string>
hook?: HostLinkHook
}
/**
* This you pass it a stack trace and it will render the error text.
*/
const makeText = ({
host,
code,
note,
link = {},
list,
time,
hook,
}: MakeText): string => {
const textList: Array<string> = []
const W = { tone: 'white' }
const WB = { tone: 'whiteBright' }
const B = { tone: 'blue' }
const M = { tone: 'magenta' }
const MB = { tone: 'cyan', bold: true }
const G = { tone: 'green' }
const H = { tone: 'blackBright' }
textList.push(...makeTextHead(note, code, host, time))
textList.push(...makeLinkHash(link, 1))
const { linkList } = makeLinkList(list, hook)
linkList.forEach(link => {
let head = []
if (link.line) {
head.push(link.line)
}
if (link.code) {
head.push(link.code)
}
const headText = head.length ? ':' + head.join(':') : ''
const siteText = tint('site <', H)
const fileText = tint(`${link.file}${headText}`, WB)
textList.push(` ${siteText}${fileText}${tint('>', H)}`)
if (link.task) {
const callText = ' ' + tint(`call <`, H)
const taskText = tint(link.task, W)
textList.push(`${callText}${taskText}${tint('>', H)}`)
}
})
return textList.join('\n')
function makeLinkHash(link: Record<string, unknown>, move: number) {
const textList: Array<string> = []
const moveText = makeTextMove(move)
for (const name in link) {
const bond = link[name]
if (bond === undefined) {
textList.push(...makeLinkVoid(name, move))
} else if (bond === null) {
textList.push(...makeLinkNull(name, move))
} else if (typeof bond === 'boolean') {
textList.push(...makeLinkWave(name, bond, move))
} else if (typeof bond === 'string') {
textList.push(...makeLinkText(name, bond, move))
} else if (typeof bond === 'number') {
textList.push(...makeLinkSize(name, bond, move))
} else if (Array.isArray(bond)) {
bond.forEach(bond => {
textList.push(...makeLinkBond(name, bond, move))
})
} else if (typeof bond === 'object') {
let base = true
for (const bondName in bond) {
const bind = (bond as Record<string, unknown>)[bondName]
if (base) {
base = false
if (bind === undefined) {
textList.push(...makeLinkVoid(name, move))
} else if (bind === null) {
textList.push(...makeLinkNull(name, move))
} else if (typeof bind === 'boolean') {
textList.push(...makeLinkWave(name, bind, move))
} else if (typeof bind === 'string') {
textList.push(...makeLinkText(name, bind, move))
} else if (typeof bind === 'number') {
textList.push(...makeLinkSize(name, bind, move))
} else if (Array.isArray(bind)) {
textList.push(`${moveText}${tint(`bind ${name}`, H)}`)
bind.forEach(bind => {
textList.push(...makeLinkBond(bondName, bind, move + 1))
})
} else if (typeof bind === 'object') {
textList.push(`${moveText}${tint(`bind ${name}`, H)}`)
textList.push(...makeLinkBond(bondName, bind, move + 1))
}
} else {
if (bind === undefined) {
textList.push(...makeLinkVoid(bondName, move + 1))
} else if (bind === null) {
textList.push(...makeLinkNull(bondName, move + 1))
} else if (typeof bind === 'boolean') {
textList.push(...makeLinkWave(bondName, bind, move + 1))
} else if (typeof bind === 'string') {
textList.push(...makeLinkText(bondName, bind, move + 1))
} else if (typeof bind === 'number') {
textList.push(...makeLinkSize(bondName, bind, move + 1))
} else if (Array.isArray(bind)) {
bind.forEach(bind => {
textList.push(...makeLinkBond(bondName, bind, move + 1))
})
} else if (typeof bind === 'object') {
textList.push(...makeLinkBond(bondName, bind, move + 1))
}
}
}
}
}
return textList
}
function makeLinkBond(name: string, bond: unknown, move: number) {
const textList: Array<string> = []
if (bond === undefined) {
textList.push(...makeLinkVoid(name, move))
} else if (bond === null) {
textList.push(...makeLinkNull(name, move))
} else if (typeof bond === 'boolean') {
textList.push(...makeLinkWave(name, bond, move))
} else if (typeof bond === 'string') {
textList.push(...makeLinkText(name, bond, move))
} else if (typeof bond === 'number') {
textList.push(...makeLinkSize(name, bond, move))
} else if (typeof bond === 'object') {
const moveText = makeTextMove(move)
textList.push(`${moveText}${tint(`bind ${name}`, H)}`)
textList.push(
...makeLinkHash(bond as Record<string, unknown>, move + 1),
)
}
return textList
}
function makeLinkVoid(name: string, move: number) {
const textList: Array<string> = []
// const moveText = makeTextMove(move)
// textList.push(
// `${moveText}${tint(`${name} <`, H)}${tint('void', B)}${tint(
// `>`,
// H,
// )}`,
// )
return textList
}
function makeLinkNull(name: string, move: number) {
const textList: Array<string> = []
const moveText = makeTextMove(move)
textList.push(
`${moveText}${tint(`bind`, H)} ${tint(name, W)}${tint(
`,`,
H,
)} ${tint('null', MB)}`,
)
return textList
}
function makeLinkSize(name: string, bond: number, move: number) {
const textList: Array<string> = []
const moveText = makeTextMove(move)
textList.push(
`${moveText}${tint(`bind`, H)} ${tint(name, W)}${tint(
`,`,
H,
)} ${tint(String(bond), G)}`,
)
return textList
}
function makeLinkWave(name: string, bond: boolean, move: number) {
const textList: Array<string> = []
const moveText = makeTextMove(move)
textList.push(
`${moveText}${tint(`bind`, H)} ${tint(name, W)}${tint(
`,`,
H,
)} ${tint(String(bond), B)}`,
)
return textList
}
function makeLinkText(name: string, bond: string, move: number) {
const textList: Array<string> = []
const moveText = makeTextMove(move)
if (bond.match(/\n/)) {
textList.push(
`${moveText}${tint(`bind`, H)} ${tint(name, W)}${tint(
`, <`,
H,
)}`,
)
bond.split(/\n/).forEach(line => {
const moveNest = move + 1
const moveNestText = makeTextMove(moveNest)
textList.push(`${moveNestText}${tint(line, M)}`)
})
textList.push(`${moveText}${tint(`>`, H)}`)
} else {
textList.push(
`${moveText}${tint(`bind`, H)} ${tint(name, W)}${tint(
`, <`,
H,
)}${tint(bond, M)}${tint(`>`, H)}`,
)
}
return textList
}
}
export default makeText
export function readListLine(text: string, hook?: HostLinkHook) {
const [a, b] = text.trim().split(/\s+/)
if (a && !b) {
return readListLineFile(a, hook)
} else if (a && b) {
return {
...readListLineFile(b, hook),
task: a,
}
}
}
export function readListLineFile(
text: string,
hook?: HostLinkHook,
): LineListLink {
const list = text.replace(/[\(\)]/g, '').split(':')
const code = list.pop()
let codeMark = code ? parseInt(code, 10) : undefined
const line = list.pop()
let lineMark = line ? parseInt(line, 10) : undefined
let file = list.join(':')
if (
hook &&
typeof lineMark === 'number' &&
typeof codeMark === 'number'
) {
;[file, lineMark, codeMark] = hook(file, lineMark, codeMark)
}
return {
code: codeMark,
file,
line: lineMark,
}
}
export type HostLinkHook = (
file: string,
line: number,
rise: number,
) => [string, number | undefined, number | undefined]
function makeTextMove(move: number) {
return new Array(move + 1).join(' ')
}