Thursday, November 6, 2014

Pigshell pipe - speak no evil!

In my last post I showed how to write a hello world program that displays an image in pigshell. However one of the most powerful things in shell scripting is the pipe and if you tried piping with the last program you will have seen:



For better or worse, only the last program in the pipeline has access to the screen. At least, in that way. So my next step is to make a program that talks pipe. Here goes:

In pigshell/src/cmd/jline.sh I put:

function Jline(opts) {
    var self = this;
    self.countdown = 10;
    Jline.base.call(self, opts);
}
inherit(Jline, Command);

Jline.prototype.usage =
    [ 'jline'
    , 'Usage:'
    , '  jline'
    ].join("\n");

Jline.prototype.next = check_next(do_docopt(function(opts, cb) {
    if (this.countdown--) this.output(this.countdown + " green bottles\n");
    else this.exit(0);
}));

Command.register("jline", Jline);


Run make and hey presto:




















No squealing!  Great, but a bit black and white.  Let's see whether we can add colour back in - after dinner!


No comments:

Post a Comment