terminal.tag 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * # Terminal Riot Tag
  3. *
  4. * A pretend commandline interface capable of displaying output from a "shell"
  5. * Javascript class. Multiple tags can be used on a page, each will work
  6. * completely independently.
  7. *
  8. * ## Dependencies
  9. *
  10. * - riot.js (http://riotjs.com/)
  11. *
  12. * ## Usage
  13. *
  14. * <terminal shell='myshellclass' welcome='text' prompt='text'></terminal>
  15. *
  16. * <script src='riot+compiler.min.js'></script>
  17. * <script src='myshell.js'></script>
  18. * <script src='terminal.tag' type='riot/tag'></script>
  19. * <script>riot.mount('terminal')</script>
  20. *
  21. * ## Configuring
  22. *
  23. * The terminal tag accepts 3 optional parameters:
  24. *
  25. * - `shell` - Text specifying the shell class to interact with.
  26. * - `welcome` - Text/HTML displayed when a terminal is first mounted.
  27. * - `prompt` - Text/HTML before the commandline input. Defaults to `'$ '`.
  28. *
  29. * With no shell logic, the terminal tag will simply print commands entered.
  30. *
  31. * Note: `welcome` and `prompt` can also be specified in the shell. Shell values
  32. * take priority over parameter values.
  33. *
  34. * ## Making A Shell Class
  35. *
  36. * A shell class should be defined before the tag is mounted by riot. It should
  37. * expect a single parameter (the terminal tag itself) which will be an
  38. * observable object.
  39. *
  40. * This observable will emit `'cmd_entered'` events containing input for
  41. * processing. Here is a minimal shell:
  42. *
  43. * // Contents of myshell.js
  44. * function myshellclass(events) {
  45. * this.prompt = ''
  46. * this.welcome = ''
  47. * events.on('cmd_entered', function(input) {
  48. * // Do processing
  49. * })
  50. * }
  51. *
  52. * ### Events
  53. *
  54. * The observable provides events to make things happen:
  55. *
  56. * events.trigger('disp_add', text) // Append `text` to the display
  57. * events.trigger('disp_set', text) // Display only `text`
  58. * events.trigger('disp_clear') // Clear the display
  59. * events.trigger('prompt_set', text) // Change the prompt to `text`
  60. * events.trigger('prompt_hide') // Hide the command prompt
  61. * events.trigger('prompt_show') // Show the command prompt
  62. *
  63. * There is also an event to swap between sets of displays and prompts:
  64. *
  65. * events.trigger('context_swap', name)
  66. *
  67. * The starting context name is `'default'`. Calling `'context_swap'` with:
  68. *
  69. * - a new name - initialises a new, empty set.
  70. * - an existing name - loads that named set.
  71. * - no name - returns to the `'default'` set.
  72. *
  73. * ### Terminal ID
  74. *
  75. * The observable also provides the id of the terminal tag via an `id` property.
  76. * This can be used, for example, for attaching handlers:
  77. *
  78. * document.getElementById(events.id).onkeypress = function() {}
  79. *
  80. */
  81. <terminal id={ id } tabindex='1'>
  82. <display welcome={ welcome } events={ this } />
  83. <commandline prompt={ prompt } events={ this } />
  84. <style>
  85. terminal { outline: none; }
  86. terminal * { padding: 0; margin: 0; line-height: normal; font-size: 100%; }
  87. </style>
  88. /**
  89. * Create a new shell with the class name given to the terminal tag.
  90. * The terminal tag object passes events between the shell and the other tags.
  91. */
  92. var shell = window[opts.shell] ? new window[opts.shell](this) : {}
  93. this.welcome = shell.welcome || opts.welcome
  94. this.prompt = shell.prompt || opts.prompt
  95. this.id = 'term-' + Math.floor(Math.random() * 10000)
  96. </terminal>
  97. <display>
  98. <div each={ output }>
  99. <raw content={ content } />
  100. </div>
  101. var ev = opts.events
  102. var self = this
  103. this.contexts = {}
  104. this.output = this.contexts['default'] = []
  105. this.on('mount', function() {
  106. this.add(opts.welcome)
  107. })
  108. ev.on('disp_add', function(text) {
  109. self.add(text)
  110. })
  111. ev.on('disp_set', function(text) {
  112. if (text) {
  113. self.clear()
  114. self.add(text)
  115. }
  116. })
  117. ev.on('disp_clear', function() {
  118. self.clear()
  119. })
  120. ev.on('context_swap', function(name) {
  121. name = name || 'default'
  122. if (!(name in self.contexts)) {
  123. self.contexts[name] = []
  124. }
  125. self.update({ output: [] }) // Cleanly disassociate current output first.
  126. self.update({ output: self.contexts[name] })
  127. })
  128. add(text) {
  129. if (text) {
  130. text = text.replace(/\r\n|\r|\n/g, '<br />')
  131. this.output.push({ 'content': text })
  132. this.update()
  133. }
  134. }
  135. clear() {
  136. this.output.length = 0
  137. this.update()
  138. }
  139. </display>
  140. <commandline>
  141. <form autocomplete='off' onsubmit={ process }>
  142. <raw name='lhs' content={ prompt.text } show={ prompt.visible }>
  143. </raw><input type='text' name='command' />
  144. </form>
  145. <style>
  146. commandline input[name='command'],
  147. commandline input[name='command']:hover,
  148. commandline input[name='command']:focus {
  149. padding: 0; margin: 0; line-height: normal; font-size: 100%;
  150. background-color: transparent; border: none; outline: none;
  151. height: auto; width: 70%;
  152. display: inline;
  153. }
  154. </style>
  155. var ev = opts.events
  156. var self = this
  157. this.contexts = {}
  158. this.prompt = this.contexts['default'] = {
  159. text: opts.prompt || '$ ',
  160. visible: true
  161. }
  162. this.on('mount', function() {
  163. this.command.focus()
  164. })
  165. ev.on('prompt_set', function(value) {
  166. self.prompt.text = value
  167. self.tags.lhs.write(value)
  168. })
  169. ev.on('prompt_hide', function() {
  170. self.prompt.visible = false
  171. self.update()
  172. })
  173. ev.on('prompt_show', function() {
  174. self.prompt.visible = true
  175. self.update()
  176. })
  177. ev.on('context_swap', function(name) {
  178. name = name || 'default'
  179. if (!(name in self.contexts)) {
  180. self.contexts[name] = { text: '', visible: true }
  181. }
  182. self.prompt = self.contexts[name]
  183. self.tags.lhs.write(self.prompt.text)
  184. })
  185. process() {
  186. var prompt = this.prompt.visible ? this.prompt.text : ''
  187. var command = this.encode(this.command.value)
  188. ev.trigger('disp_add', prompt + command + '\n')
  189. ev.trigger('cmd_entered', command)
  190. this.command.value = ''
  191. this.command.blur()
  192. this.command.focus() // Refocus to scroll display and keep input in view.
  193. }
  194. encode(text) {
  195. return text.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')
  196. }
  197. </commandline>
  198. <raw>
  199. <span></span>
  200. <style>
  201. raw { white-space: pre-wrap }
  202. </style>
  203. // Set initial html using `content` option, and...
  204. this.on('mount', function() {
  205. this.write(opts.content)
  206. })
  207. // Call `write()` manually to update the html.
  208. write(text) {
  209. this.root.innerHTML = text
  210. }
  211. </raw>