mirror of
https://github.com/pcottle/learnGitBranching.git
synced 2025-06-30 01:34:26 +02:00
more
This commit is contained in:
parent
3d1890bb3b
commit
a4169dc016
4 changed files with 64 additions and 22 deletions
|
@ -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) {
|
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
|
// preserve options if so
|
||||||
_.each(this.getShortcutMap(), function(regex, method) {
|
_.each(this.getShortcutMap(), function(regex, method) {
|
||||||
var results = regex.exec(str);
|
var results = regex.exec(str);
|
||||||
|
@ -46,9 +81,14 @@ Command.prototype.parse = function(str) {
|
||||||
|
|
||||||
// see if begins with git
|
// see if begins with git
|
||||||
if (str.slice(0,3) !== '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
|
// now slice off command part
|
||||||
this.fullCommand = str.slice('git '.length);
|
this.fullCommand = str.slice('git '.length);
|
||||||
|
|
||||||
|
@ -65,7 +105,7 @@ Command.prototype.parse = function(str) {
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
if (!matched) {
|
if (!matched) {
|
||||||
throw new Error(
|
throw new CommandProcessError(
|
||||||
"Sorry, this demo does not support that git command: " + this.fullCommand
|
"Sorry, this demo does not support that git command: " + this.fullCommand
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,18 +2,18 @@ function CommandProcessError(msg) {
|
||||||
this.msg = msg;
|
this.msg = msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
CommandProcessError.prototype = _.copy(Error.prototype);
|
|
||||||
|
|
||||||
CommandProcessError.prototype.toString = function() {
|
CommandProcessError.prototype.toString = function() {
|
||||||
return 'Command Process Error: ' + this.msg;
|
return 'Command Process Error: ' + this.msg;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
CommandProcessError.prototype.toResult = function() {
|
||||||
|
return this.msg.replace('\n', '</br>');
|
||||||
|
};
|
||||||
|
|
||||||
function CommandResult(msg) {
|
function CommandResult(msg) {
|
||||||
this.msg = msg;
|
this.msg = msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
CommandResult.prototype = _.copy(Error.prototype);
|
|
||||||
|
|
||||||
CommandResult.prototype.toString = function() {
|
CommandResult.prototype.toString = function() {
|
||||||
return 'Command Result: ' + this.msg;
|
return 'Command Result: ' + this.msg;
|
||||||
};
|
};
|
||||||
|
|
|
@ -50,5 +50,6 @@
|
||||||
<script src="git.js"></script>
|
<script src="git.js"></script>
|
||||||
<script src="commandline.js"></script>
|
<script src="commandline.js"></script>
|
||||||
<script src="views.js"></script>
|
<script src="views.js"></script>
|
||||||
|
<script src="errors.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
31
src/views.js
31
src/views.js
|
@ -43,21 +43,18 @@ var CommandLineView = Backbone.View.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
submit: function() {
|
submit: function() {
|
||||||
var value = this.$('#commandTextField').val();
|
var value = this.$('#commandTextField').val().replace('\n', '');
|
||||||
this.$('#commandTextField').val('');
|
this.$('#commandTextField').val('');
|
||||||
|
|
||||||
|
events.trigger('commandConsumed', value);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var command = new Command(value);
|
var command = new Command(value);
|
||||||
console.log(command);
|
console.log(command);
|
||||||
// immediately execute for now, will change later
|
// immediately execute for now, will change later
|
||||||
events.trigger('gitCommandReady', command);
|
events.trigger('gitCommandReady', command);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof CommandProcessError) {
|
this.processError(err);
|
||||||
events.trigger('commandProcessError', err);
|
|
||||||
} else if (err instanceof CommandResultError) {
|
|
||||||
|
|
||||||
} else {
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -77,16 +74,16 @@ var CommandLineHistoryView = Backbone.View.extend({
|
||||||
));
|
));
|
||||||
|
|
||||||
this.commandTemplate = ' \
|
this.commandTemplate = ' \
|
||||||
<p class="commandLine <%= class %>"> \
|
<p class="commandLine <%= className %>"> \
|
||||||
<span class="arrows">> > ></span> \
|
<span class="arrows">> > ></span> \
|
||||||
<%= command %> \
|
<%= command %> \
|
||||||
</p> \
|
</p> \
|
||||||
';
|
';
|
||||||
|
|
||||||
this.resultTemplate = ' \
|
this.resultTemplate = ' \
|
||||||
<p class="commandResult <%= class %>"> \
|
<p class="commandResult <%= className %>"> \
|
||||||
<%= result %>
|
<%= result %> \
|
||||||
</p>
|
</p> \
|
||||||
';
|
';
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -95,7 +92,7 @@ var CommandLineHistoryView = Backbone.View.extend({
|
||||||
_.template(
|
_.template(
|
||||||
this.commandTemplate,
|
this.commandTemplate,
|
||||||
{
|
{
|
||||||
class: 'pastCommand',
|
className: 'pastCommand',
|
||||||
command: commandText
|
command: commandText
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -107,7 +104,7 @@ var CommandLineHistoryView = Backbone.View.extend({
|
||||||
_.template(
|
_.template(
|
||||||
this.resultTemplate,
|
this.resultTemplate,
|
||||||
{
|
{
|
||||||
class: 'errorResult',
|
className: 'errorResult',
|
||||||
result: err.toResult()
|
result: err.toResult()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -115,11 +112,15 @@ var CommandLineHistoryView = Backbone.View.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
commandResultPrint: function(err) {
|
commandResultPrint: function(err) {
|
||||||
|
if (!err.msg.length) {
|
||||||
|
// blank lines
|
||||||
|
return;
|
||||||
|
}
|
||||||
this.$('#commandDisplay').append(
|
this.$('#commandDisplay').append(
|
||||||
_.template(
|
_.template(
|
||||||
this.resultTemplate,
|
this.resultTemplate,
|
||||||
{
|
{
|
||||||
class; 'commandResult',
|
className: 'commandResult',
|
||||||
result: err.toResult()
|
result: err.toResult()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue