forked from arduino/arduino-ide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode-output.tsx
150 lines (133 loc) · 3.93 KB
/
decode-output.tsx
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
import * as React from '@theia/core/shared/react';
import { Event } from '@theia/core/lib/common/event';
import { DisposableCollection } from '@theia/core/lib/common/disposable';
import { spawnCommand } from '../../../node/exec-util';
export type Line = { message: string; lineLen: number };
export type Element = {
address: string;
function: string;
path: {
value: string;
isLink: boolean;
}
lineNumber: string
};
export class DecodeOutput extends React.Component<
DecodeOutput.Props,
DecodeOutput.State
> {
/**
* Do not touch it. It is used to be able to "follow" the serial monitor log.
*/
protected toDisposeBeforeUnmount = new DisposableCollection();
constructor(props: Readonly<DecodeOutput.Props>) {
super(props);
this.state = {
elements: [],
};
}
// If a string of <digit>.<digit>.<digit> or <digit>.<digit> is found, replaces it with "*"
changeVersionToAny = (path: string) => {
const regex = new RegExp(/(\d\.\d\.\d)|(\d\.\d)/g);
const found = path.match(regex);
if(found) {
return path.replace(found[0], "*")
}
return path
}
isClientPath = async (path:string): Promise<boolean> => {
return await spawnCommand("cd", [
path
], (err) => err)
.then((data) => true)
.catch(err => false)
}
openFinder = async (path:string) => {
await spawnCommand("open", [
path
]);
}
retrievePath = (dirPath:string) => {
return dirPath.substring(0,dirPath.lastIndexOf("/")+1);
}
decodeText = async (value: string) => {
const lines = value.split("\n");
// Remove the extra newline at the end
lines.pop();
const elements : Array<Element> = [];
for(let i=0;i<lines.length;i++) {
let line = lines[i].split(/(?!\(.*)\s(?![^(]*?\))/g);
if(line[0] === "") {
line.shift();
}
let pathSplit = line[3].split(":");
pathSplit[0] = this.changeVersionToAny(pathSplit[0]);
let obj: Element = {
address: line[0],
function: line[1],
path: {
value: pathSplit[0],
isLink: false,
},
lineNumber: pathSplit[1]
};
if(await this.isClientPath(this.retrievePath(pathSplit[0]))) {
obj = {
address: line[0],
function: line[1],
path: {
value: pathSplit[0],
isLink: true,
},
lineNumber: pathSplit[1]
};
}
elements.push(obj);
}
this.setState({ elements });
};
override render(): React.ReactNode {
return (
<div style={{ whiteSpace: 'pre-wrap' }}>
{this.state.elements.map((element) => (
<div style={{display: "inline-block"}}>
<span style={{color: "green"}}>{element.address} </span>
<span style={{color: "blue", fontWeight: "bold"}}>{element.function} </span>
at
{ element.path.isLink ? <a><span onClick={async () => await this.openFinder(this.retrievePath(element.path.value))}>{element.path.value}</span></a> : <span> {element.path.value} </span> }
line
<span style={{fontWeight: "bold"}}> {element.lineNumber}</span>
</div>
))}
</div>
);
}
override shouldComponentUpdate(): boolean {
return true;
}
override componentDidMount(): void {
this.toDisposeBeforeUnmount.pushAll([
this.props.clearConsoleEvent(() =>
this.setState({ elements: [] })
),
]);
}
override componentWillUnmount(): void {
// TODO: "Your preferred browser's local storage is almost full." Discard `content` before saving layout?
this.toDisposeBeforeUnmount.dispose();
}
}
export namespace DecodeOutput {
export interface Props {
readonly clearConsoleEvent: Event<void>;
readonly height: number;
}
export interface State {
elements: Element[];
}
export interface SelectOption<T> {
readonly label: string;
readonly value: T;
}
export const MAX_CHARACTERS = 1_000_000;
}