This commit is contained in:
Peter Cottle 2012-09-10 13:24:53 -07:00
parent 3d1890bb3b
commit a4169dc016
4 changed files with 64 additions and 22 deletions

View file

@ -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> [<args>] \
");
}]
];
};
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
);
}

View file

@ -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', '</br>');
};
function CommandResult(msg) {
this.msg = msg;
}
CommandResult.prototype = _.copy(Error.prototype);
CommandResult.prototype.toString = function() {
return 'Command Result: ' + this.msg;
};

View file

@ -50,5 +50,6 @@
<script src="git.js"></script>
<script src="commandline.js"></script>
<script src="views.js"></script>
<script src="errors.js"></script>
</body>
</html>

View file

@ -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 = ' \
<p class="commandLine <%= class %>"> \
<p class="commandLine <%= className %>"> \
<span class="arrows">&gt; &gt; &gt;</span> \
<%= command %> \
</p> \
';
this.resultTemplate = ' \
<p class="commandResult <%= class %>"> \
<%= result %>
</p>
<p class="commandResult <%= className %>"> \
<%= result %> \
</p> \
';
},
@ -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()
}
)