Published
Edited
Feb 12, 2019
2 forks
Importers
15 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
a = ['oneline', 'another oneline', `line #1
line#2,
line#3`, 'line#1a\nline#2a']
Insert cell
Insert cell
Terminal(`cat dog.txt
!outputbark bark`)
Insert cell
Insert cell
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) // if command obj was passed
type = c.type;
else {
let res = /!(command|output|shell).*$/g.exec(line)
if (res) { // used something like '!command cat example.txt'
type = res[1]
line = line.slice( type.length+1)
}
}
return `<span id="${randId}-${i}" class=${type} style="${style}">${line}</span><br/>`;
})}
</div>
</div>`
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more