sofancy
#!/usr/bin/env node
const fs = require('fs').promises;
const path = require('path');
const parseArgs = require('minimist-lite');
let argv = require('minimist-lite')(process.argv.slice(2));
let charmap,find,font,str,title;
if (argv.font) {
font=argv.font;
} else if (argv.f) {
font=argv.f;
} else {
delete font
}
find=(font == undefined) ? /.*json$/ : `${font}.json`;
str=argv._.toString().replace(',', ' ');
if (str == '') {
usage();
}
function usage() {
console.log(`usage: ./sofancy.js [-f (font) | -t] string
flags:
-f (font) : output in a single font
-t : display titles in output
examples:
/sofancy.js -f wide aesthetics
/sofancy.js -t some string | fzf | xsel -i
`)
process.exit();
}
const walk = async (dir, filelist = []) => {
const files = await fs.readdir(dir);
for (file of files) {
const filepath = path.join(dir, file);
const stat = await fs.stat(filepath);
if (stat.isDirectory()) {
filelist = await walk(filepath, filelist);
} else {
if(file.match(find)) {
filelist.push(file);
charmap = require(__dirname+`/lib/${file}`);
title=(argv.t) ? file.replace(".json", "").padEnd(15) : '';
console.log(
title+
str.replace(/./g, function (s) {
if(s in charmap) {
return charmap[s]
} else if (s.toLowerCase() in charmap) {
return charmap[s.toLowerCase()]
} else if (s.toUpperCase() in charmap) {
return charmap[s.toUpperCase()]
} else {
return s
}
})
)
}
}
}
}
walk(__dirname+'/lib');