Skip to content

Latest commit

History

45 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

shell-color

Javascript parse ANSI escape code and render to HTML, for CommonJS package

style

Install

npm install --save shell-color

Constructor

Common parameters

constShellColor=require('shell-color')varsc=newShellColor({colorMap: {// optionalwhite: '#cccccc',black: '#333333'},defaultBackgroundColor: 'white',// optionaldefaultForegroundColor: 'black',// optionalsnippetTag: 'span'// optional})

Use Worker

varsc=newShellColor({useWorker: true,worker: {path: 'node_modules/shell-color/dest/worker.js',callback: function(){ ... }}})

Usage

sc.on('reset',function(){console.log('reset')})sc.on('lineStart',function(){varstartInfo=document.createElement('b')startInfo.innerText='start:'document.body.appendChild(startInfo)})sc.on('snippet',function(tag){document.body.appendChild(tag)})sc.on('lineEnd',function(){varbr=document.createElement('br')document.body.appendChild(br)})sc.reset().write('\x1b[30m black\n\x1b[m\x1b[31m red\nend')

sc.reset()

Create a new stream, before write you must call reset at least once.

sc.write(text)

Push a text to current stream

You can not keep \n in final output, because tag.innerText convert \n to <br>, for example:

varspan=document.createElement('span')span.innerText='123\n\n456'assert.equal(span.innerHTML,'123<br><br>567')

sc.on()

sc.on('lineStart',function(){ ... })sc.on('snippet',function(tag){ ... })sc.on('lineEnd',function(){ ... })

ShellColor.strip(text)

vartext='\x1b[30m black \x1b[m\x1b[31m red'varstr=sc.strip(text)// ' black red'

ShellColor.toBlockTags(text)

vartags=ShellColor.toBlockTags(text)tags.forEach(function(tag){document.body.appendChild(tag)// <p>...</p>})

ShellColor.toInlineTags(text)

vartags=ShellColor.toInlineTags(text)tags.forEach(function(tag){document.body.appendChild(tag)// <span>...</span> or <br>})

ChangeLog

  • 1.1.0 Add web worker support
  • 2.0.0 Build change to webpack

Reference

About

Javascript to parse ANSI escape code and render to HTML

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages