Previous
Next

Ubiquity command for trying out functions in the Ubiquity Command Editor

One thing I find lacking in the Ubiquity Command Editor is that in order to try out a function I've coded I can't just execute it directly. Let's say I've programmed a function called adder, like this:

 function adder(x, y) {
   return x + y;
 }
 

As far as I know, there's no direct way to test whether the function works as intended. So I made this Ubiquity command that I named 'evaly' that eval()s the parameter to the 'evaly' command and prints its result to the output pane.

So now I can fire up Ubiquity and write:

 evaly adder(10, 20) do
 

which outputs:

 The output of your function is: '30'
 

(The 'do' in the end is there so that the command doesn't try to eval () when I'm typing the command.)

Now, it's pretty limited and certainly has bugs, but I found it pleasant that it was this easy to make this sort of command that I find useful in my own development. Pretty cool.

Here's the command (just a ten minute sketch, but there you go):

 CmdUtils.CreateCommand({
   name: "evaly",
   description: "evaly: evaluates arbitrary text",
   help: "eval()s first parameter if last parameter is 'do'",
   takes: {"input": noun_arb_text},
   preview: function( pblock, input ) {
     var args = input.text.split(" ");
     if (args[args.length-1] == 'do') {
       var input_str = "";
       for (i = 0; i < args.length-1; i++) {
         input_str += args[i];
       }
       var s = "var x = " + input_str + ";";
       eval(s);
       pblock.innerHTML = "Output of your command: '" + x + "'";
     }
   },
   execute: function(input) { } 
 });