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

Add more whitespace preservation actions

This allows command line text to be preserved more accurately, and
allows the shell to do more interesting formatting.
Weiyi Lou 10 лет назад
Родитель
Сommit
98c4ad8160
1 измененных файлов с 24 добавлено и 11 удалено
  1. 24 11
      tags/terminal.tag

+ 24 - 11
tags/terminal.tag

@@ -70,7 +70,7 @@
    */
    */
   process(prompt, input) {
   process(prompt, input) {
     input = he.encode(input)
     input = he.encode(input)
-    var output = prompt + ' ' + input + '\n'
+    var output = prompt + input + '\n'
     var response = this.shell.process(input)
     var response = this.shell.process(input)
     switch(response) {
     switch(response) {
       case 'clear':
       case 'clear':
@@ -79,7 +79,7 @@
       default:
       default:
         this.tags.history.add(output + response)
         this.tags.history.add(output + response)
     }
     }
-    this.tags.commandline.setprompt(this.shell.prompt)
+    this.tags.commandline.setPrompt(this.shell.prompt)
     this.tags.commandline.command.value = ''
     this.tags.commandline.command.value = ''
   }
   }
 
 
@@ -111,11 +111,11 @@
    * Set the prompt to any truthy value.
    * Set the prompt to any truthy value.
    *
    *
    * Note about `lhs.write()` check:
    * Note about `lhs.write()` check:
-   * `setprompt()` fires at `<terminal>` setup, before `lhs.write()` exists.
+   * `setPrompt()` fires at `<terminal>` setup, before `lhs.write()` exists.
    * At that point, `lhs` is just initialised with its `content` option.
    * At that point, `lhs` is just initialised with its `content` option.
    * After that, `lhs.write()` is called for all subsequent prompt changes.
    * After that, `lhs.write()` is called for all subsequent prompt changes.
    */
    */
-  setprompt(value) {
+  setPrompt(value) {
     if (value) {
     if (value) {
       if (typeof this.tags.lhs.write != 'undefined') {
       if (typeof this.tags.lhs.write != 'undefined') {
         this.tags.lhs.write(value)
         this.tags.lhs.write(value)
@@ -129,7 +129,7 @@
   }
   }
 
 
   this.prompt = '$ '
   this.prompt = '$ '
-  this.setprompt(opts.prompt)
+  this.setPrompt(opts.prompt)
 </commandline>
 </commandline>
 
 
 <history>
 <history>
@@ -137,19 +137,32 @@
     <raw content={ content } />
     <raw content={ content } />
   </div>
   </div>
 
 
-  /**
-   * Append any truthy value to command history
-   *
-   * Replaces all newline characters with HTML breaks.
-   */
   add(output) {
   add(output) {
     if (output) {
     if (output) {
-      output = output.replace(/(?:\r\n|\r|\n)/g, '<br />');
+      output = this.preserveWhiteSpace(output)
       this.hist.push({ 'content': output })
       this.hist.push({ 'content': output })
       this.update()
       this.update()
     }
     }
   }
   }
 
 
+  /**
+   * Make sure whitespace displays correctly.
+   *
+   * Keep the following:
+   *
+   * - Newlines.
+   * - Whitespace for display. This means all spaces that are not within tags.
+   */
+  preserveWhiteSpace(text) {
+    text = text.replace(/(?:\r\n|\r|\n)/g, '<br />')
+    // Search for tags or whitespace. Replace whitespace, leave the tags.
+    text = text.replace(/<[^<]+>|( )/g, function(match, group1) {
+      if (group1 == " ") { return '&nbsp;' }
+      return match
+    })
+    return text
+  }
+
   this.hist = []
   this.hist = []
   this.add(opts.welcome)
   this.add(opts.welcome)
 </history>
 </history>