Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Feature: New command 'man' added to Jsterm #8

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 164 additions & 19 deletions commands/com1.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
var COMMANDS = COMMANDS || {};

COMMANDS.cat = function(argv, cb) {
var filenames = this._terminal.parseArgs(argv).filenames,
stdout;
var filenames = this._terminal.parseArgs(argv).filenames,stdout;

this._terminal.scroll();
if (!filenames.length) {
Expand Down Expand Up @@ -132,7 +131,23 @@ COMMANDS.clear = function(argv, cb) {
this._terminal.div.innerHTML = '';
cb();
}

/**
* echo - display a line of text.
*/
COMMANDS.echo = function(argv, cb) {
var outputs = this._terminal.parseArgs(argv).filenames;
if (!outputs){
output = '<br>';
this._terminal.write(output);
}
else{
outputs.forEach(function(output, i) {
this._terminal.write(output);
this._terminal.write(' ');
}, this);
}
cb();
}
COMMANDS.sudo = function(argv, cb) {
var count = 0;
this._terminal.returnHandler = function() {
Expand Down Expand Up @@ -167,7 +182,7 @@ COMMANDS.login = function(argv, cb) {

COMMANDS.tree = function(argv, cb) {
var term = this._terminal,
home;
home;

function writeTree(dir, level) {
dir.contents.forEach(function(entry) {
Expand All @@ -176,7 +191,7 @@ COMMANDS.tree = function(argv, cb) {
if (entry.name.startswith('.'))
return;
for (var i = 0; i < level; i++)
str += '| ';
str += '| ';
str += '|&mdash;&mdash;';
term.write(str);
term.writeLink(entry, term.dirString(dir) + '/' + entry.name);
Expand All @@ -194,19 +209,149 @@ COMMANDS.tree = function(argv, cb) {

COMMANDS.help = function(argv, cb) {
this._terminal.write(
'You can navigate either by clicking on anything that ' +
'<a href="javascript:void(0)">underlines</a> when you put your mouse ' +
'over it, or by typing commands in the terminal. Type the name of a ' +
'<span class="exec">link</span> to view it. Use "cd" to change into a ' +
'<span class="dir">directory</span>, or use "ls" to list the contents ' +
'of that directory. The contents of a <span class="text">file</span> ' +
'can be viewed using "cat". <span class="img">Images</span> are ' +
'displayed using "gimp".<br><br>If there is a command you want to get ' +
'out of, press Ctrl+C or Ctrl+D.<br><br>');
this._terminal.write('Commands are:<br>');
for (var c in this._terminal.commands) {
if (this._terminal.commands.hasOwnProperty(c) && !c.startswith('_'))
this._terminal.write(c + ' ');
'You can navigate either by clicking on anything that ' +
'<a href="javascript:void(0)">underlines</a> when you put your mouse ' +
'over it, or by typing commands in the terminal. Type the name of a ' +
'<span class="exec">link</span> to view it. Use "cd" to change into a ' +
'<span class="dir">directory</span>, or use "ls" to list the contents ' +
'of that directory. The contents of a <span class="text">file</span> ' +
'can be viewed using "cat". <span class="img">Images</span> are ' +
'displayed using "gimp".<br><br>If there is a command you want to get ' +
'out of, press Ctrl+C or Ctrl+D.<br><br>');
this._terminal.write('Commands are:<br>');
for (var c in this._terminal.commands) {
if (this._terminal.commands.hasOwnProperty(c) && !c.startswith('_'))
this._terminal.write(c + ' ');
}
cb();
}
COMMANDS.man = function(argv, cb) {
var validManPages = ["cat",
"cd",
"clear",
"echo",
"gimp",
"help",
"man",
"mkdir",
"ls",
"sudo",
"login",
"tree"];
var term = this._terminal;

function isInArray(validManPages, com)
{
return validManPages.indexOf(com.toLowerCase()) > -1;
}
var result = this._terminal.parseArgs(argv),

filename = result.filenames[0];

entry = filename;


this._terminal.scroll();
outputManPage = function(e, str) {
// if (this._terminal.commands.hasOwnProperty(e)){
if (isInArray(validManPages,e)){
//this.write('exists');
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
if (ajax.readyState == 4 && ajax.status == 200){
term.write(ajax.responseText);
cb();
}
};//closes function
ajax.open('GET', 'json/manPages/'+e, true);
ajax.send();
}
else
{
term.write('No manual entry for ' + e);
cb();
}
}.bind(this._terminal);//closes function

if (!entry){
this._terminal.write('What manual page do you want?');
cb();
}
else {
// could be in a loop for multiple man pages
outputManPage(entry, filename);
}
}

/**
* pwd - output current working directory path.
*/
/*COMMANDS.pwd = function(argv,cb)
{
this._terminal.write(this._terminal.getCWD());
cb();
}
*/
/**
* mkdir - create a new directory in the filesystem
*/
COMMANDS.mkdir = function(argv, cb){

/*
* Recursively searches the filesystem until we reach location to create the new directory.
* @param fs is the current unmodified filesystem.
* @param loc loc is the location where we want to create the new directory.
* @param new_obj is the directory we want to add to the filesystem
*/
function myFunction(fs,loc,new_obj)
{
if(fs.constructor === Array)
{
//loop through objects
for (var i=0; i<fs.length; i++) {
// console.log(fs[i].name);
//console.log(fs[i].type);
handleStuff(fs[i],loc,new_obj);
}
}
else
{
//console.log('fs is not array!');
handleStuff(fs,loc,new_obj);
}
}
cb();

// Create the new directory (new_obj) in the filesystem (fs) in current working directory (current_loc)
function handleStuff(fs,current_loc,new_obj)
{
if( (fs) && (fs.type == "dir") && (fs.name == current_loc[0]))
{
fs["contents"].push(new_obj);
}
else if (fs.type == "dir")
{
myFunction(fs["contents"],current_loc,new_obj);

}

}
var dirName = this._terminal.parseArgs(argv).filenames,stdout;
// The user must specify the name of the directory they want to create
if (!dirName.length) {
this._terminal.write('usage: mkdir [-pv] [-m mode] directory ...');
cb();
}
//get our current location
loc = new String(/[^/]*$/.exec(this._terminal.getCWD())[0]);
loc = [loc];
//this is the current directory of where the user is in the directory tree

//create new object
var new_obj = {"name": dirName[0],"type": "dir","description":"","contents":[]};
//call the function
myFunction(this._terminal.fs,loc,new_obj);

this._terminal.loadFSFromString(this._terminal.fs, function() {
cb();
}.bind(this._terminal));
}
13 changes: 6 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<!doctype html>
<html>
<head>
<title>Clark DuVall</title>
<link rel="shortcut icon" href="/images/favicon.ico">
<meta name="description" content="Clark DuVall's website o' fun!">
<link type="text/css" rel="stylesheet" href="/css/style.css">
<title>Your Name</title>
<link rel="shortcut icon" href="images/favicon.ico">
<link type="text/css" rel="stylesheet" href="css/style.css">
</head>
<body>
<script type="text/javascript" src="/commands/com1.js"></script>
<script type="text/javascript" src="/config/config.js"></script>
<script type="text/javascript" src="/js/jsterm.js"></script>
<script type="text/javascript" src="commands/com1.js"></script>
<script type="text/javascript" src="config/config.js"></script>
<script type="text/javascript" src="js/jsterm.js"></script>
</body>
</html>
12 changes: 10 additions & 2 deletions js/jsterm.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,22 @@
cb();
},

getFS: function(name,cb){
loadFS(name, function(responseText) {
return JSON.parse(responseText);
}.bind(this));
},
loadFS: function(name, cb) {
loadFS(name, function(responseText) {
this.fs = JSON.parse(responseText);
this._addDirs(this.fs, this.fs);
cb && cb();
}.bind(this));
},
loadFSFromString: function(responseText,cb) {
this._addDirs(this.fs, this.fs);
if (cb) cb();
},

loadCommands: function(commands) {
this.commands = commands;
Expand Down Expand Up @@ -490,9 +499,8 @@
String.prototype.startswith = function(s) {
return this.indexOf(s) == 0;
}

var term = Object.create(Terminal);
term.init(CONFIG, '/json/sample.json', COMMANDS, function() {
term.init(CONFIG, 'json/sample.json', COMMANDS, function() {
term.enqueue('login')
.enqueue('clark')
.enqueue('******')
Expand Down
1 change: 1 addition & 0 deletions json/manPages/cat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the man page for cat.