Last active
August 26, 2020 17:44
-
-
Save gprasanth/9894302ff2f48ada29454a801134c8e7 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
#!/usr/bin/env node | |
// https://gist.github.com/ishu3101/9fa58ca1440f780d6288#file-read_arguments-js | |
var args = process.argv.slice(2); | |
var input = args[0]; | |
var isTTY = process.stdin.isTTY; | |
var stdin = process.stdin; | |
var stdout = process.stdout; | |
// If no STDIN and no arguments, display usage message | |
if (isTTY && args.length === 0) { | |
console.log("Usage: "); | |
} | |
// If no STDIN but arguments given | |
else if (isTTY && args.length !== 0) { | |
handleShellArguments(); | |
} | |
// read from STDIN | |
else { | |
handlePipedContent(); | |
} | |
function handlePipedContent() { | |
var data = ""; | |
stdin.on("readable", function () { | |
var chuck = stdin.read(); | |
if (chuck !== null) { | |
data += chuck; | |
} | |
}); | |
stdin.on("end", function () { | |
stdout.write(cowSay(data)); | |
}); | |
} | |
function handleShellArguments() { | |
console.log(cowSay(input)); | |
} | |
/** | |
* cowsay.js | |
*/ | |
function cowSay(data) { | |
const cow = ` | |
\\ ^__^ | |
\\ (oo)\\_______ | |
(__)\\ )\\/\\ | |
||----w | | |
|| ||`.replace(/^\n/g, ""); | |
const lines = []; | |
for (let i = 0, line = ""; i < data.length; i++) { | |
line += data[i]; | |
if (line.length === 39 || i === data.length - 1) { | |
while (lines.length > 0 && line.length < 39) line += " "; | |
lines.push(line); | |
line = ""; | |
} | |
} | |
if (!lines.length) return ""; | |
const dashCount = lines[0].length + 2; | |
const header = ` ${`_`.repeat(dashCount)} \n`; | |
const footer = ` ${`-`.repeat(dashCount)} \n`; | |
let message = ""; | |
if (lines.length === 1) message = `< ${data} >\n`; | |
else { | |
let firstLine = `/ ${lines.shift()} \\\n`; | |
let lastLine = `\\ ${lines.pop()} /\n`; | |
let betweens = lines.map((l) => `| ${l} |\n`).join(""); | |
message = firstLine + betweens + lastLine; | |
} | |
return header + message + footer + cow; | |
} | |
/* | |
@gprasanth92 | |
_____________________ | |
< #100DaysOfCode #Fun > | |
--------------------- | |
\ ^__^ | |
\ (oo)\_______ | |
(__)\ )\/\ | |
||----w | | |
|| || | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment