Skip to content

Commit

Permalink
add !! support
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jul 29, 2012
1 parent 4662abd commit 772ba59
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 20 deletions.
15 changes: 0 additions & 15 deletions lib/builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,6 @@ exports['.'] = function(cmd, shell){
return proc;
};

/**
* !! [arg ...]
*/

exports['!!'] = function(cmd, shell){
var proc = new Process;

shell.rl.history.shift();
var last = shell.rl.history[0];
last += cmd.argv.join(' ');
shell.exec(last);

return proc;
};

/**
* cd <path>
*/
Expand Down
9 changes: 5 additions & 4 deletions lib/nshell.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ Shell.prototype.process = function(cmds){
cmd.argv = cmd.argv.map(function(arg){
switch (arg) {
case '$?': return self.lastExitStatus;
case '!!': return self.lastCommand;
default: return utils.unquote(arg);
}
});
Expand Down Expand Up @@ -281,12 +282,12 @@ Shell.prototype.cd = function(dir){

Shell.prototype.exec = function(line){
line = line.trim();
if ('' == line) return this.emit('blank'), this.prompt();
this.lastCommand = line;
if (utils.isDirectory(line)) return this.cd(line);
if ('' == line) return this.lastCommand = line, this.emit('blank'), this.prompt();
if (utils.isDirectory(line)) return this.lastCommand = line, this.cd(line);
var cmds = utils.parse(line);
if (!cmds.length) return this.prompt();
if (!cmds.length) return this.lastCommand = line, this.prompt();
this.process(cmds);
this.lastCommand = line;
debug('cmd %j', cmds);
debug('env %j', cmds.env);
if (!this.lookup(cmds)) return this.prompt();
Expand Down
3 changes: 2 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ function scan(str) {
var m;
while (m = re.exec(str)) {
tok = m[0];
tok = braceExpand(tok);
// TODO: minimatch bug
if ('!!' != tok) tok = braceExpand(tok);
toks = toks.concat(tok);
}
return toks;
Expand Down
5 changes: 5 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ describe('utils.parse(str)', function(){
var cmds = utils.parse('foo{bar,baz,raz}');
cmds[0].should.eql({ env: {}, argv: ['foobaz', 'fooraz'], name: 'foobar' });
})

it('should parse !!', function(){
var cmds = utils.parse('echo !!');
cmds[0].should.eql({ env: {}, argv: ['!!'], name: 'echo' });
})
})

describe('utils.unquote(str)', function(){
Expand Down

0 comments on commit 772ba59

Please sign in to comment.