Skip to content
dc edited this page Oct 11, 2015 · 1 revision

Vorpal Mode

A Mode is an extension of the Command object, and is returned after vorpal.mode is used. A mode instance inherits all vorpal.command methods, with the following altered / additional methods:

API

vorpal.mode(command[, description])

Mode is a special type of vorpal.command that brings the user into a given mode, where-in regular Vorpal commands are ignored and the full command strings are interpreted literally by the mode.action method. This will continue until the user exits the mode by typing exit.

vorpal
  .mode('repl')
  .description('Enters the user into a REPL session.')
  .delimiter('repl:')
  .action(function(command, callback) {
    this.log(eval(command));
  });
$ node server.js
node~$ 
node~$ repl
node~$ repl: 
node~$ repl: 6 * 7
42
node~$ repl: Math.random();
0.62392647205
node~$ repl: exit
node~$ 

mode.delimiter(string)

Add on an additional delimiter string to one's Vorpal prompt upon entering the mode, so the user can differentiate what state he is in.

vorpal
  .mode('repl')
  .delimiter('you are in repl>')
  .action(function(command, callback) {
    this.log(eval(command));
  });
node~$ 
node~$ repl
node~$ you are in repl>  

mode.init(function)

Behaves exactly like command.action, where the function passed in is fired once when the user enters the given mode. Passed the same parameters as command.action: args and callback. init is helpful when one needs to set up the mode or inform the user of what is happening.

vorpal
  .mode('sql')
  .delimiter('sql:')
  .init(function(args, callback){
    this.log('Welcome to SQL mode.\nYou can now directly enter arbitrary SQL commands. To exit, type `exit`.');
    callback();
  })
  .action(function(command, callback) {
    var self = this;
    app.query(command, function(res){
      self.log(res);
      callback();
    });
  });
node~$
node~$ sql
Welcome to SQL mode.
You can now directly enter arbitrary SQL commands. To exit, type `exit`.
node~$ sql: 
node~$ sql: select first_name, last_name from persons where first_name = 'George';

first_name        last_name
----------------  ----------------
George            Clooney
George            Smith
George            Stevens

node~$ sql: 
node~$ sql: exit
node~$

mode.action(function)

Similar to command.action, mode.action differs in that it is repeatedly called on each command the user types until the mode is exited. Instead of args passed as the first argument, the full command string the user typed is passed and it is expected that mode.action appropriately handles the command. Example given in mode.init section.