namastejs
Version:
A spiritual greeting from your JavaScript code. Because every function deserves a 'Namaste π'
86 lines (70 loc) β’ 2.24 kB
JavaScript
const readline = require("readline");
const { style } = require("../../../theme");
const WIDTH = 100;
const BODY_HEIGHT = 15;
const BODY_WIDTH = WIDTH - 2;
function line(char = "β") {
return char.repeat(WIDTH);
}
function pad(text = "") {
return text.padEnd(WIDTH - 1);
}
function initScreen() {
process.stdout.write("\x1Bc"); // clear screen
console.log(style.blue("β" + line() + "β"));
console.log(style.blue("β " + pad("") + "β"));
console.log(style.blue("β" + line() + "β€"));
for (let i = 0; i < BODY_HEIGHT; i++) {
console.log(style.blue("β " + pad("") + "β"));
}
console.log(style.blue("β" + line() + "β€"));
console.log(style.blue("β " + pad("") + "β"));
console.log(style.blue("β" + line() + "β"));
}
function updateHeader({ score, lives, dropped, isPaused }) {
readline.cursorTo(process.stdout, 15, 1);
readline.clearLine(process.stdout, 0);
process.stdout.write(
`${style.magenta("Score:")} ${score} β ${style.red("Lives:")} ${"β€".repeat(
lives
)} β ${style.lime("Dropped:")} ${dropped} | Press ${style.underline(
"Space"
)} for ${isPaused ? "βΆοΈ Resumed" : "βΈοΈ Paused"}`
);
}
function renderBody(words) {
const grid = Array.from({ length: BODY_HEIGHT }, () =>
Array(BODY_WIDTH).fill(" ")
);
words.forEach(({ text, x, y, color }) => {
if (y >= 0 && y < BODY_HEIGHT) {
for (let i = 0; i < text.length; i++) {
if (x + i < BODY_WIDTH) {
// grid[y][x + i] = text[i];
grid[y][x + i] = color(text[i]);
}
}
}
});
for (let row = 0; row < BODY_HEIGHT; row++) {
readline.cursorTo(process.stdout, 2, row + 3);
process.stdout.write(grid[row].join(""));
}
}
function renderInput(input) {
readline.cursorTo(process.stdout, 2, BODY_HEIGHT + 4);
readline.clearLine(process.stdout, 0);
process.stdout.write(`${style.green("Type here")} β ${input}`);
}
function parkCursor() {
readline.cursorTo(process.stdout, 2, BODY_HEIGHT + 4);
}
module.exports = {
initScreen,
updateHeader,
renderBody,
renderInput,
parkCursor,
BODY_HEIGHT,
BODY_WIDTH,
};