Просмотр исходного кода

Add support for raw html prompts

Weiyi Lou 10 лет назад
Родитель
Сommit
144d6dfb22
3 измененных файлов с 55 добавлено и 44 удалено
  1. 12 6
      index.html
  2. 16 7
      js/pgsh.js
  3. 27 31
      tags/terminal.tag

+ 12 - 6
index.html

@@ -3,7 +3,7 @@
   <head>
     <meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>
     <title>
-      Prompt
+      pgsh
     </title>
     <link rel='stylesheet' type='text/css' href='css/style.css' />
     <script src='js/vendor/riot+compiler.min.js'></script>
@@ -12,9 +12,15 @@
     <script src='tags/terminal.tag' type='riot/tag'></script>
     <script>riot.mount('terminal')</script>
   </head>
-<body>
-  <div class='sc-content'>
-    <terminal shell='pgsh' welcome='Linux parsleygardens.net 3.4.5-6-7-i286 #8 PGS Vimputer 3.4.56-7 i286<br /><br />~ Welcome to Parsley Gardens! ~<br />"He maketh me to lie down in green pa..."<br /><br />Enter "help" for list of commands'></terminal>
-  </div>
-</body>
+  <body>
+    <terminal shell='pgsh' welcome='
+    Linux parsleygardens.net 3.4.5-6-7-i286 #8 PGS Vimputer 3.4.56-7 i286
+
+    ~ Welcome to Parsley Gardens! ~
+    "He maketh me to lie down in green pa..."
+
+    Enter "help" for list of commands
+
+    '></terminal>
+  </body>
 </html>

+ 16 - 7
js/pgsh.js

@@ -1,5 +1,6 @@
 function pgsh() {
-  this.prompt = '$'
+  this.prompt = '<span style="color:purple">pgs </span><span style="color:green">$ </span>'
+  this.welcome = ''
   this.responses = {
     'about': function() {
       return 'This website belongs to Weiyi Lou ' + new Date().getFullYear()
@@ -17,15 +18,23 @@ function pgsh() {
       return 'Animation and Illustration at <a target="_blank" href="http://slightlymagic.com.au">slightlymagic.com.au</a>'
     },
     'su': function() {
-      this.Parent.prompt = '%'
-      return 'With Great Power comes Great'
+      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() {
-      if (this.Parent.prompt == '%') {
-        this.Parent.prompt = '$'
-        return ''
+      var output = 'Close browser window to exit'
+      if (this.Parent.su) {
+        this.Parent.prompt = this.Parent.oldprompt
+        this.Parent.su = false
+        output = ''
       }
-      return 'Close browser window to exit'
+      return output
     },
     'help': function() {
       var functions = [];

+ 27 - 31
tags/terminal.tag

@@ -1,19 +1,11 @@
 <terminal>
-  <history welcome={ opts.welcome } />
-  <commandline prompt={ opts.prompt }/>
+  <history welcome={ welcome } />
+  <commandline prompt={ prompt } />
 
-  this.on('mount', function() {
-    this.setshell()
-    this.tags.commandline.setprompt(this.shell.prompt)
-  })
-
-  setshell() {
-    var shell = { 'process': function() { return ''; } }
-    if (opts.shell !== undefined && window[opts.shell] !== undefined) {
-      shell = window[opts.shell]
-    }
-    this.shell = shell
-  }
+  var shell = { 'process': function() { return ''; } }
+  this.shell = window[opts.shell] ? window[opts.shell] : shell
+  this.welcome = this.shell.welcome ? this.shell.welcome : opts.welcome
+  this.prompt = this.shell.prompt ? this.shell.prompt : opts.prompt
 
   process(prompt, input) {
     var input = he.encode(input)
@@ -27,11 +19,11 @@
 
 <commandline>
   <form autocomplete='off' onsubmit={ process }>
-    { prompt } <input type='text' id='command' />
+    <raw name='lhs' content={ prompt } /><input type='text' name='command' />
   </form>
 
   <style>
-    #command {
+    input[name='command'] {
       background: transparent;
       border: none; outline: none;
       padding: 0; margin: 0;
@@ -40,22 +32,24 @@
   </style>
 
   this.on('mount', function() {
-    document.getElementById('command').focus()
+    document.getElementsByName('command')[0].focus()
   })
 
   setprompt(value) {
     if (value) {
-      this.prompt = value
-      this.update()
+      if (typeof this.tags.lhs.write != 'undefined') {
+        this.tags.lhs.write(value)
+      }
+      this.update({ 'prompt': value })
     }
   }
 
-  this.prompt = '$'
-  this.setprompt(opts.prompt)
-
   process(e) {
     this.parent.process(this.prompt, this.command.value)
   }
+
+  this.prompt = '$ '
+  this.setprompt(opts.prompt)
 </commandline>
 
 <history>
@@ -63,21 +57,23 @@
     <raw content={ content } />
   </div>
 
-  this.hist = []
-
   add(output) {
-    output = output.replace(/(?:\r\n|\r|\n)/g, '<br />');
-    this.hist.push({ 'content': output })
-    this.update()
+    if (output) {
+      output = output.replace(/(?:\r\n|\r|\n)/g, '<br />');
+      this.hist.push({ 'content': output })
+      this.update()
+    }
   }
 
-  if ( opts.welcome !== undefined) {
-    this.add(opts.welcome)
-  }
+  this.hist = []
+  this.add(opts.welcome)
 </history>
 
 <raw>
   <span></span>
 
-  this.root.innerHTML = opts.content
+  write(value) {
+    this.root.innerHTML = value
+  }
+  this.write(opts.content)
 </raw>