Jelajahi Sumber

Add 'help' support to shell commands

Weiyi Lou 10 tahun lalu
induk
melakukan
53f5e7c66b
1 mengubah file dengan 94 tambahan dan 45 penghapusan
  1. 94 45
      js/pgsh.js

+ 94 - 45
js/pgsh.js

@@ -1,57 +1,106 @@
 function pgsh() {
   this.prompt = '<span style="color:purple">pgs </span><span style="color:green">$ </span>'
   this.welcome = ''
-  this.responses = {
-    'about': function() {
-      return 'Site by Weiyi Lou ' + new Date().getFullYear()
-    },
-    'version': function() {
-      return 'Parsley Gardens Shell (pgsh) 1.0.0. Built with <a target="_blank" href="http://riotjs.com/">Riot.js</a>'
-    },
-    'git': function() {
-      return 'Self-Hosted Code Repository at <a target="_blank" href="https://code.parsleygardens.net/explore/projects">code.parsleygardens.net</a>'
-    },
-    'hello': function() {
-      return 'Hello to you too'
-    },
-    'magic': function() {
-      return 'Animation and Illustration at <a target="_blank" href="http://slightlymagic.com.au">slightlymagic.com.au</a>'
-    },
-    'su': function() {
-      var output = ''
-      if (!this.Parent.su) {
-        this.Parent.oldprompt = this.Parent.prompt
-        this.Parent.prompt = '<span style="color:purple">root </span><span style="color:red">% </span>'
-        this.Parent.su = true
-        output = 'With Great Power comes Great'
-      }
-      return output
-    },
-    'exit': function() {
-      var output = 'Close browser window to exit'
-      if (this.Parent.su) {
-        this.Parent.prompt = this.Parent.oldprompt
-        this.Parent.su = false
-        output = ''
-      }
-      return output
-    },
-    'help': function() {
-      var functions = [];
-      for(var name in this.Parent.responses) {
-        if (name !== 'Parent') {
-          functions.push(name);
+  this.commands = {
+    'about': {
+      'help': 'Author Information\nUsage: about',
+      'run': function(args) {
+        if (args.length > 0) { return this.help }
+        return 'Site by Weiyi Lou ' + new Date().getFullYear()
+      }
+    },
+    'clear': {
+      'help': 'Clears the screen\nUsage: clear',
+      'run' : function() { return 'clear' }
+    },
+    'version': {
+      'help': 'Shell Information\nUsage: version',
+      'run': function(args) {
+        if (args.length > 0) { return this.help }
+        return 'Parsley Gardens Shell (pgsh) 1.0.0. Built with <a target="_blank" href="http://riotjs.com/">Riot.js</a>'
+      }
+    },
+    'git': {
+      'help': 'Link to Code Repository\nUsage: git',
+      'run': function(args) {
+        if (args.length > 0) { return this.help }
+        return 'Self-Hosted Code Repository at <a target="_blank" href="https://code.parsleygardens.net/explore/projects">code.parsleygardens.net</a>'
+      }
+    },
+    'hello': {
+      'help': 'Usage: hello [name]',
+      'run': function(args) {
+        address = args.join(' ').trim()
+        if (address.length == 0) {
+          return 'Hello to you too'
+        } else if (address == 'pgsh') {
+          return 'Hello human'
+        } else {
+          return 'My name is not "' + address + '"'
+        }
+      }
+    },
+    'magic': {
+      'help': 'Link to Artist\nUsage: magic',
+      'run': function(args) {
+        if (args.length > 0) { return this.help }
+        return 'Animation and Illustration at <a target="_blank" href="http://slightlymagic.com.au">slightlymagic.com.au</a>'
+      }
+    },
+    'su': {
+      'help': 'Substitute as root user for Phenomenal Cosmic Power\nUsage: su',
+      'run': function(args, context) {
+        if (args.length > 0) { return this.help }
+        var output = ''
+        if (!context.su) {
+          context.oldprompt = context.prompt
+          context.prompt = '<span style="color:purple">root </span><span style="color:red">% </span>'
+          context.su = true
+          output = 'With Great Power comes Great'
+        }
+        return output
+      }
+    },
+    'exit': {
+      'help': 'Usage: exit',
+      'run': function(args, context) {
+        if (args.length > 0) { return this.help }
+        var output = 'Close browser window to exit'
+        if (context.su) {
+          context.prompt = context.oldprompt
+          context.su = false
+          output = ''
+        }
+        return output
+      }
+    },
+    'help': {
+      'help': 'List all commands or view information for a command\nUsage: help [command]',
+      'run': function(args, context) {
+        command = args.join(' ').trim()
+        if (command in context.commands) {
+          return context.commands[command].help
+        } else {
+          var output = (command) ? 'Command not found: ' + command + '\n' : ''
+          var commands = [];
+          for(var name in context.commands) {
+            if (name !== 'Parent') { commands.push(name) }
+          }
+          output += 'Available commands:\n' + commands.sort().join(' ')
+          return output
         }
       }
-      functions.sort();
-      return 'Available commands:\n' + functions.join(' ')
     }
   }
   this.process = function(input) {
+    var parts = input.trim().split(' ')
+    // First word is the command, all others are arguments.
+    var command = parts.splice(0, 1)
+    var args = parts
     var output = ''
-    if (input in this.responses) {
-      this.responses.Parent = this
-      output += this.responses[input]()
+    if (command in this.commands) {
+      var shell = this
+      output += this.commands[command].run(args, shell)
     }
     return output
   }