Skip to content

Commit 6d6ec97

Browse files
authored
Merge pull request #143 from isucon/feat/colorize-stdout
feat: stdoutの表示に色を付ける
2 parents 9ae0de2 + bfe31a2 commit 6d6ec97

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

portal/app/javascript/BenchmarkJobDetail.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ const renderJobExecution = (job: BenchmarkJob, admin: boolean) => {
142142
<div className="mt-3">
143143
<h5 className="subtitle is-5">Stdout</h5>
144144
<pre>{execution.stdout}</pre>
145+
<StdoutWithColor stdout={execution.stdout} />
145146

146147
{admin ? (
147148
<>
@@ -168,3 +169,41 @@ export const BenchmarkJobDetail: React.FC<Props> = (props: Props) => {
168169
</>
169170
);
170171
};
172+
173+
const StdoutWithColor = ({ stdout }: { stdout: string }) => {
174+
const lines = stdout.trim().split("\n");
175+
return (
176+
<pre>
177+
{lines.map((line, idx) => (
178+
<StdoutLineWithColor line={line} key={idx} />
179+
))}
180+
</pre>
181+
);
182+
};
183+
184+
const levelToColor = {
185+
info: "",
186+
warn: "has-text-warning",
187+
error: "has-text-danger",
188+
};
189+
const stdoutLineRE = /^\s*(time=[\d:.]+\s+level=(INFO|WARN|ERROR))(\s+msg=.*)$/;
190+
const StdoutLineWithColor = ({ line }: { line: string }) => {
191+
const match = line.match(stdoutLineRE);
192+
if (!match)
193+
return (
194+
<span>
195+
{line}
196+
{"\n"}
197+
</span>
198+
);
199+
200+
const [, prefix, level, suffix] = match;
201+
const lev = level.toLowerCase() as "info" | "warn" | "error";
202+
return (
203+
<>
204+
<span className="has-text-grey">{prefix}</span>
205+
<span className={levelToColor[lev]}>{suffix}</span>
206+
{"\n"}
207+
</>
208+
);
209+
};

0 commit comments

Comments
 (0)