ls and cd improved, cat added

This commit is contained in:
BlackLight 2010-12-23 14:25:43 +01:00
parent e6e55ab6fd
commit 25c03b8caf
4 changed files with 76 additions and 13 deletions

View file

@ -206,10 +206,7 @@ function blash ()
} }
} }
if ( dirs.length == 0 ) if ( dirs.length == 1 ) {
{
this.cmdOut.innerHTML = '<br/>Sorry, no matches for `' + this.prompt.value + "'";
} else if ( dirs.length == 1 ) {
this.prompt.value = this.prompt.value.replace ( arg, dirs[0].name + (( dirs[0].type == 'directory' ) ? '/' : '' )); this.prompt.value = this.prompt.value.replace ( arg, dirs[0].name + (( dirs[0].type == 'directory' ) ? '/' : '' ));
} else { } else {
this.cmdOut.innerHTML = ''; this.cmdOut.innerHTML = '';
@ -301,8 +298,35 @@ function blash ()
arg = this.path + '/' + arg; arg = this.path + '/' + arg;
} }
arg = arg.replace ( /\/*$/, '' );
arg = arg.replace ( /\/+/, '/' ); arg = arg.replace ( /\/+/, '/' );
if ( arg != '/' )
{
arg = arg.replace ( /\/*$/, '' );
}
while ( arg.match ( /^(.+?\/?\.\.)/ ))
{
var part = RegExp.$1;
if ( arg.match ( /^(.+?)\/?\.\./ ))
{
if ( RegExp.$1 == '/' )
{
arg = arg.replace ( part, '/' );
} else {
part.match ( /^(.*)\/[^\/]*\/\.\..*$/ );
var sup = RegExp.$1;
arg = arg.replace ( part, sup );
if ( arg.length == 0 )
{
arg = '/';
}
}
}
}
return arg; return arg;
} }
} }

View file

@ -67,6 +67,7 @@
], ],
"commands" : [ "commands" : [
"cat",
"cd", "cd",
"ls", "ls",
"man", "man",

45
commands/cat.json Normal file
View file

@ -0,0 +1,45 @@
{
"name" : "cat",
"info" : {
"syntax" : "cat &lt;file&gt;",
"brief" : "Show the content of a page",
},
"action" : function ( arg )
{
var out = '';
if ( !arg || arg.length == 0 )
{
return "Argument required<br/>\n";
}
var found = false;
var dir = shell.json.directories;
arg = shell.expandPath ( arg );
for ( var i=0; i < dir.length && !found; i++ )
{
if ( dir[i].path == arg )
{
found = true;
if ( dir[i].type == 'directory' )
{
return "cat: " + dir[i].path + ": Is a directory<br/>\n";
} else {
if ( dir[i].href )
{
window.open ( dir[i].href );
} else if ( dir[i].content ) {
return dir[i].content + "<br/>\n";
}
}
}
}
return out;
},
}

View file

@ -15,15 +15,8 @@
return "Parameter expected<br/>\n"; return "Parameter expected<br/>\n";
} }
arg = arg.replace ( /\/$/, '' );
if ( arg.match ( /^[^\/]/ ))
{
arg = this.path + '/' + arg;
arg = arg.replace ( /\/+/, '/' );
}
var found = false; var found = false;
arg = shell.expandPath ( arg );
for ( var i=0; i < shell.json.directories.length && !found; i++ ) for ( var i=0; i < shell.json.directories.length && !found; i++ )
{ {