function pgsh(ev) {
var self = this
/**
* Terminal UI/Shell Entry Point
*/
ev.on('cmd_entered', function(input) {
var args = input.trim().split(' ')
// Use the first argument as a command, or the value of `active`.
var command = self.active ? self.active : args.splice(0, 1).toString()
if (command in self.commands) {
self[command](args)
} else if (command) {
show(command + ' not found')
}
})
/**
* Variables and Setup
*/
var getHome = function() { return self.su_active ? '/root' : '/home/pgs' }
this.cwd = getHome()
var getPrompt = function() {
if (self.su_active) {
return 'root ' +
'' + self.cwd + ' ' +
'% '
}
return 'pgs ' +
'' + self.cwd + ' ' +
'$ '
}
this.prompt = getPrompt()
this.welcome = 'Linux parsleygardens.net 3.4.5-6-7-i286 #8 PGS Vimputer' +
'3.4.56-7 i286\n\n' +
'~ Welcome to Parsley Gardens! ~\n' +
'"He maketh me to lie down in green pa..."\n\n' +
'Type `help` for list of commands\n\n'
this.tree = {
'bin': {},
'etc': {},
'home': {
'pgs': {
'git.txt':
'Code Repository at ' +
'code.parsleygardens.net',
'magic.txt':
'Animation & Illustration at ' +
'slightlymagic.com.au',
'about.md':
'# Parsley Gardens\n' +
'Made by Weiyi Lou ' + new Date().getFullYear(),
}
},
'mnt': {},
'root': {
'bin': {
'destroy.sh':
"#!/usr/bin/env bash\n" +
"set -euo pipefail\n" +
"IFS=$'\\n\\t'\n\n" +
"rm -rf /"
},
'everyday_lost_item_locations_global.sqlite': '',
'government-secrets.txt': ''
},
'usr': {
'local': {
'bin': {
'nothing.txt': 'Really nothing'
}
}
},
'var': {
'cache': {},
'log': {},
'run': {}
}
}
this.commands = {
'about': 'Author information',
'cat': 'Con(cat)enate the contents of a file',
'cd': 'Change folder/directory',
'clear': 'Clears the screen',
'exit': 'Leave the current context',
'hello': 'Say hello to the computer\nUsage: hello [name]',
'help': 'List commands or view information for one\nUsage: help [command]',
'ls': 'List folder contents',
'pwd': 'Show the current working directory',
'question': 'Displays a question',
'search': 'Search the Web (with a Duck)\nUsage: search [query]',
'su': 'Gain Phenomenal Cosmic Power',
'version': 'Display shell information'
}
/**
* Commands
*/
this.about = function(args) {
self.cat(['/home/pgs/about.md'])
}
this.cat = function(args) {
var arg = args.join(' ').trim()
var path = resolveAbsPath(arg)
if (pathExists(path, true)) {
var content = getContents(path)
if (path.search('.md|.sh') != -1) {
content = hljs.highlightAuto(content).value
}
show('\n' + content + '\n\n')
} else {
show("cd: " + arg + ": no such file")
}
}
this.cd = function(args) {
var arg = args.join(' ').trim()
arg = arg ? arg : getHome()
var path = resolveAbsPath(arg)
if (pathExists(path)) {
self.cwd = path
ev.trigger('prompt_set', getPrompt())
} else {
show("cd: " + arg + ": no such directory")
}
}
this.clear = function(args) {
ev.trigger('disp_clear')
}
this.exit = function(args) {
if (self.su_active) {
self.su_active = false
ev.trigger('prompt_set', getPrompt())
return
}
show('Close browser window to exit')
}
this.hello = function(args) {
address = args.join(' ').trim()
if (address.length == 0) {
show('Hello to you too')
} else if (address == 'pgsh') {
show('Hello human')
} else {
show('My name is not "' + address + '"')
}
}
this.help = function(args) {
command = args.join(' ').trim()
if (command in self.commands) {
show(self.commands[command])
} else if (command) {
show('No information for: ' + command)
} else {
var commands = []
for (var name in self.commands) {
commands.push(name)
}
show('Available commands:\n\n' + commands.sort().join(' ') +
'\n\n`help [command]` for more information.')
}
}
this.ls = function(args) {
var arg = args.join(' ').trim()
var folder = arg || self.cwd
var path = resolveAbsPath(folder)
if (pathExists(path)) {
var contents = getContents(path)
var items = []
for (var item in contents) {
items.push(item)
}
items.sort()
var dstat = 'drwxr-xr-x '
var fstat = '-rw-r--r-- '
items.forEach(function(item, key, items) {
var prefix = (typeof contents[item] === 'string') ? fstat : dstat
var suffix = (typeof contents[item] === 'string') ? '' : ''
items[key] = prefix + item + suffix
})
items.unshift([dstat + '..' + ''])
show(items.join('\n'))
} else {
show("ls: " + args + ": no such directory")
}
}
this.pwd = function(args) {
show(self.cwd)
}
this.question = function(args) {
var questions = [
'Isn\'t this song the best?',
'1 + 2 = ?',
'Am I a sandwich?',
'Where were you at 3:15am on April 14th?',
"Don't you mean prism?",
'Butts twelve by pies?',
'I say there, Monstrosity. Do you know the times?',
'What is the name of the spaces between the teeth of a comb?',
'Why are you dressed up like Ship\'s Captain?'
]
var rand = Math.floor(Math.random() * questions.length);
show(questions[rand])
}
this.search = function(args) {
if (!args.join(' ').trim()) { return show(self.commands.search) }
ev.trigger('prompt_hide')
show('Searching for "' + args.join(' ') + '" in new window...')
setTimeout(function() {
window.open('https://duckduckgo.com/?q=' + args.join('+'), '_blank')
ev.trigger('prompt_show')
}, 1000)
}
this.su = function(args) {
if (self.su_active !== true) {
self.su_active = true
if (!self.su_warned) {
self.su_warned = true
show('We trust you have received the usual lecture from the ' +
'local System Administrator.\n' +
'It usually boils down to these three things:\n\n' +
' #1) Respect the privacy of others.\n' +
' #2) Think before you type.\n' +
' #3) With great power comes great\n\n')
}
ev.trigger('prompt_set', getPrompt())
}
}
this.version = function(args) {
show('Parsley Gardens Shell (pgsh) 1.1.0 Built with ' +
'Riot ' +
'Skeleton ' +
'highlight.js')
}
/**
* Helper Functions
*/
var show = function(text) { ev.trigger('disp_add', text) }
var set = function(text) { ev.trigger('disp_set', text) }
// Calculate a destination's path with respect to the current folder.
var resolveAbsPath = function(dest) {
var parts = dest.trim().split('/')
var absolute = dest.startsWith('/') || self.cwd == '/'
var path = absolute ? [''] : self.cwd.split('/')
parts.forEach(function(part) {
if (part == '..' && path.length > 1) { path.pop() }
if (part != '..' && part) { path.push(part) }
})
return path.length > 1 ? path.join('/') : '/'
}
// Check if a path exists in the tree
var pathExists = function(path, file = false) {
var parts = path.trim().split('/')
var fname = file ? parts.pop() : ''
var tree = self.tree
for (var i = 0; i < parts.length; i++) {
var part = parts[i]
if (!part) {
continue
}
if (!(tree.hasOwnProperty(part) && typeof tree[part] === 'object')) {
return false
}
tree = tree[part]
}
if (file && !(tree.hasOwnProperty(fname) && typeof tree[fname] === 'string')) {
return false
}
return true
}
// Return the contents of a given path.
var getContents = function(path) {
var parts = path.trim().split('/')
var tree = self.tree
parts.forEach(function(part) {
if (part) { tree = tree[part] }
})
return tree
}
}