UNPKG

@darkobits/lolcatjs

Version:

Fork of https://github.com/robertmarsal/lolcatjs.

249 lines (248 loc) 6.94 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); import { EOL } from "os"; import readlienz from "readline"; import byeByeMeow from "@darkobits/adeiu"; import sleep from "@darkobits/sleep"; import newCursorPlz from "ansi"; import CanHazTurn from "async-lock"; import { Chalk } from "chalk"; import urFilez from "fs-extra"; import izGudArg from "ow"; import pEechSeriez from "p-each-series"; import forTehStreamToBecomeFinished from "p-stream"; import byLinesPlz from "split2"; import noColorzPlz from "strip-color"; import iCanHazThruStream from "through"; import yargz from "yargs"; import { TEH_DEFAULT_OPSHUNZ, none } from "../etc/constantz.js"; import { randyNumPlz, makeItRainbow } from "./random-tingz.js"; class MakinUrText { constructor({ seed, freq, spread, animate, speed, duration, force } = {}) { /** * It's not nice to tamper with globals so we make a speshul chalk just for * us. */ __publicField(this, "_muhChalk"); /** * Cursors for teh streams we get piped to. */ __publicField(this, "_cursorz"); /** * Rainbow frequency. */ __publicField(this, "_freq"); /** * Rainbow spread. */ __publicField(this, "_spread"); /** * Animate ur linez? */ __publicField(this, "_animate"); /** * Makes animation go faster. */ __publicField(this, "_speed"); /** * How much duration for animating. */ __publicField(this, "_duration"); /** * So we only anime 1 of ur linez at a tiem. */ __publicField(this, "_wait4Turn"); /** * Rainbow seed. */ __publicField(this, "_seed"); /** * Keeps all the loltext we make. */ __publicField(this, "_loltext"); /** * Duplex stream to which colorized output is written. This stream may also be * piped into another writable stream, such as process.stdout. */ __publicField(this, "stream"); this._loltext = ""; this._cursorz = []; this._wait4Turn = new CanHazTurn(); this.stream = this.makeStreamPlz(); if (seed) { izGudArg(seed, "seed", izGudArg.number.positive); this._seed = seed; } else { this._seed = randyNumPlz(none, 256); } if (freq) { izGudArg(freq, "freq", izGudArg.number); this._freq = freq; } else { this._freq = TEH_DEFAULT_OPSHUNZ.freq; } if (spread) { izGudArg(spread, "spread", izGudArg.number); this._spread = spread; } else { this._spread = TEH_DEFAULT_OPSHUNZ.spread; } this._animate = Boolean(animate); if (this._animate) { byeByeMeow(() => { this.yesCursor(); process.exit(none); }); } if (speed) { izGudArg(speed, "speed", izGudArg.number); this._speed = speed; } else { this._speed = TEH_DEFAULT_OPSHUNZ.speed; } if (duration) { izGudArg(duration, "duration", izGudArg.number); this._duration = duration; } else { this._duration = TEH_DEFAULT_OPSHUNZ.duration; } this._muhChalk = new Chalk(force ? { level: 2 } : void 0); } /** * Makes an strem for us. */ makeStreamPlz() { function writeDis(data) { this.emit("data", data); } const pipe = (wrappedPipe) => { return (destStream, opts) => { this._cursorz.push(newCursorPlz(destStream)); return wrappedPipe(destStream, opts); }; }; const anStream = iCanHazThruStream(writeDis); anStream.pipe = pipe(anStream.pipe.bind(anStream)); return anStream; } /** * Brings ur cursor back 4 u. */ yesCursor() { this._cursorz.forEach((cursor) => { cursor.show(); }); } /** * Hides ur cursor 4 u. */ noCursor() { this._cursorz.forEach((cursor) => { cursor.hide(); }); } /** * Turns text into -----> !loltext!. */ colorizeUrLine(line) { return [...noColorzPlz(line)].map((char, index) => { const { red, green, blue } = makeItRainbow(this._freq, this._seed + index / this._spread); return this._muhChalk.rgb(red, green, blue)(char); }).join(""); } /** * Makes ur linez animated. FTW! */ async animateUrLine(urLine) { return this._wait4Turn.acquire("meow", async () => { const seed = this._seed; for (let i = 1; i < this._duration; i += 1) { this.noCursor(); readlienz.cursorTo(this.stream, none); this._seed += this._spread; if (i % 2 === none) { const urColorizedLine = this.colorizeUrLine(urLine.substr(none, yargz().terminalWidth())); this.stream.write(urColorizedLine); } await sleep(1 / this._speed * (none + 1e3)); } this._seed = seed; this.yesCursor(); }); } /** * Makes 1 line of loltext 4 u. * * If ur wantin animated text can does that too. */ async makeLine(line) { this._seed += 0.33; if (this._animate) { await this.animateUrLine(line); } const colorizedLine = `${this.colorizeUrLine(line)} `; this._loltext += colorizedLine; this.stream.write(colorizedLine); } /** * Lets u lol ur strings. */ fromString(urString = "") { urString.split("\n").forEach(async (urLien) => { await this.makeLine(urLien); }); } /** * Lets u lol ur streams. */ async fromStream(urStream) { return new Promise(async (keep, noKeep) => { const urStreamWasTakingTooLong = setTimeout(() => { noKeep(new Error("Stream timeout; did not receive any data.")); }, 250); urStream.resume(); urStream.setEncoding("utf8"); urStream.pipe(byLinesPlz()).on("data", async (tehLine) => { clearTimeout(urStreamWasTakingTooLong); await this.makeLine(tehLine); }); await forTehStreamToBecomeFinished(urStream); keep(); }); } /** * Lets u lol ur filez. */ async fromFile(urFile) { const urLinez = (await urFilez.readFile(urFile, "utf8")).split(EOL); await pEechSeriez(urLinez, async (disLine) => this.makeLine(disLine)); } /** * Lets u use ur loltext. */ toString() { return this._loltext.trim(); } /** * 4 when u wants 2 jus lulz a string orly quick. */ static fromString(urString, urOpshunz) { const urLoltext = new MakinUrText(urOpshunz); urString.split(EOL).forEach((line) => { urLoltext._seed += 0.33; const colorizedLine = `${urLoltext.colorizeUrLine(line)} `; urLoltext._loltext += colorizedLine; urLoltext.stream.write(colorizedLine); }); return urLoltext.toString(); } } export { MakinUrText as default }; //# sourceMappingURL=makin-ur-text.js.map