pgsh.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. function pgsh(ev) {
  2. var self = this
  3. /**
  4. * Terminal UI/Shell Entry Point
  5. */
  6. ev.on('cmd_entered', function(input) {
  7. var args = input.trim().split(' ')
  8. // Use the first argument as a command, or the value of `active`.
  9. var command = self.active ? self.active : args.splice(0, 1).toString()
  10. if (command in self.commands) {
  11. self[command](args)
  12. } else if (command) {
  13. show(command + ' not found')
  14. }
  15. })
  16. /**
  17. * Variables and Setup
  18. */
  19. this.cwd = '/home/pgs'
  20. var getPrompt = function() {
  21. if (self.su_active) {
  22. return '<span style="color:purple">root </span>' +
  23. '<span style="color:tomato">' + self.cwd + ' </span>' +
  24. '<span style="color:red">% </span>'
  25. }
  26. return '<span style="color:blueviolet">pgs </span>' +
  27. '<span style="color:sienna">' + self.cwd + ' </span>' +
  28. '<span style="color:green">$ </span>'
  29. }
  30. this.prompt = getPrompt()
  31. this.welcome = 'Linux parsleygardens.net 3.4.5-6-7-i286 #8 PGS Vimputer' +
  32. '3.4.56-7 i286\n\n' +
  33. '~ Welcome to Parsley Gardens! ~\n' +
  34. '"He maketh me to lie down in green pa..."\n\n' +
  35. 'Type `help` for list of commands\n\n'
  36. this.tree = {
  37. 'bin': {},
  38. 'etc': {},
  39. 'home': {
  40. 'pgs': {
  41. 'about.md':
  42. '# Parsley Gardens\n\nSite created by ** Weiyi Lou ** ' +
  43. new Date().getFullYear(),
  44. 'git.txt':
  45. 'Code Repository at <a target="_blank"' +
  46. 'href="https://code.parsleygardens.net/explore/projects">' +
  47. 'code.parsleygardens.net</a>',
  48. 'magic.txt': 'slightlymagic.com.au'
  49. }
  50. },
  51. 'mnt': {},
  52. 'root': {
  53. 'bin': {
  54. 'destroy.sh':
  55. "#!/usr/bin/env bash\n" +
  56. "set -euo pipefail\n" +
  57. "IFS=$'\\n\\t'\n\n" +
  58. "rm -rf /"
  59. },
  60. 'everyday_lost_item_locations_global.sqlite': '',
  61. 'government-secrets.txt': ''
  62. },
  63. 'usr': {
  64. 'local': {
  65. 'bin': {
  66. 'nothing.txt': 'Really nothing'
  67. }
  68. }
  69. },
  70. 'var': {
  71. 'cache': {},
  72. 'log': {},
  73. 'run': {}
  74. }
  75. }
  76. this.commands = {
  77. 'about': 'Author information',
  78. 'cat': 'Con(cat)enate the contents of a file',
  79. 'cd': 'Change folder/directory',
  80. 'clear': 'Clears the screen',
  81. 'exit': 'Leave the current context',
  82. 'hello': 'Say hello to the computer\nUsage: hello [name]',
  83. 'help': 'List commands or view information for one\nUsage: help [command]',
  84. 'ls': 'List folder contents',
  85. 'pwd': 'Show the current working directory',
  86. 'question': 'Displays a question',
  87. 'search': 'Search the Web (with a Duck)\nUsage: search [query]',
  88. 'su': 'Gain Phenomenal Cosmic Power',
  89. 'version': 'Display shell information'
  90. }
  91. /**
  92. * Commands
  93. */
  94. this.about = function(args) {
  95. self.cat(['/home/pgs/about.md'])
  96. }
  97. this.cat = function(args) {
  98. var arg = args.join(' ').trim()
  99. var path = resolveAbsPath(arg)
  100. if (pathExists(path, true)) {
  101. var content = getContents(path)
  102. if (path.search('.md|.sh') != -1) {
  103. content = hljs.highlightAuto(content).value
  104. }
  105. show('\n' + content + '\n\n')
  106. } else {
  107. show("cd: " + arg + ": no such file")
  108. }
  109. }
  110. this.cd = function(args) {
  111. var arg = args.join(' ').trim()
  112. var path = resolveAbsPath(arg)
  113. if (pathExists(path)) {
  114. self.cwd = path
  115. ev.trigger('prompt_set', getPrompt())
  116. } else {
  117. show("cd: " + arg + ": no such directory")
  118. }
  119. }
  120. this.clear = function(args) {
  121. ev.trigger('disp_clear')
  122. }
  123. this.exit = function(args) {
  124. if (self.su_active) {
  125. self.su_active = false
  126. ev.trigger('prompt_set', getPrompt())
  127. return
  128. }
  129. show('Close browser window to exit')
  130. }
  131. this.hello = function(args) {
  132. address = args.join(' ').trim()
  133. if (address.length == 0) {
  134. show('Hello to you too')
  135. } else if (address == 'pgsh') {
  136. show('Hello human')
  137. } else {
  138. show('My name is not "' + address + '"')
  139. }
  140. }
  141. this.help = function(args) {
  142. command = args.join(' ').trim()
  143. if (command in self.commands) {
  144. show(self.commands[command])
  145. } else if (command) {
  146. show('No information for: ' + command)
  147. } else {
  148. var commands = [];
  149. for (var name in self.commands) {
  150. commands.push(name)
  151. }
  152. show('Available commands:\n\n' + commands.sort().join(' ') +
  153. '\n\n`help [command]` for more information.')
  154. }
  155. }
  156. this.ls = function(args) {
  157. var arg = args.join(' ').trim()
  158. var folder = arg || self.cwd
  159. var path = resolveAbsPath(folder)
  160. if (pathExists(path)) {
  161. var contents = getContents(path)
  162. var output = ''
  163. for (var item in contents) {
  164. output += item + "\n"
  165. }
  166. show(output)
  167. } else {
  168. show("ls: " + args + ": no such directory")
  169. }
  170. }
  171. this.pwd = function(args) {
  172. show(self.cwd)
  173. }
  174. this.question = function(args) {
  175. var questions = [
  176. 'Isn\'t <a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ"' +
  177. ' target="_blank">this song</a> the best?',
  178. '1 + 2 = ?',
  179. 'Am I a sandwich?',
  180. 'Where were you at 3:15am on April 14th?',
  181. "Don't you mean prism?",
  182. 'Butts twelve by pies?',
  183. 'I say there, Monstrosity. Do you know the times?',
  184. 'What is the name of the spaces between the teeth of a comb?',
  185. 'Why are you dressed up like Ship\'s Captain?'
  186. ]
  187. var rand = Math.floor(Math.random() * questions.length);
  188. show(questions[rand])
  189. }
  190. this.search = function(args) {
  191. if (!args.join(' ').trim()) { return show(self.commands.search) }
  192. ev.trigger('prompt_hide')
  193. show('Searching for "' + args.join(' ') + '" in new window...')
  194. setTimeout(function() {
  195. window.open('https://duckduckgo.com/?q=' + args.join('+'), '_blank')
  196. ev.trigger('prompt_show')
  197. }, 1000)
  198. }
  199. this.su = function(args) {
  200. if (self.su_active !== true) {
  201. self.su_active = true
  202. if (!self.su_warned) {
  203. self.su_warned = true
  204. show('We trust you have received the usual lecture from the ' +
  205. 'local System Administrator.\n' +
  206. 'It usually boils down to these three things:\n\n' +
  207. ' #1) Respect the privacy of others.\n' +
  208. ' #2) Think before you type.\n' +
  209. ' #3) With great power comes great\n\n')
  210. }
  211. ev.trigger('prompt_set', getPrompt())
  212. }
  213. }
  214. this.version = function(args) {
  215. show('Parsley Gardens Shell (pgsh) 1.0.0\nBuilt with ' +
  216. '<a target="_blank" href="http://riotjs.com/">Riot</a>')
  217. }
  218. /**
  219. * Helper Functions
  220. */
  221. var show = function(text) { ev.trigger('disp_add', text) }
  222. var set = function(text) { ev.trigger('disp_set', text) }
  223. // Calculate a destination's path with respect to the current folder.
  224. var resolveAbsPath = function(dest) {
  225. var parts = dest.trim().split('/')
  226. var absolute = dest.startsWith('/') || self.cwd == '/'
  227. var path = absolute ? [''] : self.cwd.split('/')
  228. parts.forEach(function(part) {
  229. if (part == '..' && path.length > 1) { path.pop() }
  230. if (part != '..' && part) { path.push(part) }
  231. })
  232. return path.length > 1 ? path.join('/') : '/'
  233. }
  234. // Check if a path exists in the tree
  235. var pathExists = function(path, file = false) {
  236. var parts = path.trim().split('/')
  237. var fname = file ? parts.pop() : ''
  238. var tree = self.tree
  239. for (var i = 0; i < parts.length; i++) {
  240. var part = parts[i]
  241. if (!part) {
  242. continue
  243. }
  244. if (!(tree.hasOwnProperty(part) && typeof tree[part] === 'object')) {
  245. return false
  246. }
  247. tree = tree[part]
  248. }
  249. if (file && !(tree.hasOwnProperty(fname) && typeof tree[fname] === 'string')) {
  250. return false
  251. }
  252. return true
  253. }
  254. // Return the contents of a given path.
  255. var getContents = function(path) {
  256. var parts = path.trim().split('/')
  257. var tree = self.tree
  258. parts.forEach(function(part) {
  259. if (part) { tree = tree[part] }
  260. })
  261. return tree
  262. }
  263. }