const fs = require('fs');
const readline = require('readline');
/**
* Pretty-prints the contents of a text file with retry logic.
* @param {string} filePath The path to the text file.
* @param {number} maxRetries The maximum number of retry attempts.
*/
async function prettyPrintFile(filePath, maxRetries = 3) {
let retries = 0;
while (retries < maxRetries) {
try {
const fileContent = await fs.promises.readFile(filePath, 'utf8');
// Use a simple regex for basic pretty-printing (line wrapping)
const prettyText = fileContent.replace(/\n(?!\s)/g, '\n'); // Remove trailing newlines
console.log(prettyText);
return; // Exit the function if successful
} catch (error) {
retries++;
console.error(`Error reading file (${filePath}): ${error.message}. Retry ${retries}/${maxRetries}...`);
if (retries === maxRetries) {
console.error(`Failed to read file (${filePath}) after ${maxRetries} retries.`);
return; // Exit after max retries
}
// Optionally add a delay between retries
await new Promise(resolve => setTimeout(resolve, 1000)); // Wait 1 second
}
}
}
//Example usage (replace 'your_file.txt' with the actual file path)
//prettyPrintFile('your_file.txt');
Add your comment