Skip to content

Commit

Permalink
References #2
Browse files Browse the repository at this point in the history
Apply a convention consistently through the code and document it.
  • Loading branch information
fdemian committed Nov 1, 2014
1 parent 5d6ff1b commit 3cb865c
Show file tree
Hide file tree
Showing 3 changed files with 365 additions and 326 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@ jqTerminal
==========

A Unix-like terminal, written in Javascript.


###Code Conventions

- Tabs should not be used.
- Indentation must use 2 spaces.
- The curly braces at the start and end of functions must go on their own line. Exceptions are cases when doing this results in a javascript error.
174 changes: 91 additions & 83 deletions js/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,98 +7,105 @@

defaultCommands =
[
{name:"man",man:"man - display the on-line manual pages (aka ''man pages'')",run:man},
{name:"clear",man:"clear - clear the terminal screen",run:clear},
{name:"shutdown",man:"shutdown - bring the system down",run:shutdown},
{name:"reboot",man:"reboot - reboot the system",run:shutdown},
{name:"ls",man:"ls - list directory contents",run:ls},
{name:"cat",man:"cat - concatenate files and print on the standard output",run:cat},
{name:"echo",man:"echo - echo the contents of a file",run:echo},
{name:"export", man:"export - set an environment variable", run:exportVariable}
{name:"man",man:"man - display the on-line manual pages (aka ''man pages'')",run:man},
{name:"clear",man:"clear - clear the terminal screen",run:clear},
{name:"shutdown",man:"shutdown - bring the system down",run:shutdown},
{name:"reboot",man:"reboot - reboot the system",run:shutdown},
{name:"ls",man:"ls - list directory contents",run:ls},
{name:"cat",man:"cat - concatenate files and print on the standard output",run:cat},
{name:"echo",man:"echo - echo the contents of a file",run:echo},
{name:"export", man:"export - set an environment variable", run:exportVariable}
];

function clear(arguments, self)
{
self.clear();
self.clear();
}

// Show the manual pages for a given command.
function man(arguments, self)
{
var consoleCommands = self.commands;
var command = arguments[1];
self.write("\n");
if( command !== undefined && command.trim() != "")
{
// Does the command we're trying to search information about exist?
var commandIndex = $.inArray(command, $.map(consoleCommands,function(item,index){ return item.name;}));
if(commandIndex != -1)
{
self.write(consoleCommands[commandIndex].man);
}
else
{
self.write("No manual entry for " + command);
}
}
else
{
self.write("What manual page do you want?");
}
var consoleCommands = self.commands;
var command = arguments[1];
self.write("\n");

if( command !== undefined && command.trim() != "")
{
// Does the command we're trying to search information about exist?
var commandIndex = $.inArray(command, $.map(consoleCommands,function(item,index){ return item.name;}));

if(commandIndex != -1)
{
self.write(consoleCommands[commandIndex].man);
}
else
{
self.write("No manual entry for " + command);
}
}
else
{
self.write("What manual page do you want?");
}
}

// Bring the system down.
function shutdown(arguments, self)
{
if((arguments !== undefined) && (arguments.length > 1) && (arguments[1].trim()!= "") && (arguments[1].indexOf("-t") != -1))
{
var time = arguments[2].trim();
if(!isNaN(time))
{
// Wait <time> seconds before shuting down the system.
self.write("\n");
self.write("Bringing down the system in " + time + " seconds");
self.write("\n");
setTimeout(function(){shutdown([], self);}, (parseInt(time)*1000));
return parseInt(time);
}
}

self.destroy();

return 0;

if((arguments !== undefined) && (arguments.length > 1) && (arguments[1].trim()!= "") && (arguments[1].indexOf("-t") != -1))
{
var time = arguments[2].trim();
if(!isNaN(time))
{
// Wait <time> seconds before shuting down the system.
self.write("\n");
self.write("Bringing down the system in " + time + " seconds");
self.write("\n");
setTimeout(function(){shutdown([], self);}, (parseInt(time)*1000));
return parseInt(time);
}

}

self.destroy();

return 0;
}

function ls(arguments, self)
{
var separator = (arguments === undefined || arguments.indexOf("-l") == -1)? "\t" : "\n";
var filesInDirectory = self.files;
self.write("\n");

for(dirIndex =0; dirIndex < filesInDirectory.length; dirIndex++)
{
if( arguments.indexOf("-l") !== -1)
{
console.log(filesInDirectory[dirIndex].permissions);
console.log(filesInDirectory[dirIndex].type);
console.log(filesInDirectory[dirIndex].owner);
console.log(filesInDirectory[dirIndex].date);
self.write(filesInDirectory[dirIndex].permissions + " " + filesInDirectory[dirIndex].type + " " + filesInDirectory[dirIndex].owner + " " + filesInDirectory[dirIndex].owner + " " + filesInDirectory[dirIndex].date + " ");
}
self.write(filesInDirectory[dirIndex].name);
self.write(separator);
}
var separator = (arguments === undefined || arguments.indexOf("-l") == -1)? "\t" : "\n";
var filesInDirectory = self.files;
self.write("\n");

for(dirIndex =0; dirIndex < filesInDirectory.length; dirIndex++)
{
if( arguments.indexOf("-l") !== -1)
{
console.log(filesInDirectory[dirIndex].permissions);
console.log(filesInDirectory[dirIndex].type);
console.log(filesInDirectory[dirIndex].owner);
console.log(filesInDirectory[dirIndex].date);
self.write(filesInDirectory[dirIndex].permissions + " " + filesInDirectory[dirIndex].type + " " + filesInDirectory[dirIndex].owner + " " + filesInDirectory[dirIndex].owner + " " + filesInDirectory[dirIndex].date + " ");
}

self.write(filesInDirectory[dirIndex].name);
self.write(separator);

}
}

function cat(arguments, self)
{
var filesInDirectory = self.files;
var filePos = $.inArray(arguments[1], $.map(filesInDirectory,function(item,index){ return item.name;}));
if(filePos != -1)
{
self.write("\n");
self.write(filesInDirectory[filePos].content);
}
var filesInDirectory = self.files;
var filePos = $.inArray(arguments[1], $.map(filesInDirectory,function(item,index){ return item.name;}));

if(filePos != -1)
{
self.write("\n");
self.write(filesInDirectory[filePos].content);
}
}

function echo(arguments, self)
Expand All @@ -108,22 +115,23 @@ function echo(arguments, self)

for(var i = 1; i < arguments.length; i++)
{
if(arguments[i].indexOf("$") == 0)
{
envValue = self.getEnvironmentVariable(arguments[i].slice(1, arguments[i].length));
self.write(envValue);
self.write("\n");
}
else
{
self.write(arguments[i]);
self.write(" ");
}
if(arguments[i].indexOf("$") == 0)
{
envValue = self.getEnvironmentVariable(arguments[i].slice(1, arguments[i].length));
self.write(envValue);
self.write("\n");
}
else
{
self.write(arguments[i]);
self.write(" ");
}
}
}

// Exports an environment variable.
// arguments is usually a string of the form <environment variable> = <value>
/* Exports an environment variable.
"arguments" is usually a string of the form <environment variable> = <value>
*/
function exportVariable(arguments, self)
{
var splittedArguments = arguments[1].split("=");
Expand Down
Loading

0 comments on commit 3cb865c

Please sign in to comment.