tuiHoneyPot

front and back end of my TUI honeypot
Log | Files | Refs | README

history.js (3279B)


      1 /**
      2  * The history service
      3  * @return {Object}
      4  */
      5 window.FakeTerminal.history = function (instance) {
      6 
      7     /**
      8      * Avoid scope issues by using `base` instead of `this`
      9      * @type {Object}
     10      */
     11     var base = this;
     12 
     13     // --------------------------------------------------------------------------
     14 
     15     /**
     16      * Tracks the number of previously executed commands
     17      * @type {number}
     18      */
     19     base.counter = 0;
     20 
     21     /**
     22      * An array of all the previously executed commands
     23      * @type {Array}
     24      */
     25     base.items = [];
     26 
     27     /**
     28      * Tracks the user's position when browsing items using the arrow keys
     29      * @type {null}
     30      */
     31     base.browseIndex = null;
     32 
     33     // --------------------------------------------------------------------------
     34 
     35     /**
     36      * Constructs window.FakeTerminal.history
     37      * @returns {Object}
     38      * @private
     39      */
     40     base.__construct = function () {
     41         return base;
     42     };
     43 
     44     // --------------------------------------------------------------------------
     45 
     46     /**
     47      * Pushes a new command onto the history array
     48      * @param {String} command The command which was executed
     49      */
     50     base.push = function (command) {
     51         base.counter++;
     52         base.items.push({
     53             counter: base.counter,
     54             command: command
     55         });
     56         if (base.items.length > instance.options.history) {
     57             base.items = base.items.slice(base.items.length - instance.options.history);
     58         }
     59     };
     60 
     61     // --------------------------------------------------------------------------
     62 
     63     /**
     64      * Browse through the command history
     65      * @param  {string} direction Whether to go UP or DOWN through history
     66      * @return {Object}           A reference to the class, for chaining
     67      */
     68     base.browse = function (direction) {
     69 
     70         if (direction === instance.keymap.UP) {
     71 
     72             /**
     73              * Going up through the history. if browseIndex is null then set it
     74              * to the end of history array.
     75              */
     76 
     77             if (base.browseIndex === null) {
     78                 base.browseIndex = base.items.length;
     79             }
     80 
     81             //  Go down an index
     82             base.browseIndex--;
     83 
     84             //  Don't go below 0
     85             if (base.browseIndex < 0) {
     86                 base.browseIndex = 0;
     87             }
     88 
     89         } else if (direction === instance.keymap.DOWN) {
     90 
     91             /**
     92              * Going down through the history. if browseIndex is null then set it
     93              * to the beginning of the history array
     94              */
     95 
     96             if (base.browseIndex === null) {
     97                 base.browseIndex = 0;
     98             }
     99 
    100             //  Go up an index
    101             base.browseIndex++;
    102 
    103             //  Don't go beyond the limits!
    104             if (base.browseIndex >= base.items.length) {
    105                 base.browseIndex = base.items.length;
    106             }
    107         }
    108 
    109         // --------------------------------------------------------------------------
    110 
    111         //  Get the command
    112         if (base.items[base.browseIndex]) {
    113             return base.items[base.browseIndex].command;
    114         } else {
    115             return null;
    116         }
    117     };
    118 
    119     // --------------------------------------------------------------------------
    120 
    121     return base.__construct();
    122 };