Terminal = (commands, options={}) => {
let {
w=width,
terminalStyle='',
commandPrefix='$ ',
shellPrefix=' > ',
outputPrefix='',
commandStyle='$ ',
shellStyle=' > ',
outputStyle='',
commandPrefixStyle='',
shellPrefixStyle='',
outputPrefixStyle='',
} = options;
console.log(commands)
if(Array.isArray(commands)) {
commands = commands.map(c=>{
if(typeof(c) === 'string')
return c.split('\n')
return c
}).flat()
}
else {
commands = commands.split('\n')
}
let randId = `terminal-id-${Math.floor(Math.random()*100)}-${Date.now()}`;
return html`
<style>
/* Add CSS here for ::before in prefix styling */
${commands.map( (c,i) => {
if (!c.prefix) return '';
return `#${randId}-${i}::before {
content: '${c.prefix}'!important;
}`}).join('\n')}
${'#'+randId} .command::before {
content: '${commandPrefix}';
color: #f1ff00;
${commandPrefixStyle}
}
${'#'+randId} .command {
${commandStyle}
}
${'#'+randId} .output {
/*Make the output non-copy-pasteable*/
-webkit-user-select: none; /* Safari */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* IE10+/Edge */
user-select: none; /* Standard */
${outputStyle}
}
${'#'+randId} .output::before {
content: '${outputPrefix}';
${outputPrefixStyle}
}
${'#'+randId} .shell {
${shellStyle}
}
${'#'+randId} .shell::before {
content: '${shellPrefix}';
${shellPrefixStyle}
}
${'#'+randId}.terminal{
font-family: Consolas, monaco, monospace;
background-color:#000;
color:#FFF;
border-radius:5px;
padding: 10px 15px;
width: ${w};
margin: 0 auto;
${terminalStyle}
}
</style>
<div id="${randId}" class=terminal>
<div data-clipboard-target="copy-command">
${commands.map( (c,i) => {
let type = 'command';
let line = c.line || c;
let style = c.style || '';
if(c.type)
type = c.type;
else {
let res = /!(command|output|shell).*$/g.exec(line)
if (res) {
type = res[1]
line = line.slice( type.length+1)
}
}
return `<span id="${randId}-${i}" class=${type} style="${style}">${line}</span><br/>`;
})}
</div>
</div>`
}