Created
September 24, 2024 10:40
-
-
Save denysdovhan/1ad66f4219fd1ccd4d6085f58081ca24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const readline = require('readline'); | |
const REFRESH_INTERVAL = 50; | |
// Function to clear the terminal | |
function clearTerminal() { | |
console.clear(); | |
} | |
// Function to generate random characters | |
function getRandomCharacter() { | |
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
return characters.charAt(Math.floor(Math.random() * characters.length)); | |
} | |
// Function to generate a random color using ANSI escape codes | |
function getRandomColor() { | |
// ANSI escape codes for foreground colors (30–37 are standard colors) | |
const colorCodes = [ | |
'\x1b[31m', // Red | |
'\x1b[32m', // Green | |
'\x1b[33m', // Yellow | |
'\x1b[34m', // Blue | |
'\x1b[35m', // Magenta | |
'\x1b[36m', // Cyan | |
'\x1b[37m', // White | |
]; | |
return colorCodes[Math.floor(Math.random() * colorCodes.length)]; | |
} | |
// Function to reset the color to default | |
function resetColor() { | |
return '\x1b[0m'; // ANSI escape code to reset color | |
} | |
// Function to generate a box frame with random colored characters inside | |
function generateBoxFrame(width, height) { | |
const horizontalBorder = '─'; | |
const verticalBorder = '│'; | |
const topLeftCorner = '┌'; | |
const topRightCorner = '┐'; | |
const bottomLeftCorner = '└'; | |
const bottomRightCorner = '┘'; | |
let frame = ''; | |
// Top border | |
frame += topLeftCorner + horizontalBorder.repeat(width - 2) + topRightCorner + '\n'; | |
// Middle rows with random colored characters | |
for (let i = 0; i < height - 2; i++) { | |
let row = verticalBorder; // Left border | |
for (let j = 0; j < width - 2; j++) { | |
const randomChar = getRandomCharacter(); | |
const coloredChar = getRandomColor() + randomChar + resetColor(); // Apply random color and reset | |
row += coloredChar; | |
} | |
row += verticalBorder + '\n'; // Right border | |
frame += row; | |
} | |
// Bottom border | |
frame += bottomLeftCorner + horizontalBorder.repeat(width - 2) + bottomRightCorner + '\n'; | |
return frame; | |
} | |
// Function to continuously refresh the terminal with the box frame | |
function startRefreshingScreen() { | |
setInterval(() => { | |
const { columns: width, rows: height } = process.stdout; | |
// Clear the terminal | |
clearTerminal(); | |
// Generate and print the box frame with random colored characters | |
const frame = generateBoxFrame(width, height - 1); // -1 to fit the terminal | |
process.stdout.write(frame); | |
}, REFRESH_INTERVAL); // Refresh interval | |
} | |
// Start the rendering loop | |
startRefreshingScreen(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment