Skip to content

Commit

Permalink
Added README and more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
padsbanger committed Nov 8, 2015
1 parent 878ae08 commit 457b560
Show file tree
Hide file tree
Showing 20 changed files with 151 additions and 2,226 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ module.exports = function(grunt) {
karma: {
unit: {
configFile: 'karma.conf.js',
singleRun: true
singleRun: false
}
},

Expand Down
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
# pipboy [![Build Status](https://travis-ci.org/padsbanger/pipboy.svg?branch=master)](https://travis-ci.org/padsbanger/pipboy) [![Coverage Status](https://coveralls.io/repos/padsbanger/pipboy/badge.svg?branch=master&service=github)](https://coveralls.io/github/padsbanger/pipboy?branch=master)
# Pipboy [![Build Status](https://travis-ci.org/padsbanger/pipboy.svg?branch=master)](https://travis-ci.org/padsbanger/pipboy) [![Coverage Status](https://coveralls.io/repos/padsbanger/pipboy/badge.svg?branch=master&service=github)](https://coveralls.io/github/padsbanger/pipboy?branch=master)

A simple recreation of Fallout PipBoy 3000 in Angular.js

## Instalation.

Download server from here:

https://github.com/formstack/front-end-project-api

and run it.


Clone this repo and install client:

```
npm install -g grunt-cli
```

```
npm install
```

Run client:

```
grunt serve
```

Run unit tests:

```
grunt karma:unit
```
130 changes: 67 additions & 63 deletions js/src/directives/terminalDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,165 +11,171 @@ pipboy.directive('terminal', function(CommandService) {
pre: function preLink() {},
post: function postLink(scope, element, attrs, controller) {

scope.prompt = '[~]$'
scope.currentDirName = '/home'
scope.currentDirIndex = 0
scope.commandLine = ''
scope.prompt = '[~]$';
scope.currentDirName = '/home';
scope.currentDirIndex = 0;
scope.commandLine = '';

var terminalWindow = element;
var terminal = document.querySelector('.terminal-main');
var input = angular.element(terminalWindow[0].querySelector('input[type="text"]'))
var results = angular.element(terminalWindow[0].querySelector('.terminal-results'))
var input = angular.element(terminalWindow[0].querySelector('input[type="text"]'));
var results = angular.element(terminalWindow[0].querySelector('.terminal-results'));
var line = '';

scope.commandHistory = [];
scope.commandIndex = -1;
scope.currentDirList = {};

CommandService.sendCommand('ls', scope.currentDirIndex).then(function(data) {
scope.currentDirList = data.data.items
start();
});
start();

function commandHanlder(command, param) {

switch (command) {
case 'help':
handleHelp();
scope.handleHelp();
break;

case 'clear':
handleClear();
scope.handleClear();
break;

case 'ls':
handleList(param);
scope.handleList(param);
break;

case 'cat':
handleCat(param);
scope.handleCat(param);
break;

case 'lol':
handleLol();
scope.handleLol();
break;

case 'cd':
handleCd(param);
scope.handleCd(param);
break;

case 'pwd':
handlePwd();
scope.handlePwd();
break;

case 'exit':
handleExit();
scope.handleExit();
break;

default:
handleDefault();
scope.handleDefault();
}
}

function start() {
line = 'Welcome to your very own Pip-Boy 3000 ! <br/>Type `help` to see avaiable commands. <br/>Use arrow keys to toggle between already inputed commands.';
CommandService.sendCommand('ls', scope.currentDirIndex).then(function(data) {
scope.currentDirList = data.data.items;
line = 'Welcome to your very own Pip-Boy 3000 ! <br/>Type `help` to see avaiable commands. <br/>Use arrow keys to toggle between already inputed commands.';

drawLine(line);
});

drawLine(line)
}

function handleAutoComplete(query) {
scope.handleAutoComplete = function(query) {
CommandService.sendAutoComplete(scope.currentDirIndex, query).then(function(data) {
var data = data.data.items;
var autocomplete = '';
if (Object.keys(data).length) {
for (var prop in data) {
// I want only first result, since server never returns more, even if i ask nicely
autocomplete = data[prop].name
if (data.hasOwnProperty(prop)) {
// I want only first result, since server never returns more, even if i ask nicely
autocomplete = data[prop].name;
}
break;
}

scope.commandLine = commandParser(scope.commandLine).command + ' ' + autocomplete
scope.commandLine = commandParser(scope.commandLine).command + ' ' + autocomplete;
}

})
}

function drawLine(text, file) {
line = '';
var element = {}
var element = {};
if (text) {
element = document.createElement('pre');
if (!file) {
element.className = 'css-typing';
} else {}
element.innerHTML = text;
results[0].appendChild(element)
results[0].appendChild(element);
}

terminalWindow[0].scrollTop = terminalWindow[0].scrollHeight;
}


function handleDefault() {
scope.handleDefault = function() {
line = 'Unrecognized input. Type `help` to see all commands.';
drawLine(line);
}

function handleHelp() {
scope.handleHelp = function() {
line = 'help clear ls cat cd pwd exit';
drawLine(line)
drawLine(line);
}

function handleExit() {
scope.handleExit = function() {
// try to unit tests this, lol
if (confirm('You sure ? ( ͡° ͜ʖ ͡°)')) {
window.close();
}
}

function handleLol() {
scope.handleLol = function() {
line = '( ͡° ͜ʖ ͡°)';
drawLine(line);
}

function handlePwd() {
scope.handlePwd = function() {
line = 'Currently in: ' + scope.currentDirName;
drawLine(line)
drawLine(line);
}

function handleCat(param) {
scope.handleCat = function(param) {
for (var prop in scope.currentDirList) {
if (param === scope.currentDirList[prop].name && scope.currentDirList[prop].type === 'file') {
CommandService.sendCommand('cat', prop).then(function(data) {
drawLine(data.data.content, true)
});
if (scope.currentDirList.hasOwnProperty(prop)) {
if (param === scope.currentDirList[prop].name && scope.currentDirList[prop].type === 'file') {
CommandService.sendCommand('cat', prop).then(function(data) {
drawLine(data.data.content, true);
});
}
}
}
}

// so angular-way :/
function handleClear() {
scope.handleClear = function() {
results[0].innerHTML = '';
}

function handleList(param) {

scope.handleList = function(param) {
if (!param) {
for (var prop in scope.currentDirList) {
line += scope.currentDirList[prop].name + ' ';
if (scope.currentDirList.hasOwnProperty(prop)) {
line += scope.currentDirList[prop].name + ' ';
}
}
drawLine(line);
} else {
for (var prop in scope.currentDirList) {
if (param === scope.currentDirList[prop].name) {
if (param === scope.currentDirList[prop].name && scope.currentDirList.hasOwnProperty(prop) && scope.currentDirList[prop].type === 'folder') {
CommandService.sendCommand('ls', prop).then(function(data) {
var data = data.data.items;
for (var prop in data) {
line += data[prop].name + ' ';
}
drawLine(line)
}, function errorCallback(data) {
drawLine(data.data)
drawLine(data.data);
});
}
}
Expand All @@ -178,45 +184,44 @@ pipboy.directive('terminal', function(CommandService) {
}


function handleCd(param) {
scope.handleCd = function(param) {
if (param) {
for (var prop in scope.currentDirList) {
if (param === scope.currentDirList[prop].name && scope.currentDirList[prop].type === 'folder') {
if (param === scope.currentDirList[prop].name && scope.currentDirList[prop].type === 'folder' && scope.currentDirList.hasOwnProperty(prop)) {
scope.currentDirName += '/' + param;
scope.currentDirIndex = prop
scope.currentDirIndex = prop;

CommandService.sendCommand('ls', prop).then(function(data) {
scope.currentDirList = data.data.items
scope.currentDirList = data.data.items;
})
break;
}
}
}
if (!param || param === '..') {
scope.currentDirName = '/home'
scope.currentDirIndex = 0
scope.currentDirName = '/home';
scope.currentDirIndex = 0;

CommandService.sendCommand('ls', scope.currentDirIndex).then(function(data) {
scope.currentDirList = data.data.items
scope.currentDirList = data.data.items;
});
}

}

function nextCommand() {
scope.nextCommand = function() {
if (scope.commandIndex === -1) {
return;
}

if (scope.commandIndex < scope.commandHistory.length - 1) {
scope.commandLine = scope.commandHistory[++scope.commandIndex];
scope.$apply();
} else {
scope.commandLine = '';
}
}

function previousCommand() {
scope.previousCommand = function() {
if (scope.commandIndex === -1) {
scope.commandIndex = scope.commandHistory.length;
}
Expand All @@ -237,33 +242,32 @@ pipboy.directive('terminal', function(CommandService) {
scope.inputActions = function($event) {

if ($event.keyCode === 38) {
previousCommand();

scope.previousCommand();
}

if ($event.keyCode === 40) {
nextCommand();
scope.nextCommand();
}

if ($event.keyCode === 9) {
$event.preventDefault();

var query = commandParser(scope.commandLine).param
if (query) {
handleAutoComplete(query)
scope.handleAutoComplete(query);
}
}

}


scope.execute = function() {
var commandObj = commandParser(scope.commandLine)
var commandObj = commandParser(scope.commandLine);

var fullCommandToPrint = scope.prompt + ' ' + scope.currentDirName + ' ' + scope.commandLine
var fullCommandToPrint = scope.prompt + ' ' + scope.currentDirName + ' ' + scope.commandLine;

drawLine(fullCommandToPrint)
commandHanlder(commandObj.command, commandObj.param)
drawLine(fullCommandToPrint);
commandHanlder(commandObj.command, commandObj.param);
scope.commandHistory.push(scope.commandLine);

scope.commandLine = '';
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@
"time-grunt": "^1.1.0",
"grunt-karma": "^0.10.1",
"karma": "^0.12.31",
"karma-ng-html2js-preprocessor": "^0.1.2",
"karma-script-launcher": "^0.1.0",
"jasmine-reporters": "^2.0.5",
"karma-junit-reporter": "^0.2.2",
"grunt-contrib-copy": "^0.8.0",
"jasmine-core": "^2.2.0",
"karma-jasmine": "^0.3.5",
"matchdep": "^0.3.0",
"karma-ng-html2js-preprocessor": "^0.1.2",
"karma-coverage": "^0.2.7",
"karma-phantomjs-launcher": "^0.1.4",
"grunt-karma-coveralls": "^2.5.2",
"karma-coverage": "^0.2.7"
"grunt-karma-coveralls": "^2.5.2"
}
}
Loading

0 comments on commit 457b560

Please sign in to comment.