UNPKG

@hp4k1h5/terminordle

Version:

> multiplayer [wordle](https://www.powerlanguage.co.uk/wordle/) clone in your terminal

80 lines (79 loc) 2.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.infoIndex = exports.display = exports.MsgColors = exports.typesetAlphabet = exports.typesetGuess = void 0; const chalk = require("chalk"); const structs_1 = require("../lib/structs"); const util_1 = require("../util"); const optColorMap = { [structs_1.Visibility.hidden]: chalk.bgWhite, [structs_1.Visibility.guessed]: chalk.bgGray, [structs_1.Visibility.exists]: chalk.bgYellow, [structs_1.Visibility.revealed]: chalk.bgGreenBright, }; function typesetGuess(guess) { return guess .map((option) => { return optColorMap[option.visibility](option.letter); }) .join(''); } exports.typesetGuess = typesetGuess; function typesetAlphabet(reset = false) { if (reset) { Object.entries(util_1.letters).forEach(([letter]) => { util_1.letters[letter] = structs_1.Visibility.hidden; }); } return Object.entries(util_1.letters) .map(([k, v]) => { return optColorMap[v](k); }) .join(''); } exports.typesetAlphabet = typesetAlphabet; class Display { constructor() { this.screen = [ chalk.greenBright('Welcome to terminordle'), `>> ${' '.repeat(20)} <<`, typesetAlphabet(), ]; } addToGuesses(guess) { this.screen.splice(infoIndex() + 1, 1, typesetAlphabet()); this.screen.push(typesetGuess(guess)); // clear message this.alterMessage(); } clear(user_id, session_id) { // reset screen this.screen = [ chalk.greenBright(' terminordle'), `${chalk.blueBright('session id:')} ${session_id}`, `${chalk.cyanBright('user id: ')} ${user_id}`, `>> ${' '.repeat(20)} <<`, typesetAlphabet(true), ]; // clear info message this.alterMessage(); } alterMessage(message = '', color = MsgColors['redBright']) { const infoInd = infoIndex(); this.screen[infoInd] = `>> ${chalk[color](message)}${' '.repeat(Math.max(21 - message.length, 0))}<<`; } print() { console.clear(); process.stdout.write(this.screen.join('\n') + '\n'); } } var MsgColors; (function (MsgColors) { MsgColors["redBright"] = "redBright"; MsgColors["green"] = "green"; MsgColors["bold"] = "bold"; })(MsgColors = exports.MsgColors || (exports.MsgColors = {})); exports.display = new Display(); function infoIndex() { return exports.display.screen.findIndex(line => /^>>/.test(line)); } exports.infoIndex = infoIndex;