terminal.tag 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * # Terminal Riot Tag
  3. *
  4. * Provides a pretend commandline interface, capable of displaying output from a
  5. * "shell" JavaScript class. Multiple terminal tags can be used on a page, using
  6. * the same shell class or different ones - each terminal will work
  7. * independently.
  8. *
  9. * ## Defaults
  10. *
  11. * The default prompt is `'$ '`.
  12. * The default welcome message is empty.
  13. * The default shell performs no actions: pressing enter will simply move the
  14. * cursor to the next prompt line.
  15. *
  16. * ## Usage
  17. *
  18. * <terminal shell='myshellclass' welcome='text' prompt='text'></terminal>
  19. *
  20. * <script src='riot+compiler.min.js'></script>
  21. * <script src='myshell.js'></script>
  22. * <script src='terminal.tag' type='riot/tag'></script>
  23. * <script>riot.mount('terminal')</script>
  24. *
  25. * ## Dependencies
  26. *
  27. * - riot.js (http://riotjs.com/)
  28. *
  29. * ## Making A Shell Class
  30. *
  31. * A shell class must be defined before the tag is mounted. Shells can keep
  32. * prompt and welcome message settings, and should listen to the `'cmd_entered'`
  33. * event to process input from the `commandline` tag.
  34. *
  35. * Here is the structure of a minimal shell that does nothing:
  36. *
  37. * // Contents of myshell.js
  38. * function myshellclass(events) {
  39. * this.prompt = ''
  40. * this.welcome = ''
  41. * events.on('cmd_entered', function(input) {
  42. * // Do nothing
  43. * })
  44. * }
  45. *
  46. * The shell can trigger events available on the `display` and `commandline`
  47. * tags to make things happen:
  48. *
  49. * events.trigger('disp_add', text) // Append `text` to the display
  50. * events.trigger('disp_set', text) // Display only `text`
  51. * events.trigger('disp_clear') // Clear the display
  52. * events.trigger('disp_hide') // Save the display, then clear it
  53. * events.trigger('disp_restore') // Restore the saved display
  54. * events.trigger('prompt_set', text) // Change the command prompt to `text`
  55. * events.trigger('prompt_hide') // Hide the command prompt
  56. * events.trigger('prompt_show') // Show the command prompt, if hidden
  57. *
  58. */
  59. <terminal>
  60. <display welcome={ welcome } events={ this } />
  61. <commandline prompt={ prompt } events={ this } />
  62. /**
  63. * Create a new shell with the class name given to the terminal tag.
  64. * The terminal tag object passes events between the shell and the other tags.
  65. */
  66. var shell = window[opts.shell] ? new window[opts.shell](this) : {}
  67. this.welcome = shell.welcome || opts.welcome
  68. this.prompt = shell.prompt || opts.prompt
  69. </terminal>
  70. <display>
  71. <div each={ output }>
  72. <raw content={ content } />
  73. </div>
  74. var ev = opts.events
  75. var self = this
  76. this.output = []
  77. this.on('mount', function() {
  78. this.add(opts.welcome)
  79. })
  80. ev.on('disp_add', function(text) {
  81. self.add(text)
  82. })
  83. ev.on('disp_set', function(text) {
  84. if (text) {
  85. self.clear()
  86. self.add(text)
  87. }
  88. })
  89. ev.on('disp_clear', function() {
  90. self.clear()
  91. })
  92. ev.on('disp_hide', function() {
  93. self.saved = self.output.splice(0, self.output.length)
  94. self.clear()
  95. })
  96. ev.on('disp_restore', function() {
  97. if (self.saved.length > 0) {
  98. self.clear()
  99. self.output = self.saved.splice(0, self.saved.length)
  100. self.update()
  101. }
  102. })
  103. add(text) {
  104. if (text) {
  105. text = text.replace(/\r\n|\r|\n/g, '<br />')
  106. this.output.push({ 'content': text })
  107. this.update()
  108. }
  109. }
  110. clear() {
  111. this.output.length = 0
  112. this.update()
  113. }
  114. </display>
  115. <commandline>
  116. <form autocomplete='off' onsubmit={ process }>
  117. <raw name='lhs' content={ prompt } show={ visible } /><input type='text' name='command' />
  118. </form>
  119. <style>
  120. commandline input[name='command'] {
  121. background: transparent;
  122. border: none; outline: none;
  123. padding: 0; margin: 0;
  124. width: 70%;
  125. }
  126. </style>
  127. var ev = opts.events
  128. var self = this
  129. this.prompt = opts.prompt || '$ '
  130. this.visible = true
  131. this.on('mount', function() {
  132. document.getElementsByName('command')[0].focus()
  133. })
  134. ev.on('prompt_set', function(value) {
  135. self.prompt = value
  136. self.tags.lhs.write(value)
  137. })
  138. ev.on('prompt_hide', function() {
  139. self.update({ visible: false })
  140. })
  141. ev.on('prompt_show', function() {
  142. self.update({ visible: true })
  143. })
  144. process() {
  145. var prompt = this.visible ? this.prompt : ''
  146. var command = this.encode(this.command.value)
  147. this.command.value = ''
  148. ev.trigger('disp_add', prompt + command + '\n')
  149. ev.trigger('cmd_entered', command)
  150. }
  151. encode(text) {
  152. return text.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')
  153. }
  154. </commandline>
  155. <raw>
  156. <span></span>
  157. <style>
  158. raw { white-space: pre-wrap }
  159. </style>
  160. // Set the initial html using the `content` option.
  161. this.on('mount', function() {
  162. this.write(opts.content)
  163. })
  164. // Call `write()` manually to update the html.
  165. write(text) {
  166. this.root.innerHTML = text
  167. }
  168. </raw>