| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- 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
- */
- this.cwd = '/home/pgs'
- var getPrompt = function() {
- if (self.su_active) {
- return '<span style="color:purple">root </span>' +
- '<span style="color:tomato">' + self.cwd + ' </span>' +
- '<span style="color:red">% </span>'
- }
- return '<span style="color:blueviolet">pgs </span>' +
- '<span style="color:sienna">' + self.cwd + ' </span>' +
- '<span style="color:green">$ </span>'
- }
- 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': {
- 'about.md':
- '# Parsley Gardens\n\nSite created by ** Weiyi Lou ** ' +
- new Date().getFullYear(),
- 'git.txt':
- 'Code Repository at <a target="_blank"' +
- 'href="https://code.parsleygardens.net/explore/projects">' +
- 'code.parsleygardens.net</a>',
- 'magic.txt': 'slightlymagic.com.au'
- }
- },
- '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()
- 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 output = ''
- for (var item in contents) {
- output += item + "\n"
- }
- show(output)
- } else {
- show("ls: " + args + ": no such directory")
- }
- }
- this.pwd = function(args) {
- show(self.cwd)
- }
- this.question = function(args) {
- var questions = [
- 'Isn\'t <a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ"' +
- ' target="_blank">this song</a> 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.0.0\nBuilt with ' +
- '<a target="_blank" href="http://riotjs.com/">Riot</a>')
- }
- /**
- * 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
- }
- }
|