pgsh.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. function pgsh(ev) {
  2. var self = this
  3. ev.on('cmd_entered', function(input) {
  4. // First word is the command, all others are arguments.
  5. var parts = input.trim().split(' ')
  6. var command = self.active ? self.active : parts.splice(0, 1).toString()
  7. var args = parts
  8. if (command in self.commands) {
  9. self.commands[command].run(args)
  10. } else if (command) {
  11. show(command + ' not found')
  12. }
  13. })
  14. var show = function(text) {
  15. ev.trigger('disp_add', text)
  16. }
  17. this.prompt = '<span style="color:blueviolet">pgs </span>' +
  18. '<span style="color:green">$ </span>'
  19. var welc = 'Linux parsleygardens.net 3.4.5-6-7-i286 #8 PGS Vimputer ' +
  20. '3.4.56-7 i286\n\n' +
  21. '~ Welcome to Parsley Gardens! ~\n' +
  22. '"He maketh me to lie down in green pa..."\n\n' +
  23. 'Type `help` for list of commands\n\n'
  24. this.welcome = welc
  25. this.commands = {
  26. 'about': {
  27. 'help': 'Author Information\nUsage: about',
  28. 'run': function(args) {
  29. if (args.length > 0) { show(this.help); return }
  30. var me = 'Site by Weiyi Lou ' + new Date().getFullYear() + '\n\n'
  31. show(me)
  32. }
  33. },
  34. 'clear': {
  35. 'help': 'Clears the screen\nUsage: clear',
  36. 'run' : function(args) {
  37. ev.trigger('disp_clear')
  38. }
  39. },
  40. 'version': {
  41. 'help': 'Shell Information\nUsage: version',
  42. 'run': function(args) {
  43. if (args.length > 0) { show(this.help); return }
  44. var ve = 'Parsley Gardens Shell (pgsh) 1.0.0 Built with ' +
  45. '<a target="_blank" href="http://riotjs.com/">Riot.js</a>'
  46. show(ve)
  47. }
  48. },
  49. 'git': {
  50. 'help': 'Link to Code Repository\nUsage: git',
  51. 'run': function(args) {
  52. if (args.length > 0) { show(this.help); return }
  53. var repo = 'Self-Hosted Code Repository at <a target="_blank" ' +
  54. 'href="https://code.parsleygardens.net/explore/projects">' +
  55. 'code.parsleygardens.net</a>'
  56. show(repo)
  57. }
  58. },
  59. 'hello': {
  60. 'help': 'Say hello to the computer\nUsage: hello [name]',
  61. 'run': function(args) {
  62. address = args.join(' ').trim()
  63. if (address.length == 0) {
  64. show('Hello to you too')
  65. } else if (address == 'pgsh') {
  66. show('Hello human')
  67. } else {
  68. show('My name is not "' + address + '"')
  69. }
  70. }
  71. },
  72. 'magic': {
  73. 'help': 'Link to Artist\nUsage: magic',
  74. 'run': function(args) {
  75. if (args.length > 0) { show(this.help); return }
  76. var art = 'Animation and Illustration at <a target="_blank" ' +
  77. 'href="http://slightlymagic.com.au">slightlymagic.com.au</a>'
  78. show(art)
  79. }
  80. },
  81. 'su': {
  82. 'help': 'Gain Phenomenal Cosmic Power\nUsage: su',
  83. 'run': function(args) {
  84. if (args.length > 0) { show(this.help); return }
  85. if (!self.su) {
  86. self.su = true
  87. if (!self.su_warned) {
  88. self.su_warned = true
  89. var wa = 'We trust you have received the usual lecture from the ' +
  90. 'local System Administrator.\n' +
  91. 'It usually boils down to these three things:\n\n' +
  92. ' #1) Respect the privacy of others.\n' +
  93. ' #2) Think before you type.\n' +
  94. ' #3) With great power comes great\n\n'
  95. show(wa)
  96. }
  97. var p = '<span style="color:tomato">root </span>'
  98. p += '<span style="color:red">% </span>'
  99. ev.trigger('prompt_set', p)
  100. }
  101. }
  102. },
  103. 'exit': {
  104. 'help': 'Leave the current context\nUsage: exit',
  105. 'run': function(args) {
  106. if (args.length > 0) { show(this.help); return }
  107. if (self.su) {
  108. self.su = false
  109. ev.trigger('prompt_set', self.prompt)
  110. return
  111. }
  112. show('Close browser window to exit')
  113. }
  114. },
  115. 'search': {
  116. 'help': 'Search the Web (with a Duck)\nUsage: search [query]',
  117. 'run': function(args) {
  118. if (!args.join(' ').trim()) { show(this.help); return }
  119. ev.trigger('prompt_hide')
  120. show('Searching for "' + args.join(' ') + '" in new window...')
  121. setTimeout(function() {
  122. window.open('https://duckduckgo.com/?q=' + args.join('+'), '_blank')
  123. ev.trigger('prompt_show')
  124. }, 1000)
  125. }
  126. },
  127. 'questions': {
  128. 'help': 'Answer some questions!\nUsage: questions',
  129. 'run': function(args) {
  130. if (!self.active) {
  131. self.active = 'questions'
  132. ev.trigger('disp_hide')
  133. ev.trigger('prompt_hide')
  134. show('Welcome to the questions. Do you want to continue?')
  135. return
  136. }
  137. if (args == 'exit') {
  138. self.active = ''
  139. ev.trigger('disp_restore')
  140. ev.trigger('prompt_show')
  141. show('Thanks for answering questions!')
  142. return
  143. }
  144. var output = 'You answered with "' + args + '"!\nNext question!\n' +
  145. '<span style="color:#555">(Type "exit" to end)</span>\n\n'
  146. ev.trigger('disp_set', output)
  147. var rand = Math.floor(Math.random() * this.questions.length);
  148. show(this.questions[rand])
  149. },
  150. 'questions': [
  151. 'Isn\'t <a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ"' +
  152. ' target="_blank">this song</a> the best?',
  153. 'Iggledy Piggledy?',
  154. '1 + 2 = ?',
  155. 'Am I a sandwich?',
  156. 'Where were you at 3:15am on April 14th?',
  157. "Don't you mean prism?",
  158. 'Butts twelve by pies?',
  159. 'I say there, Monstrosity. Do you know the times?',
  160. '...is essay-writing an exercise in masking ignorance?',
  161. 'What is the name of the spaces. Between the teeth. Of a comb?',
  162. 'Why are you dressed up like Ship\'s Captain?'
  163. ]
  164. },
  165. 'help': {
  166. 'help': 'List available commands or view information for a given ' +
  167. 'command\nUsage: help [command]',
  168. 'run': function(args) {
  169. command = args.join(' ').trim()
  170. if (command in self.commands) {
  171. show(self.commands[command].help)
  172. } else if (command) {
  173. show('No information for: ' + command)
  174. } else {
  175. var commands = [];
  176. for(var name in self.commands) {
  177. commands.push(name)
  178. }
  179. show('Available commands:\n' + commands.sort().join(' ') +
  180. '\n\n`help [command]` for more information.')
  181. }
  182. }
  183. }
  184. }
  185. }