Jelajahi Sumber

Add clear-screen support

Weiyi Lou 10 tahun lalu
induk
melakukan
2a4d08b844
1 mengubah file dengan 24 tambahan dan 4 penghapusan
  1. 24 4
      tags/terminal.tag

+ 24 - 4
tags/terminal.tag

@@ -42,6 +42,14 @@
  *
  *     // Shell object used by Terminal Riot Tag
  *     <terminal shell='myshell'></terminal>
+ *
+ * ## Special Return Values
+ *
+ * The Terminal tag can recognise particular return values from the Shell, and
+ * can perform actions other than printing output:
+ *
+ * - `'clear'`. Causes the Terminal to clear the history.
+ *
  */
 <terminal>
   <history welcome={ welcome } />
@@ -57,16 +65,28 @@
    * How to process a command:
    * - Make the input safe by transforming html entities.
    * - Keep the last command in history as we go to the next line.
-   * - Append the shell output, and update the prompt as required.
+   * - Based on shell response, perform special actions or append the output.
+   * - Update the prompt as required.
    */
   process(prompt, input) {
-    var input = he.encode(input)
+    input = he.encode(input)
     var output = prompt + ' ' + input + '\n'
-    output += this.shell.process(input)
-    this.tags.history.add(output)
+    var response = this.shell.process(input)
+    switch(response) {
+      case 'clear':
+        this.clear()
+        break
+      default:
+        this.tags.history.add(output + response)
+    }
     this.tags.commandline.setprompt(this.shell.prompt)
     this.tags.commandline.command.value = ''
   }
+
+  clear() {
+    this.tags.history.hist = []
+    this.update()
+  }
 </terminal>
 
 <commandline>