Pārlūkot izejas kodu

Move commands into individual functions

Weiyi Lou 10 gadi atpakaļ
vecāks
revīzija
c5d4dbbd31
1 mainītis faili ar 122 papildinājumiem un 151 dzēšanām
  1. 122 151
      js/pgsh.js

+ 122 - 151
js/pgsh.js

@@ -7,15 +7,14 @@ function pgsh(ev) {
     var command = self.active ? self.active : parts.splice(0, 1).toString()
     var args = parts
     if (command in self.commands) {
-      self.commands[command].run(args)
+      self[command](args)
     } else if (command) {
       show(command + ' not found')
     }
   })
 
-  var show = function(text) {
-    ev.trigger('disp_add', text)
-  }
+  var show = function(text) { ev.trigger('disp_add', text) }
+  var set = function(text) { ev.trigger('disp_set', text) }
 
   this.prompt = '<span style="color:blueviolet">pgs </span>' +
     '<span style="color:green">$ </span>'
@@ -27,155 +26,127 @@ function pgsh(ev) {
     '"He maketh me to lie down in green pa..."\n\n' +
     'Type `help` for list of commands\n\n'
   this.commands = {
-    'about': {
-      'help': 'Author Information\nUsage: about',
-      'run': function(args) {
-        if (args.length > 0) { show(this.help); return }
-        show('Site by Weiyi Lou ' + new Date().getFullYear() + '\n\n')
-      }
-    },
-    'clear': {
-      'help': 'Clears the screen\nUsage: clear',
-      'run' : function(args) {
-        ev.trigger('disp_clear')
-      }
-    },
-    'version': {
-      'help': 'Shell Information\nUsage: version',
-      'run': function(args) {
-        if (args.length > 0) { show(this.help); return }
-        show('Parsley Gardens Shell (pgsh) 1.0.0 Built with ' +
-             '<a target="_blank" href="http://riotjs.com/">Riot</a>')
-      }
-    },
-    'git': {
-      'help': 'Link to Code Repository\nUsage: git',
-      'run': function(args) {
-        if (args.length > 0) { show(this.help); return }
-        show('Code Repository at <a target="_blank" ' +
-             'href="https://code.parsleygardens.net/explore/projects">' +
-             'code.parsleygardens.net</a>')
-      }
-    },
-    'hello': {
-      'help': 'Say hello to the computer\nUsage: hello [name]',
-      'run': 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 + '"')
-        }
-      }
-    },
-    'magic': {
-      'help': 'Link to Artist\nUsage: magic',
-      'run': function(args) {
-        if (args.length > 0) { show(this.help); return }
-        show('Animation and Illustration at <a target="_blank" ' +
-             'href="http://slightlymagic.com.au">slightlymagic.com.au</a>')
-      }
-    },
-    'su': {
-      'help': 'Gain Phenomenal Cosmic Power\nUsage: su',
-      'run': function(args) {
-        if (args.length > 0) { show(this.help); return }
-        if (!self.su) {
-          self.su = 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', self.prompt_su)
-        }
-      }
-    },
-    'exit': {
-      'help': 'Leave the current context\nUsage: exit',
-      'run': function(args) {
-        if (args.length > 0) { show(this.help); return }
-        if (self.su) {
-          self.su = false
-          ev.trigger('prompt_set', self.prompt)
-          return
-        }
-        show('Close browser window to exit')
-      }
-    },
-    'search': {
-      'help': 'Search the Web (with a Duck)\nUsage: search [query]',
-      'run': function(args) {
-        if (!args.join(' ').trim()) { show(this.help); return }
-        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)
+    'about': 'Author Information',
+    'clear': 'Clears the screen',
+    'version': 'Shell Information',
+    'git': 'Link to Code Repository',
+    'hello': 'Say hello to the computer\nUsage: hello [name]',
+    'magic': 'Link to Artist',
+    'su': 'Gain Phenomenal Cosmic Power',
+    'exit': 'Leave the current context',
+    'search': 'Search the Web (with a Duck)\nUsage: search [query]',
+    'questions': 'Answer some questions!',
+    'help': 'List commands or view information for one\nUsage: help [command]'
+  }
+
+  /**
+   * The Commands
+   */
+  this.about = function(args) {
+    show('Site by Weiyi Lou ' + new Date().getFullYear() + '\n')
+  }
+  this.clear = function(args) {
+    ev.trigger('disp_clear')
+  }
+  this.version = function(args) {
+    show('Parsley Gardens Shell (pgsh) 1.0.0 Built with ' +
+         '<a target="_blank" href="http://riotjs.com/">Riot</a>')
+  }
+  this.git = function(args) {
+    show('Code Repository at <a target="_blank" ' +
+         'href="https://code.parsleygardens.net/explore/projects">' +
+    'code.parsleygardens.net</a>')
+  }
+  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.magic = function(args) {
+    show('Animation and Illustration at <a target="_blank" ' +
+         'href="http://slightlymagic.com.au">slightlymagic.com.au</a>')
+  }
+  this.su = function(args) {
+    if (self.su !== true) {
+      self.su = 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')
       }
-    },
-    'questions': {
-      'help': 'Answer some questions!\nUsage: questions',
-      'run': function(args) {
-        if (!self.active) {
-          self.active = 'questions'
-          ev.trigger('context_swap', 'questions')
-          ev.trigger('disp_set', 'Welcome to the questions. ' +
-                     'Do you want to continue?')
-          return
-        }
-        if (args == 'exit') {
-          self.active = ''
-          ev.trigger('context_swap')
-          show('Thanks for answering questions!')
-          return
-        }
-        var output = 'You answered with "' + args + '"!\nNext question!\n' +
-          '<span style="color:#555">(Type "exit" to end)</span>\n\n'
-        ev.trigger('disp_set', output)
-        var rand = Math.floor(Math.random() * this.questions.length);
-        show(this.questions[rand])
-      },
-      'questions': [
-        'Isn\'t <a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ"' +
-        ' target="_blank">this song</a> the best?',
-        'Iggledy Piggledy?',
-        '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?',
-        '...is essay-writing an exercise in masking ignorance?',
-        'What is the name of the spaces. Between the teeth. Of a comb?',
-        'Why are you dressed up like Ship\'s Captain?'
-      ]
-    },
-    'help': {
-      'help': 'List available commands or view information for a given ' +
-        'command\nUsage: help [command]',
-      'run': function(args) {
-        command = args.join(' ').trim()
-        if (command in self.commands) {
-          show(self.commands[command].help)
-        } else if (command) {
-          show('No information for: ' + command)
-        } else {
-          var commands = [];
-          for(var name in self.commands) {
-            commands.push(name)
-          }
-          show('Available commands:\n' + commands.sort().join(' ') +
-               '\n\n`help [command]` for more information.')
-        }
+      ev.trigger('prompt_set', self.prompt_su)
+    }
+  }
+  this.exit = function(args) {
+    if (self.su) {
+      self.su = false
+      ev.trigger('prompt_set', self.prompt)
+      return
+    }
+    show('Close browser window to exit')
+  }
+  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.questions = function(args) {
+    if (!self.active) {
+      self.active = 'questions'
+      ev.trigger('context_swap', 'questions')
+      set('Welcome to the questions. Do you want to continue?')
+      return
+    }
+    if (args == 'exit') {
+      self.active = ''
+      ev.trigger('context_swap')
+      show('Thanks for answering questions!')
+      return
+    }
+    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?',
+      '...is essay-writing an exercise in masking ignorance?',
+      'What is the name of the spaces between the teeth of a comb?',
+      'Why are you dressed up like Ship\'s Captain?'
+    ]
+    set('You answered with "' + args + '"!\nNext question!\n' +
+      '<span style="color:#555">(Type "exit" to end)</span>\n\n')
+    var rand = Math.floor(Math.random() * questions.length);
+    show(questions[rand])
+  }
+  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' + commands.sort().join(' ') +
+           '\n\n`help [command]` for more information.')
     }
   }
 }