Skip to content

Commit 4d30053

Browse files
committed
ANSI to React
1 parent afec60c commit 4d30053

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

index.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,35 @@
1-
console.log('soon');
1+
import React from 'react';
2+
3+
const ansiToJSON = require('ansi-to-json');
4+
5+
function ansiJSONtoStyleBundle(ansiBundle) {
6+
const style = {};
7+
if(ansiBundle.bg) {
8+
style.backgroundColor = `rgb(${ansiBundle.bg})`;
9+
}
10+
if(ansiBundle.fg) {
11+
style.color = `rgb(${ansiBundle.fg})`;
12+
}
13+
return {
14+
content: ansiBundle.content,
15+
style,
16+
};
17+
}
18+
19+
function ansiToInlineStyle(text) {
20+
return ansiToJSON(text).map(ansiJSONtoStyleBundle)
21+
}
22+
23+
function inlineBundleToReact(bundle, key) {
24+
return <span style={bundle.style} key={key}>{bundle.content}</span>
25+
}
26+
27+
export default function Ansi(props) {
28+
const { text } = props;
29+
return (
30+
<code>
31+
{ansiToInlineStyle(text).map(inlineBundleToReact)}
32+
</code>
33+
)
34+
}
35+

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
"author": "Kyle Kelley <[email protected]>",
1414
"license": "MPL-2.0",
1515
"dependencies": {
16-
"ansi-parser": "^3.2.0",
17-
"ansi-to-html": "^0.3.0",
18-
"entities": "^1.1.1"
16+
"ansi-to-json": "^1.0.0"
1917
}
2018
}

0 commit comments

Comments
 (0)