Skip to content

Instantly share code, notes, and snippets.

@justinvh
Created December 16, 2011 05:50
Show Gist options
  • Save justinvh/1484679 to your computer and use it in GitHub Desktop.
Save justinvh/1484679 to your computer and use it in GitHub Desktop.
Taking advantage of function->string
function files(where, opts) {
/***
Generates a list of files from the output of `ls`.
Arguments:
where -- From where to list files (default '.')
opts -- Optional parameters that can be passed to `ls`.
***/
where = where || ".";
opts = opts || [];
calling = opts.length ? String.format("ls %s %s", opts.join(""), where) :
String.format("ls %s", where);
return s8.cmd(calling).split("\n");
}
function help(fun) {
/***
Prints the help text of a function.
Arguments:
fun -- The function (or alternatively an object with help defined)
***/
if (fun === undefined)
return stdout.writeln("What function do you need help with?");
if (!(fun instanceof Function)) {
if (fun.help)
return stdout.writeln(fun.help);
return stdout.writeln("I can not help you with that topic.");
}
var sf = new String(fun);
var matches = sf.match(/\/\*\*\*([\s\S]*)\*\*\*\//m);
if (matches)
return stdout.writeln(matches[1]);
return stdout.writeln("I can not help you with that function.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment