Skip to content

Commit b781fe3

Browse files
committed
Add borrowed parser and update style interfaces
1 parent e5837bc commit b781fe3

File tree

3 files changed

+860
-49
lines changed

3 files changed

+860
-49
lines changed

src/logs/model.ts

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const ansiColorRE = /\u001b\[((?:\d+;?)+)m(.*)\u001b\[0m/gmu;
21
const groupMarker = '##[group]';
3-
const commandRE = /##\[[a-z]+\]/gm;
2+
3+
import { Parser, IStyle } from './parser'
44

55
export enum Type {
66
Setup,
@@ -14,23 +14,16 @@ export interface LogSection {
1414
name?: string;
1515
}
1616

17-
export interface LogColorInfo {
17+
export interface LogStyleInfo {
1818
line: number;
19-
start: number;
20-
end: number;
21-
22-
color: CustomColor;
23-
}
24-
25-
export interface CustomColor {
26-
foreground?: string;
27-
background?: string;
19+
content: string;
20+
style?: IStyle;
2821
}
2922

3023
export interface LogInfo {
31-
updatedLog: string;
24+
updatedLogLines: string[];
3225
sections: LogSection[];
33-
colorFormats: LogColorInfo[];
26+
styleFormats: LogStyleInfo[];
3427
}
3528

3629
export function parseLog(log: string): LogInfo {
@@ -44,10 +37,12 @@ export function parseLog(log: string): LogInfo {
4437
// Assume there is always the setup section
4538
const sections: LogSection[] = [firstSection];
4639

47-
const colorInfo: LogColorInfo[] = [];
48-
4940
let currentRange: LogSection | null = null;
41+
42+
const parser = new Parser()
43+
const styleInfo: LogStyleInfo[] = [];
5044
const lines = log.split(/\n|\r/).filter(l => !!l);
45+
5146
let lineIdx = 0;
5247

5348
for (const line of lines) {
@@ -75,24 +70,13 @@ export function parseLog(log: string): LogInfo {
7570
};
7671
}
7772

78-
// Remove commands
79-
lines[lineIdx] = line.replace(commandRE, "");
80-
81-
// Check for custom colors
82-
let match: RegExpExecArray | null;
83-
if ((match = ansiColorRE.exec(line))) {
84-
const colorConfig = match[1];
85-
const text = match[2];
86-
87-
colorInfo.push({
73+
const stateFragments = parser.getStates(line)
74+
for (const state of stateFragments) {
75+
styleInfo.push({
8876
line: lineIdx,
89-
color: parseCustomColor(colorConfig),
90-
start: match.index,
91-
end: match.index + text.length
77+
content: state.output,
78+
style: state.style
9279
});
93-
94-
// Remove from output
95-
lines[lineIdx] = line.replace(ansiColorRE, text);
9680
}
9781

9882
++lineIdx;
@@ -104,23 +88,9 @@ export function parseLog(log: string): LogInfo {
10488
}
10589

10690
return {
107-
updatedLog: lines.join("\n"),
108-
sections,
109-
colorFormats: colorInfo
91+
updatedLogLines: lines,
92+
sections: sections,
93+
styleFormats: styleInfo
11094
};
11195
}
11296

113-
function parseCustomColor(str: string): CustomColor {
114-
const ret: CustomColor = {};
115-
116-
const segments = str.split(";");
117-
if (segments.length > 0) {
118-
ret.foreground = segments[0];
119-
}
120-
121-
if (segments.length > 1) {
122-
ret.background = segments[1];
123-
}
124-
125-
return ret;
126-
}

0 commit comments

Comments
 (0)