@allurereport/web-commons 3.11.0 → 3.12.0

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/dist/strings.d.ts CHANGED
@@ -9,4 +9,6 @@ export interface AnsiToHtmlConfig {
9
9
  };
10
10
  }
11
11
  export declare const isAnsi: (text?: string) => boolean;
12
+ export declare const ansiSemanticColors: Record<number, string>;
13
+ export declare const normalizeAnsiForegroundColors: (text: string) => string;
12
14
  export declare const ansiToHTML: (text: string, config?: AnsiToHtmlConfig) => string;
package/dist/strings.js CHANGED
@@ -1,6 +1,140 @@
1
1
  import AnsiToHtml from "ansi-to-html";
2
2
  const ansiRegex = /\x1B\[[0-9;?]*[ -/]*[@-~]/g;
3
3
  export const isAnsi = (text) => typeof text === "string" && new RegExp(ansiRegex).test(text);
4
+ const semanticAnsiColor = {
5
+ primary: 256,
6
+ muted: 257,
7
+ danger: 258,
8
+ success: 259,
9
+ warning: 260,
10
+ info: 261,
11
+ unknown: 262,
12
+ decorative: 263,
13
+ };
14
+ export const ansiSemanticColors = {
15
+ 0: "var(--color-text-primary)",
16
+ 1: "var(--color-intent-danger-text)",
17
+ 2: "var(--color-intent-success-text)",
18
+ 3: "var(--color-intent-warning-text)",
19
+ 4: "var(--color-intent-info-text)",
20
+ 5: "var(--color-status-unknown-text)",
21
+ 6: "var(--color-decorative-5-text)",
22
+ 7: "var(--color-text-primary)",
23
+ 8: "var(--color-text-muted)",
24
+ 9: "var(--color-intent-danger-text)",
25
+ 10: "var(--color-intent-success-text)",
26
+ 11: "var(--color-intent-warning-text)",
27
+ 12: "var(--color-intent-info-text)",
28
+ 13: "var(--color-status-unknown-text)",
29
+ 14: "var(--color-decorative-5-text)",
30
+ 15: "var(--color-text-primary)",
31
+ [semanticAnsiColor.primary]: "var(--color-text-primary)",
32
+ [semanticAnsiColor.muted]: "var(--color-text-muted)",
33
+ [semanticAnsiColor.danger]: "var(--color-intent-danger-text)",
34
+ [semanticAnsiColor.success]: "var(--color-intent-success-text)",
35
+ [semanticAnsiColor.warning]: "var(--color-intent-warning-text)",
36
+ [semanticAnsiColor.info]: "var(--color-intent-info-text)",
37
+ [semanticAnsiColor.unknown]: "var(--color-status-unknown-text)",
38
+ [semanticAnsiColor.decorative]: "var(--color-decorative-5-text)",
39
+ };
40
+ const xtermColorLevels = [0, 95, 135, 175, 215, 255];
41
+ const getReadableAnsiColor = (red, green, blue) => {
42
+ const brightness = red * 0.299 + green * 0.587 + blue * 0.114;
43
+ const max = Math.max(red, green, blue);
44
+ const min = Math.min(red, green, blue);
45
+ if (red >= 150 && green >= 120 && blue <= 210 && red + green >= blue * 2.2) {
46
+ return semanticAnsiColor.warning;
47
+ }
48
+ if (green >= red + 20 && green >= blue + 20) {
49
+ return semanticAnsiColor.success;
50
+ }
51
+ if (red >= green + 30 && red >= blue + 30) {
52
+ return semanticAnsiColor.danger;
53
+ }
54
+ if (blue >= red + 20 && blue >= green - 10) {
55
+ return semanticAnsiColor.info;
56
+ }
57
+ if (red >= 120 && blue >= 120 && green <= max - 20) {
58
+ return semanticAnsiColor.unknown;
59
+ }
60
+ if (max - min <= 32) {
61
+ return brightness >= 150 || brightness <= 80 ? semanticAnsiColor.primary : semanticAnsiColor.muted;
62
+ }
63
+ return semanticAnsiColor.primary;
64
+ };
65
+ const getXtermColorRgb = (colorIndex) => {
66
+ if (colorIndex >= 16 && colorIndex <= 231) {
67
+ const cubeIndex = colorIndex - 16;
68
+ return {
69
+ red: xtermColorLevels[Math.floor(cubeIndex / 36)],
70
+ green: xtermColorLevels[Math.floor((cubeIndex % 36) / 6)],
71
+ blue: xtermColorLevels[cubeIndex % 6],
72
+ };
73
+ }
74
+ if (colorIndex >= 232 && colorIndex <= 255) {
75
+ const level = (colorIndex - 232) * 10 + 8;
76
+ return {
77
+ red: level,
78
+ green: level,
79
+ blue: level,
80
+ };
81
+ }
82
+ };
83
+ const isValidColorPart = (value) => Number.isInteger(value) && value >= 0 && value <= 255;
84
+ export const normalizeAnsiForegroundColors = (text) => text.replace(/\x1b\[([0-9;]*)m/g, (match, sequence) => {
85
+ const codes = sequence.length === 0 ? [0] : sequence.split(";").map(Number);
86
+ if (codes.some((code) => !Number.isInteger(code))) {
87
+ return match;
88
+ }
89
+ const fragments = [];
90
+ let displayCodes = [];
91
+ const flushDisplayCodes = () => {
92
+ if (displayCodes.length > 0) {
93
+ fragments.push(`\x1b[${displayCodes.join(";")}m`);
94
+ displayCodes = [];
95
+ }
96
+ };
97
+ for (let index = 0; index < codes.length;) {
98
+ const code = codes[index];
99
+ if (code === 38 && codes[index + 1] === 5 && isValidColorPart(codes[index + 2])) {
100
+ const rgb = getXtermColorRgb(codes[index + 2]);
101
+ flushDisplayCodes();
102
+ fragments.push(`\x1b[38;5;${rgb ? getReadableAnsiColor(rgb.red, rgb.green, rgb.blue) : semanticAnsiColor.primary}m`);
103
+ index += 3;
104
+ continue;
105
+ }
106
+ if (code === 38 &&
107
+ codes[index + 1] === 2 &&
108
+ isValidColorPart(codes[index + 2]) &&
109
+ isValidColorPart(codes[index + 3]) &&
110
+ isValidColorPart(codes[index + 4])) {
111
+ flushDisplayCodes();
112
+ fragments.push(`\x1b[38;5;${getReadableAnsiColor(codes[index + 2], codes[index + 3], codes[index + 4])}m`);
113
+ index += 5;
114
+ continue;
115
+ }
116
+ if (code === 48 && codes[index + 1] === 5 && isValidColorPart(codes[index + 2])) {
117
+ flushDisplayCodes();
118
+ fragments.push(`\x1b[48;5;${codes[index + 2]}m`);
119
+ index += 3;
120
+ continue;
121
+ }
122
+ if (code === 48 &&
123
+ codes[index + 1] === 2 &&
124
+ isValidColorPart(codes[index + 2]) &&
125
+ isValidColorPart(codes[index + 3]) &&
126
+ isValidColorPart(codes[index + 4])) {
127
+ flushDisplayCodes();
128
+ fragments.push(`\x1b[48;2;${codes[index + 2]};${codes[index + 3]};${codes[index + 4]}m`);
129
+ index += 5;
130
+ continue;
131
+ }
132
+ displayCodes.push(code);
133
+ index += 1;
134
+ }
135
+ flushDisplayCodes();
136
+ return fragments.join("");
137
+ });
4
138
  export const ansiToHTML = (text, config) => new AnsiToHtml({
5
139
  escapeXML: true,
6
140
  ...config,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@allurereport/web-commons",
3
- "version": "3.11.0",
3
+ "version": "3.12.0",
4
4
  "description": "Collection of utilities used across the web Allure reports",
5
5
  "keywords": [
6
6
  "allure",
@@ -26,10 +26,10 @@
26
26
  "lint:fix": "oxlint --import-plugin --fix src test features stories"
27
27
  },
28
28
  "dependencies": {
29
- "@allurereport/aql": "3.11.0",
30
- "@allurereport/charts-api": "3.11.0",
31
- "@allurereport/core-api": "3.11.0",
32
- "@allurereport/plugin-api": "3.11.0",
29
+ "@allurereport/aql": "3.12.0",
30
+ "@allurereport/charts-api": "3.12.0",
31
+ "@allurereport/core-api": "3.12.0",
32
+ "@allurereport/plugin-api": "3.12.0",
33
33
  "@preact/signals": "^2.6.1",
34
34
  "@preact/signals-core": "^1.12.2",
35
35
  "ansi-to-html": "^0.7.2",