ANSIDec is a versatile library designed to handle ANSI escape sequences, enabling the display of ANSI and ASCII art in web browsers. By converting Unix encoding to HTML, it enhances the visual representation of terminal output. Usable in both browsers and Node.js, it offers a straightforward setup with npm and customizable formatting features.
ANSIDec is a lightweight JavaScript library designed to handle a limited set of ANSI escape sequences, making it possible to display ANSI and ASCII art in web browsers. This tool transforms Unix-encoded text into HTML, facilitating the rendering of colorful terminal-based content in modern web applications. Additionally, ANSIDec is compatible with Node.js, expanding its usability beyond browsers.
Features
- Browser Compatibility: Easily render ANSI text and ASCII art in browsers by converting Unix-style coding into HTML format.
- Node.js Support: Utilize the library within Node.js environments, broadening its application.
- Custom Formatting Options: Implement custom styles for text output, including colors, backgrounds, bold, italic, and underline using the
formatfunction.
Usage Example
To incorporate ANSIDec into a project, require the library and utilize the provided functions to format your text as needed:
var ansi = require('ansidec');
var format = ansi.format(function(styles, color, background, text) {
var style = [];
if (color) {
style.push('color:' + color);
}
if (background) {
style.push('background:' + background);
}
if (styles.bold) {
style.push('font-weight:bold');
}
if (styles.italic) {
style.push('font-style:italic');
}
if (styles.underline) {
styles.push('text-decoration:underline');
}
return '<span style="' + style.join(';') + '">'+ text + '</span>';
});
document.querySelector('pre').innerHTML = format(text);
Rendering ANSI Art
To render ANSI art, convert the text from the ANSI art file to UTF-8, and you can make use of the tool effectively. For handling additional metadata, such as SAUCE data, the ansi.meta function can be employed to extract information related to the artwork.
var sauce = ansi.meta(text);
if (sauce) {
var chars = sauce.tInfo[0];
output.style.width = chars + 'ch';
}
Demo
Explore a live demonstration of ANSIDec at this link.
Changelog
- 0.3.4 - Fixed GitHub URL in README for npm.
- 0.3.3 - Added parsing for zstring from SAUCE and improved unit tests.
- 0.3.2 - Enhanced SAUCE comments parsing.
- 0.3.1 - Alternative SAUCE extraction methods.
- 0.3.0 - Initial capabilities for parsing SAUCE metadata.
- 0.2.1 - Version bump for npm.
- 0.2.0 - Updated handling of 8-bit colors.
- 0.1.0 - First release of the library.
No comments yet.
Sign in to be the first to comment.