Skip to content

Instantly share code, notes, and snippets.

@rauschma
Last active June 5, 2025 17:23
Show Gist options
  • Save rauschma/da41775f52b664198bf904a1177ddc0c to your computer and use it in GitHub Desktop.
Save rauschma/da41775f52b664198bf904a1177ddc0c to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
import { watch } from 'node:fs/promises';
import * as fs from 'node:fs';
import * as path from 'node:path';
async function main() {
const args = process.argv.slice(2);
if (args.length === 0) {
const bin = path.basename(process.argv[1]);
console.log('Usage:');
console.log(`${bin} thread.txt`);
console.log();
console.log('• The text file contains posts, separated by lines with “---”.');
console.log('• This command displays the remaining characters for each post.');
return;
}
const fileName = args[0];
showThread(fileName);
const watcher = watch(fileName);
for await (const _event of watcher) {
showThread(fileName);
}
}
function showThread(fileName) {
const text = fs.readFileSync(fileName, 'utf-8');
console.clear();
const chunks = text.split(/^---$/m)
.map(ch => ch.trim());
for (const chunk of chunks) {
console.log(chunk);
console.log('--- ' + (500 - countGraphemes(chunk)))
}
}
function countGraphemes(text) {
const segmenter = new Intl.Segmenter('en-US', { granularity: 'grapheme' });
return Array.from(segmenter.segment(text)).length;
}
await main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment