Jelajahi Sumber

Make `help` easier to read

Weiyi Lou 9 tahun lalu
induk
melakukan
28987fd330
1 mengubah file dengan 82 tambahan dan 43 penghapusan
  1. 82 43
      js/pgsh.js

+ 82 - 43
js/pgsh.js

@@ -7,7 +7,7 @@ function pgsh(ev) {
   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()
+    var command = self.active ? self.active : args.shift()
     if (command in self.commands) {
       self[command](args)
     } else if (command) {
@@ -15,6 +15,27 @@ function pgsh(ev) {
     }
   })
 
+  /**
+   * Key functions
+   */
+  ev.on('mount', function() {
+    document.getElementById(ev.id).onkeydown = function(e) {
+      var code = e.keyCode || e.which
+      // Tab
+      if (code == 9) {
+        var input = ev.tags['command-line'].command.value.trim().split(' ')
+        var command = input.shift()
+        if (['cat', 'cd', 'ls'].indexOf(command) != -1) {
+          var args = input.join(' ').trim()
+          var isFile = (command == 'cat') ? true : false;
+          var path = completePath(args, isFile)
+          if (path) { ev.trigger('cmd_set', cmd + ' ' + path) }
+        }
+        e.preventDefault()
+      }
+    }
+  })
+
   /**
    * Variables and Setup
    */
@@ -22,20 +43,23 @@ function pgsh(ev) {
   this.cwd = getHome()
   var getPrompt = function() {
     if (self.su_active) {
-      return '<span style="color:tomato">root </span>' +
-      '<span style="color:sienna">' + self.cwd + ' </span>' +
-      '<span style="color:red">% </span>'
+      return '' +
+      '<span style="color:#dc322f">root </span>' +
+      '<span style="color:#b58900">' + self.cwd + ' </span>' +
+      '<span style="color:#268bd2">% </span>'
     }
-    return '<span style="color:blueviolet">pgs </span>' +
-      '<span style="color:sienna">' + self.cwd + ' </span>' +
-      '<span style="color:green">$ </span>'
+    return '' +
+      '<span style="color:#d33682">pgs </span>' +
+      '<span style="color:#b58900">' + self.cwd + ' </span>' +
+      '<span style="color:#2aa198">$ </span>'
   }
   this.prompt = getPrompt()
-  this.welcome = 'Linux parsleygardens.net 3.4.5-6-7-i286 #8 PGS Vimputer' +
+  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'
+    'Type `<strong>help</strong>` for list of commands\n\n'
   this.tree = {
     'bin': {},
     'etc': {},
@@ -50,8 +74,10 @@ function pgsh(ev) {
           'href="http://slightlymagic.com.au">' +
           'slightlymagic.com.au</a>',
         'about.md':
-          '# Parsley Gardens\n' +
-          'Made by Weiyi Lou ' + new Date().getFullYear(),
+          '# Parsley Gardens Website\n\n' +
+          '1. Made by Weiyi Lou ' + new Date().getFullYear() + '\n' +
+          '2. Done for fun.\n' +
+          '3. Not a real terminal.',
       }
     },
     'mnt': {},
@@ -80,27 +106,27 @@ function pgsh(ev) {
     }
   }
   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'
+    'about':    { order: 1, help: 'Author Information' },
+    'cat':      { order: 2, help: 'Con(cat)enate the contents of a file' },
+    'cd':       { order: 2, help: 'Change folder/directory' },
+    'clear':    { order: 2, help: 'Clears the screen' },
+    'exit':     { order: 2, help: 'Leave the current context' },
+    'hello':    { order: 2, help: 'Say hello to the computer\nUsage: hello [name]' },
+    'help':     { order: 1, help: 'List commands or view information for one\nUsage: help [command]' },
+    'ls':       { order: 2, help: 'List folder contents' },
+    'magic':    { order: 1, help: 'The Website of an Artist' },
+    'pwd':      { order: 2, help: 'Show the current working directory' },
+    'question': { order: 1, help: 'Displays a question' },
+    'search':   { order: 2, help: 'Search the Web (with a Duck)\nUsage: search [query]' },
+    'su':       { order: 2, help: 'Gain Phenomenal Cosmic Power' },
+    'version':  { order: 1, help: 'Display shell information' }
   }
 
   /**
    * Commands
    */
-  this.about = function(args) {
-    self.cat(['/home/pgs/about.md'])
-  }
+  this.about = function(args) { self.cat(['/home/pgs/about.md']) }
+  this.magic = function(args) { self.cat(['/home/pgs/magic.txt']) }
   this.cat = function(args) {
     var arg = args.join(' ').trim()
     var path = resolveAbsPath(arg)
@@ -125,9 +151,7 @@ function pgsh(ev) {
       show("cd: " + arg + ": no such directory")
     }
   }
-  this.clear = function(args) {
-    ev.trigger('disp_clear')
-  }
+  this.clear = function(args) { ev.trigger('disp_clear') }
   this.exit = function(args) {
     if (self.su_active) {
       self.su_active = false
@@ -149,16 +173,23 @@ function pgsh(ev) {
   this.help = function(args) {
     command = args.join(' ').trim()
     if (command in self.commands) {
-      show(self.commands[command])
+      show(self.commands[command].help)
     } else if (command) {
       show('No information for: ' + command)
     } else {
-      var commands = []
+      var main = []
+      var other = []
       for (var name in self.commands) {
-        commands.push(name)
+        if (self.commands[name].order == 1) {
+          main.push(name)
+        } else {
+          other.push(name)
+        }
       }
-      show('Available commands:\n\n' + commands.sort().join(' ') +
-           '\n\n`help [command]` for more information.')
+      show('\nAvailable commands:\n\n' +
+           '    ' + main.sort().join(' ') + '\n\n' +
+           '    ' + other.sort().join(' ') + '\n\n' +
+           '`help [command]` for more information.')
     }
   }
   this.ls = function(args) {
@@ -172,22 +203,21 @@ function pgsh(ev) {
         items.push(item)
       }
       items.sort()
-      var dstat = 'drwxr-xr-x <span style="color:deepskyblue">'
+      var dstat = 'drwxr-xr-x <span style="color:#268bd2">'
       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') ? '' : '</span>'
         items[key] = prefix + item + suffix
       })
-      items.unshift([dstat + '..' + '</span>'])
+      items.unshift(dstat + '..</span>')
+      items.unshift('total ' + items.length)
       show(items.join('\n'))
     } else {
       show("ls: " + args + ": no such directory")
     }
   }
-  this.pwd = function(args) {
-    show(self.cwd)
-  }
+  this.pwd = function(args) { show(self.cwd) }
   this.question = function(args) {
     var questions = [
       'Isn\'t <a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ"' +
@@ -205,7 +235,7 @@ function pgsh(ev) {
     show(questions[rand])
   }
   this.search = function(args) {
-    if (!args.join(' ').trim()) { return show(self.commands.search) }
+    if (!args.join(' ').trim()) { return show(self.commands.search.help) }
     ev.trigger('prompt_hide')
     show('Searching for "' + args.join(' ') + '" in new window...')
     setTimeout(function() {
@@ -218,7 +248,8 @@ function pgsh(ev) {
       self.su_active = true
       if (!self.su_warned) {
         self.su_warned = true
-        show('We trust you have received the usual lecture from the ' +
+        show('\n\n' +
+             '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' +
@@ -255,7 +286,7 @@ function pgsh(ev) {
 
   // Check if a path exists in the tree
   var pathExists = function(path, isFile) {
-    var isFile = typeof isFile !== 'undefined' ? isFile : false; // Old way, for Safari.
+    var isFile = (typeof isFile !== 'undefined') ? isFile : false; // Old way, for Safari.
     var parts = path.trim().split('/')
     var fname = isFile ? parts.pop() : ''
     var tree = self.tree
@@ -285,4 +316,12 @@ function pgsh(ev) {
     return tree
   }
 
+  var completePath = function(path, isFile) {
+    // check if we are doing partial name completion
+      // ending slash means completing first content
+      // not ending slash means word completion - cut off bit for completion
+    // first get right directory with absolute path
+    console.log('completing')
+    return ''
+  }
 }