From 64c6bcb4693331bece9fa067750254bdfb6d5962 Mon Sep 17 00:00:00 2001 From: Darren Date: Mon, 17 Sep 2012 01:43:03 +0100 Subject: [PATCH] Input aliasing (InputPreProcessor) --- client_backbone/dev/model_application.js | 40 +++++++-- client_backbone/dev/utils.js | 107 +++++++++++++++++++++++ client_backbone/dev/view.js | 22 ++++- 3 files changed, 162 insertions(+), 7 deletions(-) diff --git a/client_backbone/dev/model_application.js b/client_backbone/dev/model_application.js index 7031a4d..672ec7d 100644 --- a/client_backbone/dev/model_application.js +++ b/client_backbone/dev/model_application.js @@ -482,26 +482,30 @@ kiwi.model.Application = Backbone.Model.extend(new (function () { * Bind to certain commands that may be typed into the control box */ this.bindControllboxCommands = function (controlbox) { + $.extend(controlbox.preprocessor.aliases, { + '/p': '/part $1+', + '/me': '/action $1+', + '/j': '/join $1+', + '/q': '/query $1+', + '/k': '/kick $1+', + }); + controlbox.on('unknown_command', this.unknownCommand); controlbox.on('command', this.allCommands); controlbox.on('command_msg', this.msgCommand); controlbox.on('command_action', this.actionCommand); - controlbox.on('command_me', this.actionCommand); controlbox.on('command_join', this.joinCommand); - controlbox.on('command_j', this.joinCommand); controlbox.on('command_part', this.partCommand); - controlbox.on('command_p', this.partCommand); controlbox.on('command_nick', function (ev) { kiwi.gateway.changeNick(ev.params[0]); }); controlbox.on('command_query', this.queryCommand); - controlbox.on('command_q', this.queryCommand); controlbox.on('command_topic', this.topicCommand); @@ -509,7 +513,6 @@ kiwi.model.Application = Backbone.Model.extend(new (function () { controlbox.on('command_quote', this.quoteCommand); - controlbox.on('command_k', this.kickCommand); controlbox.on('command_kick', this.kickCommand); @@ -525,6 +528,33 @@ kiwi.model.Application = Backbone.Model.extend(new (function () { $script(ev.params[0] + '?' + (new Date().getTime())); }); + controlbox.on('command_alias', function (ev) { + // No parameters passed so list them + if (!ev.params[1]) { + $.each(controlbox.preprocessor.aliases, function (name, rule) { + kiwi.app.panels.server.addMsg(' ', name + ' => ' + rule); + }); + return; + } + + // Deleting an alias? + if (ev.params[0] === 'del' || ev.params[0] === 'delete') { + var name = ev.params[1]; + if (name[0] !== '/') name = '/' + name; + delete controlbox.preprocessor.aliases[name]; + return; + } + + // Add the alias + var name = ev.params[0]; + ev.params.shift(); + var rule = ev.params.join(' '); + + if (name[0] !== '/') name = '/' + name; + console.log('added', name, 'as', rule); + controlbox.preprocessor.aliases[name] = rule; + }); + controlbox.on('command_applet', this.appletCommand); controlbox.on('command_settings', this.settingsCommand); }; diff --git a/client_backbone/dev/utils.js b/client_backbone/dev/utils.js index bd4d6a2..26f164e 100644 --- a/client_backbone/dev/utils.js +++ b/client_backbone/dev/utils.js @@ -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= 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 */ diff --git a/client_backbone/dev/view.js b/client_backbone/dev/view.js index 19238a4..80bde05 100644 --- a/client_backbone/dev/view.js +++ b/client_backbone/dev/view.js @@ -468,6 +468,9 @@ kiwi.view.ControlBox = Backbone.View.extend({ // Hold tab autocomplete data tabcomplete: {active: false, data: [], prefix: ''}, + // Instance of InputPreProcessor + preprocessor: null, + events: { 'keydown input': 'process' }, @@ -475,6 +478,9 @@ kiwi.view.ControlBox = Backbone.View.extend({ initialize: function () { var that = this; + this.preprocessor = new InputPreProcessor(); + this.preprocessor.recursive_depth = 5; + kiwi.gateway.bind('change:nick', function () { $('.nick', that.$el).text(this.get('nick')); }); @@ -596,10 +602,22 @@ kiwi.view.ControlBox = Backbone.View.extend({ processInput: function (command_raw) { - var command, - params = command_raw.split(' '); + var command, params, + pre_processed; + // The default command + if (command_raw[0] !== '/') { + command_raw = '/msg ' + kiwi.app.panels.active.get('name') + ' ' + command_raw; + } + + // Process the raw command for any aliases + this.preprocessor.vars.server = kiwi.gateway.get('name'); + this.preprocessor.vars.channel = kiwi.app.panels.active.get('name'); + this.preprocessor.vars.destination = this.preprocessor.vars.channel; + command_raw = this.preprocessor.process(command_raw); + // Extract the command and parameters + params = command_raw.split(' '); if (params[0][0] === '/') { command = params[0].substr(1).toLowerCase(); params = params.splice(1); -- 2.25.1