Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e114999

Browse files
authored
feat: support ansi colors in startup script logs (coder#7037)
This will help with beautiful output for devcontainers!
1 parent 2637f55 commit e114999

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

site/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@vitejs/plugin-react": "2.1.0",
4444
"@xstate/inspect": "0.6.5",
4545
"@xstate/react": "3.2.1",
46+
"ansi-to-html": "^0.7.2",
4647
"axios": "1.3.4",
4748
"canvas": "2.11.0",
4849
"chart.js": "3.9.1",

site/src/components/Logs/Logs.tsx

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { makeStyles, Theme } from "@material-ui/core/styles"
22
import { LogLevel } from "api/typesGenerated"
33
import dayjs from "dayjs"
4-
import { FC } from "react"
4+
import { FC, useMemo } from "react"
55
import { MONOSPACE_FONT_FAMILY } from "../../theme/constants"
66
import { combineClasses } from "../../util/combineClasses"
7+
import AnsiToHTML from "ansi-to-html"
78

89
export interface Line {
910
time: string
@@ -53,6 +54,8 @@ export const Logs: FC<React.PropsWithChildren<LogsProps>> = ({
5354

5455
export const logLineHeight = 20
5556

57+
const convert = new AnsiToHTML()
58+
5659
export const LogLine: FC<{
5760
line: Line
5861
hideTimestamp?: boolean
@@ -62,6 +65,9 @@ export const LogLine: FC<{
6265
const styles = useStyles({
6366
lineNumbers: Boolean(number),
6467
})
68+
const output = useMemo(() => {
69+
return convert.toHtml(line.output)
70+
}, [line.output])
6571

6672
return (
6773
<div className={combineClasses([styles.line, line.level])} style={style}>
@@ -73,7 +79,11 @@ export const LogLine: FC<{
7379
<span className={styles.space}>&nbsp;&nbsp;&nbsp;&nbsp;</span>
7480
</>
7581
)}
76-
<span>{line.output}</span>
82+
<span
83+
dangerouslySetInnerHTML={{
84+
__html: output,
85+
}}
86+
/>
7787
</div>
7888
)
7989
}

site/src/components/Resources/AgentRow.stories.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ Starting.args = {
109109
agent: MockWorkspaceAgentStarting,
110110

111111
storybookStartupLogs: [
112-
"Cloning Git repository...",
113-
"Starting Docker Daemon...",
114-
"Adding some 🧙magic🧙...",
112+
"\x1b[91mCloning Git repository...",
113+
"\x1b[2;37;41mStarting Docker Daemon...",
114+
"\x1b[1;95mAdding some 🧙magic🧙...",
115115
"Starting VS Code...",
116116
].map((line, index) => ({
117117
id: index,

site/yarn.lock

+8-1
Original file line numberDiff line numberDiff line change
@@ -4502,6 +4502,13 @@ ansi-to-html@^0.6.11:
45024502
dependencies:
45034503
entities "^2.0.0"
45044504

4505+
ansi-to-html@^0.7.2:
4506+
version "0.7.2"
4507+
resolved "https://registry.yarnpkg.com/ansi-to-html/-/ansi-to-html-0.7.2.tgz#a92c149e4184b571eb29a0135ca001a8e2d710cb"
4508+
integrity sha512-v6MqmEpNlxF+POuyhKkidusCHWWkaLcGRURzivcU3I9tv7k4JVhFcnukrM5Rlk2rUywdZuzYAZ+kbZqWCnfN3g==
4509+
dependencies:
4510+
entities "^2.2.0"
4511+
45054512
any-promise@^1.1.0, any-promise@~1.3.0:
45064513
version "1.3.0"
45074514
resolved "https://registry.yarnpkg.com/any-promise/-/any-promise-1.3.0.tgz#abc6afeedcea52e809cdc0376aed3ce39635d17f"
@@ -6654,7 +6661,7 @@ enhanced-resolve@^5.10.0:
66546661
graceful-fs "^4.2.4"
66556662
tapable "^2.2.0"
66566663

6657-
entities@^2.0.0:
6664+
entities@^2.0.0, entities@^2.2.0:
66586665
version "2.2.0"
66596666
resolved "https://registry.yarnpkg.com/entities/-/entities-2.2.0.tgz#098dc90ebb83d8dffa089d55256b351d34c4da55"
66606667
integrity sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A==

0 commit comments

Comments
 (0)