Weiyi Lou 10 лет назад
Родитель
Сommit
5fc902dec0
1 измененных файлов с 51 добавлено и 19 удалено
  1. 51 19
      js/pgsh.js

+ 51 - 19
js/pgsh.js

@@ -1,12 +1,12 @@
 function pgsh() {
-  this.prompt = '<span style="color:purple">pgs </span><span style="color:green">$ </span>'
+  this.prompt = '<span style="color:blueviolet">pgs </span><span style="color:green">$ </span>'
   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\
-Enter "help" for list of commands\n\
+Type "help" for list of commands\n\
 \n'
   this.commands = {
     'about': {
@@ -18,7 +18,9 @@ Enter "help" for list of commands\n\
     },
     'clear': {
       'help': 'Clears the screen\nUsage: clear',
-      'run' : function() { return 'clear' }
+      'run' : function(args, shell, terminal) {
+        terminal.tags.display.clear()
+      }
     },
     'version': {
       'help': 'Shell Information\nUsage: version',
@@ -56,13 +58,12 @@ Enter "help" for list of commands\n\
     },
     'su': {
       'help': 'Substitute as root user for Phenomenal Cosmic Power\nUsage: su',
-      'run': function(args, context) {
+      'run': function(args, shell, terminal) {
         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
+        if (!shell.su) {
+          terminal.tags.commandline.setPrompt('<span style="color:tomato">root </span><span style="color:red">% </span>')
+          shell.su = true
           output = 'With Great Power comes Great'
         }
         return output
@@ -70,27 +71,59 @@ Enter "help" for list of commands\n\
     },
     'exit': {
       'help': 'Usage: exit',
-      'run': function(args, context) {
+      'run': function(args, shell, terminal) {
         if (args.length > 0) { return this.help }
         var output = 'Close browser window to exit'
-        if (context.su) {
-          context.prompt = context.oldprompt
-          context.su = false
+        if (shell.su) {
+          terminal.tags.commandline.setPrompt(shell.prompt)
+          shell.su = false
           output = ''
         }
         return output
       }
     },
+    'search': {
+      'help': 'Search the web\nUsage: search [query]',
+      'run': function(args) {
+        window.open('https://duckduckgo.com/?q=' + args.join('+'), '_blank')
+        return 'Searching for "' + args.join(' ') + '" in new window...'
+      }
+    },
+    'questions': {
+      'help': 'Answer some questions!\nUsage: questions',
+      'run': function() {
+        return {
+          'run': function(input, terminal) {
+            if (!this.running) {
+              this.running = true
+              terminal.tags.display.hide()
+              terminal.tags.display.add('Welcome to the questions. Do you wish to continue?')
+              terminal.tags.commandline.hidePrompt()
+              return
+            }
+            if (input != 'exit') {
+              terminal.tags.display.set('You answered the last question with "' + input + '"!\nWould you like another question?\n(Type "exit" to end)')
+            } else {
+              this.running = false
+              terminal.tags.display.restore()
+              terminal.tags.display.add('Thanks for answering questions!')
+              terminal.tags.commandline.showPrompt()
+              terminal.returnToShell()
+            }
+          }
+        }
+      }
+    },
     'help': {
       'help': 'List all commands or view information for a command\nUsage: help [command]',
-      'run': function(args, context) {
+      'run': function(args, shell) {
         command = args.join(' ').trim()
-        if (command in context.commands) {
-          return context.commands[command].help
+        if (command in shell.commands) {
+          return shell.commands[command].help
         } else {
           var output = (command) ? 'Command not found: ' + command + '\n' : ''
           var commands = [];
-          for(var name in context.commands) {
+          for(var name in shell.commands) {
             if (name !== 'Parent') { commands.push(name) }
           }
           output += 'Available commands:\n' + commands.sort().join(' ')
@@ -99,15 +132,14 @@ Enter "help" for list of commands\n\
       }
     }
   }
-  this.process = function(input) {
+  this.run = function(input, terminal) {
     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 (command in this.commands) {
-      var shell = this
-      output += this.commands[command].run(args, shell)
+      output = this.commands[command].run(args, this, terminal)
     }
     return output
   }