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.
format function.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);
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';
}
Explore a live demonstration of ANSIDec at this link.
No comments yet.
Sign in to be the first to comment.