Skip to content

Instantly share code, notes, and snippets.

@gergob
Created July 18, 2013 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gergob/6033253 to your computer and use it in GitHub Desktop.
Save gergob/6033253 to your computer and use it in GitHub Desktop.
Contains the implementation of linux's yes command.
#!/usr/bin/env node
/*
This small node.js app should do exactly what the yes linux command does.
Quote from man yes:
Repeatedly output a line with all specified STRING(s), or `y'.
*/
var DEFAULT_TEXT = "y\n";
var CUSTOM_TEXT = "";
var printUsage = function() {
process.stdout.write("this will contain the details how to use the program\n");
};
var writeDefaultText = function() {
process.stdout.write(DEFAULT_TEXT);
};
var writerCustomText = function() {
process.stdout.write(CUSTOM_TEXT);
};
//handler for CTRL + C
process.on("SIGINT", function() {
clearInterval(writeDefaultText);
clearInterval(writerCustomText);
process.exit(0);
});
if(process.argv.length == 2) {
setInterval(writeDefaultText, 2);
}
else if(process.argv.length == 3) {
CUSTOM_TEXT = process.argv[2];
if(CUSTOM_TEXT[CUSTOM_TEXT.length - 1] != "\n") {
CUSTOM_TEXT += "\n";
}
setInterval(writerCustomText, 2);
}
else {
printUsage();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment