-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·75 lines (66 loc) · 1.54 KB
/
index.js
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
const AnsiToHtml = require('ansi-to-html')
const escape = require('escape-html')
const lightTheme = {
newline: false,
bg: '#fff',
fg: '#111',
name: 'light',
}
const darkTheme = {
newline: false,
bg: '#000',
fg: '#eee',
name: 'dark',
}
const getHtmlStart = options => {
const start = `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style>
body {
font-family: "Courier New", Courier, monospace;
padding: 0 1em;
line-height: 1.4;
color: ${options.fg};
background-color: ${options.bg};
}
pre {
padding: 0 0;
margin: 0 0;
font-family: "Courier New", Courier, monospace;
}
</style>
</head>
<body><pre>\n
`
return start
}
const htmlEnd = '\n</pre></body></html>'
const streams = options => {
const convert = new AnsiToHtml(options)
const start = getHtmlStart(options)
console.log(start)
process.stdin.setEncoding('utf8')
process.stdin.on('data', function(chunk) {
return process.stdout.write(convert.toHtml(escape(chunk)))
})
process.stdin.on('end', () => {
console.log(htmlEnd)
})
}
const strings = (s, theme = 'dark') => {
const options = theme === 'dark' ? darkTheme : lightTheme
const htmlStart = getHtmlStart(options)
const convert = new AnsiToHtml(options)
const converted = convert.toHtml(escape(s))
return htmlStart.trim() + converted + htmlEnd
}
module.exports = {
streams,
strings,
themes: {
light: lightTheme,
dark: darkTheme,
},
}