UNPKG

imagelogger

Version:

Shows an image in console.

35 lines (34 loc) 1.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Image = void 0; const chalk_1 = require("chalk"); class Image { constructor(width, height) { this.width = width; this.height = height; } get buffer() { return this.p_buffer.slice().map(k => k.slice()); } set buffer(buf) { if (buf.length !== this.height) { throw new RangeError(`The buffer must have as many items (${buf.length}) as the height of the image (${this.height}).`); } if (buf.find(k => k.length !== this.width)) { throw new RangeError(`A line has a different amount of pixels (${buf.find(k => k.length !== this.width).length}) than the width (${this.width}).`); } this.p_buffer = buf.slice().map(k => k.slice()); } } exports.Image = Image; function render(img) { let s = ''; for (let row of img.buffer) { for (let pixel of row) { s += chalk_1.bgHex(`#${pixel.red.toString(16).padStart(2, '0')}${pixel.green.toString(16).padStart(2, '0')}${pixel.blue.toString(16).padStart(2, '0')}`)(' '); } s += `\n`; } process.stdout.write(s); } exports.default = render;