Input aliasing (InputPreProcessor)
authorDarren <darren@darrenwhitlen.com>
Mon, 17 Sep 2012 00:43:03 +0000 (01:43 +0100)
committerDarren <darren@darrenwhitlen.com>
Mon, 17 Sep 2012 00:43:03 +0000 (01:43 +0100)
client_backbone/dev/model_application.js
client_backbone/dev/utils.js
client_backbone/dev/view.js

index 7031a4d8c2fae1b3a5ec8c1cdf220def005dcf7e..672ec7dd083a3a475994ca01806f54e84f63b21f 100644 (file)
@@ -482,26 +482,30 @@ kiwi.model.Application = Backbone.Model.extend(new (function () {
      * Bind to certain commands that may be typed into the control box\r
      */\r
     this.bindControllboxCommands = function (controlbox) {\r
+        $.extend(controlbox.preprocessor.aliases, {\r
+            '/p': '/part $1+',\r
+            '/me': '/action $1+',\r
+            '/j': '/join $1+',\r
+            '/q': '/query $1+',\r
+            '/k': '/kick $1+',\r
+        });\r
+\r
         controlbox.on('unknown_command', this.unknownCommand);\r
 \r
         controlbox.on('command', this.allCommands);\r
         controlbox.on('command_msg', this.msgCommand);\r
 \r
         controlbox.on('command_action', this.actionCommand);\r
-        controlbox.on('command_me', this.actionCommand);\r
 \r
         controlbox.on('command_join', this.joinCommand);\r
-        controlbox.on('command_j', this.joinCommand);\r
 \r
         controlbox.on('command_part', this.partCommand);\r
-        controlbox.on('command_p', this.partCommand);\r
 \r
         controlbox.on('command_nick', function (ev) {\r
             kiwi.gateway.changeNick(ev.params[0]);\r
         });\r
 \r
         controlbox.on('command_query', this.queryCommand);\r
-        controlbox.on('command_q', this.queryCommand);\r
 \r
         controlbox.on('command_topic', this.topicCommand);\r
 \r
@@ -509,7 +513,6 @@ kiwi.model.Application = Backbone.Model.extend(new (function () {
 \r
         controlbox.on('command_quote', this.quoteCommand);\r
 \r
-        controlbox.on('command_k', this.kickCommand);\r
         controlbox.on('command_kick', this.kickCommand);\r
 \r
 \r
@@ -525,6 +528,33 @@ kiwi.model.Application = Backbone.Model.extend(new (function () {
             $script(ev.params[0] + '?' + (new Date().getTime()));\r
         });\r
 \r
+        controlbox.on('command_alias', function (ev) {\r
+            // No parameters passed so list them\r
+            if (!ev.params[1]) {\r
+                $.each(controlbox.preprocessor.aliases, function (name, rule) {\r
+                    kiwi.app.panels.server.addMsg(' ', name + '   =>   ' + rule);\r
+                });\r
+                return;\r
+            }\r
+\r
+            // Deleting an alias?\r
+            if (ev.params[0] === 'del' || ev.params[0] === 'delete') {\r
+                var name = ev.params[1];\r
+                if (name[0] !== '/') name = '/' + name;\r
+                delete controlbox.preprocessor.aliases[name];\r
+                return;\r
+            }\r
+\r
+            // Add the alias\r
+            var name = ev.params[0];\r
+            ev.params.shift();\r
+            var rule = ev.params.join(' ');\r
+\r
+            if (name[0] !== '/') name = '/' + name;\r
+            console.log('added', name, 'as', rule);\r
+            controlbox.preprocessor.aliases[name] = rule;\r
+        });\r
+\r
         controlbox.on('command_applet', this.appletCommand);\r
         controlbox.on('command_settings', this.settingsCommand);\r
     };\r
index bd4d6a2dba150e5813cd229b93a48bb7d05f6f9e..26f164e42806cfe2de475bbcbe871de246889cec 100644 (file)
@@ -97,6 +97,113 @@ function secondsToTime(secs) {
 
 
 
+
+
+/* Command input Alias + re-writing */
+function InputPreProcessor () {
+    this.recursive_depth = 3;
+
+    this.aliases = {};
+    this.vars = {version: 1};
+
+    // Current recursive depth
+    var depth = 0;
+
+
+    // Takes an array of words to process!
+    this.processInput = function (input) {
+        var words = input || [],
+            alias = this.aliases[words[0]],
+            alias_len,
+            current_alias_word = '',
+            compiled = [];
+
+        // If an alias wasn't found, return the original input
+        if (!alias) return input;
+
+        // Split the alias up into useable words
+        alias = alias.split(' ');
+        alias_len = alias.length;
+
+        // Iterate over each word and pop them into the final compiled array.
+        // Any $ words are processed with the result ending into the compiled array.
+        for (var i=0; i<alias_len; i++) {
+            current_alias_word = alias[i];
+
+            // Non $ word
+            if (current_alias_word[0] !== '$') {
+                compiled.push(current_alias_word);
+                continue;
+            }
+
+            // Refering to an input word ($N)
+            if (!isNaN(current_alias_word[1])) {
+                var num = current_alias_word.match(/\$(\d+)(\+)?(\d+)?/);
+
+                // Did we find anything or does the word it refers to non-existant?
+                if (!num || !words[num[1]]) continue;
+                
+                if (num[2] === '+' && num[3]) {
+                    // Add X number of words
+                    compiled = compiled.concat(words.slice(parseInt(num[1], 10), parseInt(num[1], 10) + parseInt(num[3], 10)));
+                } else if (num[2] === '+') {
+                    // Add the remaining of the words
+                    compiled = compiled.concat(words.slice(parseInt(num[1], 10)));
+                } else {
+                    // Add a single word
+                    compiled.push(words[parseInt(num[1], 10)]);
+                }
+
+                continue;
+            }
+
+
+            // Refering to a variable
+            if (typeof this.vars[current_alias_word.substr(1)] !== 'undefined') {
+
+                // Get the variable
+                compiled.push(this.vars[current_alias_word.substr(1)]);
+
+                continue;
+            }
+
+        }
+
+        return compiled;
+    };
+
+
+    this.process = function (input) {
+        input = input || '';
+
+        var words = input.split(' ');
+
+        depth++;
+        if (depth >= this.recursive_depth) {
+            depth--;
+            return input;
+        }
+
+        if (this.aliases[words[0]]) {
+            words = this.processInput(words);
+            
+            if (this.aliases[words[0]]) {
+                words = this.process(words.join(' ')).split(' ');
+            }
+
+        }
+
+        depth--;
+        return words.join(' ');
+    };
+}
+
+
+
+
+
+
+
 /**
  * Convert HSL to RGB formatted colour
  */
index 19238a4a1a74cfb2b041c18dd8554bb7cf5df323..80bde051d87c3c9e5e0abbded21c324809701126 100644 (file)
@@ -468,6 +468,9 @@ kiwi.view.ControlBox = Backbone.View.extend({
     // Hold tab autocomplete data\r
     tabcomplete: {active: false, data: [], prefix: ''},\r
 \r
+    // Instance of InputPreProcessor\r
+    preprocessor: null,\r
+\r
     events: {\r
         'keydown input': 'process'\r
     },\r
@@ -475,6 +478,9 @@ kiwi.view.ControlBox = Backbone.View.extend({
     initialize: function () {\r
         var that = this;\r
 \r
+        this.preprocessor = new InputPreProcessor();\r
+        this.preprocessor.recursive_depth = 5;\r
+\r
         kiwi.gateway.bind('change:nick', function () {\r
             $('.nick', that.$el).text(this.get('nick'));\r
         });\r
@@ -596,10 +602,22 @@ kiwi.view.ControlBox = Backbone.View.extend({
 \r
 \r
     processInput: function (command_raw) {\r
-        var command,\r
-            params = command_raw.split(' ');\r
+        var command, params,\r
+            pre_processed;\r
         \r
+        // The default command\r
+        if (command_raw[0] !== '/') {\r
+            command_raw = '/msg ' + kiwi.app.panels.active.get('name') + ' ' + command_raw;\r
+        }\r
+\r
+        // Process the raw command for any aliases\r
+        this.preprocessor.vars.server = kiwi.gateway.get('name');\r
+        this.preprocessor.vars.channel = kiwi.app.panels.active.get('name');\r
+        this.preprocessor.vars.destination = this.preprocessor.vars.channel;\r
+        command_raw = this.preprocessor.process(command_raw);\r
+\r
         // Extract the command and parameters\r
+        params = command_raw.split(' ');\r
         if (params[0][0] === '/') {\r
             command = params[0].substr(1).toLowerCase();\r
             params = params.splice(1);\r