diff --git a/src/commandline.js b/src/commandline.js index d1ace2f0..b26606db 100644 --- a/src/commandline.js +++ b/src/commandline.js @@ -34,8 +34,43 @@ Command.prototype.getRegexMap = function() { }; }; +Command.prototype.getSandboxCommands = function() { + return [ + [/^ls/, function() { + throw new CommandResult("\ + DontWorryAboutFilesInThisDemo.txt\ + "); + }], + [/^cd/, function() { + throw new CommandResult("\ + Directory Changed to '/directories/dont/matter/in/this/demo' \ + "); + }], + [/^git$/, function() { + throw new CommandResult("\ + Git Version PCOTTLE.1.0\ + Usage:\ + git [] \ + "); + }] + ]; +}; + Command.prototype.parse = function(str) { - // first check if shortcut exists, and replace, but + // first if the string is empty, they just want a blank line + if (!str.length) { + throw new CommandResult(""); + } + + // then check if it's one of our sandbox commands + _.each(this.getSandboxCommands(), function(tuple) { + var regex = tuple[0]; + if (regex.exec(str)) { + tuple[1](); + } + }); + + // then check if shortcut exists, and replace, but // preserve options if so _.each(this.getShortcutMap(), function(regex, method) { var results = regex.exec(str); @@ -46,9 +81,14 @@ Command.prototype.parse = function(str) { // see if begins with git if (str.slice(0,3) !== 'git') { - throw new Error('Git commands only, sorry!'); + throw new CommandProcessError('Git commands only, sorry!'); } + // ok, we have a (probably) valid command. actually parse it + this.gitParse(str); +}; + +Command.prototype.gitParse = function(str) { // now slice off command part this.fullCommand = str.slice('git '.length); @@ -65,7 +105,7 @@ Command.prototype.parse = function(str) { }, this); if (!matched) { - throw new Error( + throw new CommandProcessError( "Sorry, this demo does not support that git command: " + this.fullCommand ); } diff --git a/src/errors.js b/src/errors.js index 2104b827..9ae595d6 100644 --- a/src/errors.js +++ b/src/errors.js @@ -2,18 +2,18 @@ function CommandProcessError(msg) { this.msg = msg; } -CommandProcessError.prototype = _.copy(Error.prototype); - CommandProcessError.prototype.toString = function() { return 'Command Process Error: ' + this.msg; }; +CommandProcessError.prototype.toResult = function() { + return this.msg.replace('\n', '
'); +}; + function CommandResult(msg) { this.msg = msg; } -CommandResult.prototype = _.copy(Error.prototype); - CommandResult.prototype.toString = function() { return 'Command Result: ' + this.msg; }; diff --git a/src/index.html b/src/index.html index e22659f0..91fcb43b 100644 --- a/src/index.html +++ b/src/index.html @@ -50,5 +50,6 @@ + diff --git a/src/views.js b/src/views.js index 5278c540..c13ead01 100644 --- a/src/views.js +++ b/src/views.js @@ -43,21 +43,18 @@ var CommandLineView = Backbone.View.extend({ }, submit: function() { - var value = this.$('#commandTextField').val(); + var value = this.$('#commandTextField').val().replace('\n', ''); this.$('#commandTextField').val(''); + + events.trigger('commandConsumed', value); + try { var command = new Command(value); console.log(command); // immediately execute for now, will change later events.trigger('gitCommandReady', command); } catch (err) { - if (err instanceof CommandProcessError) { - events.trigger('commandProcessError', err); - } else if (err instanceof CommandResultError) { - - } else { - throw err; - } + this.processError(err); } } }); @@ -77,16 +74,16 @@ var CommandLineHistoryView = Backbone.View.extend({ )); this.commandTemplate = ' \ -

\ +

\ > > > \ <%= command %> \

\ '; this.resultTemplate = ' \ -

\ - <%= result %> -

+

\ + <%= result %> \ +

\ '; }, @@ -95,7 +92,7 @@ var CommandLineHistoryView = Backbone.View.extend({ _.template( this.commandTemplate, { - class: 'pastCommand', + className: 'pastCommand', command: commandText } ) @@ -107,7 +104,7 @@ var CommandLineHistoryView = Backbone.View.extend({ _.template( this.resultTemplate, { - class: 'errorResult', + className: 'errorResult', result: err.toResult() } ) @@ -115,11 +112,15 @@ var CommandLineHistoryView = Backbone.View.extend({ }, commandResultPrint: function(err) { + if (!err.msg.length) { + // blank lines + return; + } this.$('#commandDisplay').append( _.template( this.resultTemplate, { - class; 'commandResult', + className: 'commandResult', result: err.toResult() } )