const express = require('express');
const app = express();
const Rainbow = require('color-rainbow');
const escape = require('escape-html');
const style = require('ansi-styles');
app.use((req, res, next) => {
const { text } = req.query;
if (text && typeof text === 'string') {
req.chars = [...text];
req.colors = Rainbow.create(req.chars.length);
return next();
}
res.status(400).send('Please provide a string using the ?text query parameter.');
});
app.get('/', ({ chars, colors }, res) => {
const r = chars.map((c, i) => `<span style="color: ${colors[i].hexString()}">${escape(c)}</span>`);
res.type('text/html');
res.send(r.join(''));
});
app.get('/terminal', ({ chars, colors, query }, res) => {
const set = ['ansi', 'ansi256', 'ansi16m'].includes(query.colorset) ? query.colorset : 'ansi16m';
const r = chars.map((c, i) => style.color[set].hex(colors[i].hexString()) + c + style.color.close);
res.send(r.join(''));
});
app.listen(3000);