-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
ConvertAnsi.ts
73 lines (67 loc) · 1.98 KB
/
ConvertAnsi.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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import ansiRegex = require('ansi-regex');
import style = require('ansi-styles');
import type {Config, NewPlugin, Printer, Refs} from '../types';
const toHumanReadableAnsi = (text: string) =>
text.replace(ansiRegex(), match => {
switch (match) {
case style.red.close:
case style.green.close:
case style.cyan.close:
case style.gray.close:
case style.white.close:
case style.yellow.close:
case style.bgRed.close:
case style.bgGreen.close:
case style.bgYellow.close:
case style.inverse.close:
case style.dim.close:
case style.bold.close:
case style.reset.open:
case style.reset.close:
return '</>';
case style.red.open:
return '<red>';
case style.green.open:
return '<green>';
case style.cyan.open:
return '<cyan>';
case style.gray.open:
return '<gray>';
case style.white.open:
return '<white>';
case style.yellow.open:
return '<yellow>';
case style.bgRed.open:
return '<bgRed>';
case style.bgGreen.open:
return '<bgGreen>';
case style.bgYellow.open:
return '<bgYellow>';
case style.inverse.open:
return '<inverse>';
case style.dim.open:
return '<dim>';
case style.bold.open:
return '<bold>';
default:
return '';
}
});
export const test: NewPlugin['test'] = (val: unknown) =>
typeof val === 'string' && !!val.match(ansiRegex());
export const serialize: NewPlugin['serialize'] = (
val: string,
config: Config,
indentation: string,
depth: number,
refs: Refs,
printer: Printer,
) => printer(toHumanReadableAnsi(val), config, indentation, depth, refs);
const plugin: NewPlugin = {serialize, test};
export default plugin;